Redis java呼び出しAPI
35013 ワード
接続クライアントリンクの取得
RedisClient.java
様々なタイプのストレージjavaAPI
RedisAPI.java
ここでは簡単なJava呼び出しRedisのcrudメソッドですが、実は簡単です.その中にはいくつかのjarパッケージを自分でインポートする必要があります.私が使っているのは3.0+のredis jarパッケージです.maven中央倉庫では2.8.1しか見つかりません.pomファイルを使っています.
JArパッケージ
コードをgithubに置いたhttps://github.com/xiewenda/javaProject/tree/master/ssm-parent/ssm-redis
RedisClient.java
package redis;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisClient {
private JedisPool jedisPool;//
private ShardedJedisPool shardedJedisPool;//
private static RedisClient redisClient;
private RedisClient()
{
initialPool();
initialShardedPool();
}
public static RedisClient getRedisClient(){
if(redisClient==null){
synchronized (RedisClient.class) {
if(redisClient==null) redisClient = new RedisClient();
}
}
return redisClient;
}
public Jedis getJedis() {
return jedisPool.getResource();
}
public ShardedJedis getShardedJedis() {
return shardedJedisPool.getResource();
}
/** * */
private void initialPool()
{
//
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(false);
jedisPool = new JedisPool(config,"127.0.0.1",6379);
}
/** * */
private void initialShardedPool()
{
//
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(false);
// slave
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6379, "master"));
//
shardedJedisPool = new ShardedJedisPool(config, shards);
}
public void Close() {
jedisPool.close();
shardedJedisPool.close();
}
}
様々なタイプのストレージjavaAPI
RedisAPI.java
package redis;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.SortingParams;
public class RedisAPI {
public static void main(String[] args) {
// // Redis
// Jedis jedis = new Jedis("127.0.0.1");
// System.out.println("Connection to server sucessfully");
// //
// System.out.println("Server is running: "+jedis.ping());
System.out.println(RedisClient.getRedisClient().getShardedJedis());
System.out.println(RedisClient.getRedisClient().getJedis());
}
private Jedis redis;
@Before
public void initConn(){
redis = RedisClient.getRedisClient().getJedis();
}
@Test
public void KeyApi(){
System.out.println("==============key=========================");
//
System.out.println(" :"+redis.flushDB());;
print(" key111 "+redis.exists("key111"));
print(" key111:"+redis.set("key111", "hello world"));
print(" key111:"+redis.get("key111"));
print(" key112:"+redis.set("key112", "key112value"));
print(" key113:"+redis.set("key113", "key113value"));
// key
for(String key : redis.keys("*")){
print(key+":"+redis.get(key));
}
print(" key111:"+redis.del("key111"));
print("key111 :"+redis.exists("key111"));
// key
print(" key112 3 "+redis.expire("key112", 3));
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
print("key112 :"+redis.ttl("key112"));
print("key112 :"+redis.persist("key112"));
print(" key112 :"+redis.exists("key112"));
print(" key112 value :"+redis.type("key112"));
print(" key113 key114:"+redis.rename("key113", "key114"));
print(" key114value:"+redis.get("key114"));
}
@Test
public void StringApi(){
print("=====================string========================");
print(" :"+redis.flushDB());
print("============ =============");
print(" key001:"+redis.set("key001", "key001value"));
print(" key001:"+redis.set("key001", "key-update")+" "+redis.get("key001"));
print(" :"+redis.append("key001", "-append")+" "+redis.get("key001"));
print(" key001:"+redis.del("key001"));
print("================== , ==================");
print(" key021,key022,key023:"+redis.mset("key021","key021value","key022","key022value","key023","key023value"));
print(" key :"+redis.mget("key021","key022","key023"));
print(" key:"+redis.del(new String[]{"key021","key022"}));
print(" key021:"+redis.get("key021"));
print("===========other=======================");
print(" key :"+redis.setnx("key031", "key031value"));
print(" key :"+redis.setnx("key032", "key032value"));
print(" key :"+redis.setnx("key032", "key032value_tow"));
print("key031:"+redis.get("key031"));
print("key032 :"+redis.get("key032"));
print(" key033 :"+redis.setex("key033", 2, "key033value"));
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
print(" key033 :"+redis.get("key033"));
print(" key032:"+redis.getrange("key032",1, 5));
}
@Test
public void ListApi(){
//list 0
print("====================list======================");
print(" :"+redis.flushDB());
print("=========== =================");
print(" list");
print("listString:"+redis.lpush("listString", "vector"));
print("listString:"+redis.lpush("listString", "vector"));
print("listString:"+redis.lpush("listString", "arraylist"));
print("listString:"+redis.lpush("listString", "hashmap"));
print("listNumber:"+redis.lpush("listNumber", "1"));
print("listNumber:"+redis.lpush("listNumber", "4"));
print("listNumber:"+redis.lpush("listNumber", "3"));
print("listNumber:"+redis.lpush("listNumber", "2"));
print("listString :"+redis.lrange("listString", 0, -1));
print("listNumber :"+redis.lrange("listNumber", 0, -1));
print("");
print("============ =================");
print(" :"+redis.lset("listString", -1, "vector-update"));
print(" 0 :"+redis.lindex("listString", 0));
print(" :"+redis.lrange("listString", 0, -1));
print("");
print("============== ================");
print(" , :"+redis.lrem("listString", 1, "vector"));
print(" :"+redis.lrange("listString", 0, -1));
print("");
/*
* list alpha, SortingParams, sort("list"),
* "ERR One or more scores can't be converted into double"
*/
SortingParams sortingParameters = new SortingParams();
sortingParameters.alpha();//
print(" :"+redis.sort("listString",sortingParameters));
print(" :"+redis.sort("listNumber"));
}
@Test
public void SetApi(){
print("=====================set=====================");
print(" :"+redis.flushDB());
print("============ ==============");
print(" sets value1"+redis.sadd("sets", "value1"));
print(" sets value2"+redis.sadd("sets", "value2"));
print(" sets value3"+redis.sadd("sets", "value3"));
print("sets :"+redis.smembers("sets"));
print("=============== =============");
print(" , :"+redis.sadd("sets", "value2"));
print("sets:"+redis.smembers("sets"));
print("============ ==================");
for(String value : redis.smembers("sets")){
print(value);
}
print(" :"+redis.sismember("sets", "value2"));
print("============== =============");
print(" :"+redis.srem("sets", "value2"));
print(" :"+redis.spop("sets"));
print("sets:"+redis.smembers("sets"));
print("");
print("============== =================");
print(" sets1 value1"+redis.sadd("sets1", "value1"));
print(" sets1 value2"+redis.sadd("sets1", "value2"));
print(" sets1 value3"+redis.sadd("sets1", "value3"));
print(" sets2 value3"+redis.sadd("sets2", "value3"));
print(" sets2 value4"+redis.sadd("sets2", "value4"));
print(" sets2 value5"+redis.sadd("sets2", "value5"));
print("sets1 :"+redis.smembers("sets1"));
print("sets2 :"+redis.smembers("sets2"));
print("sets1 sets2 :"+redis.sinter("sets1","sets2"));
print("sets1 sets2 :"+redis.sunion("sets1","sets2"));
print("sets1 sets2 :"+redis.sdiff("sets1","sets2"));
}
@Test
public void HashApi(){
print("================hash=================");
print(" :"+redis.flushDB());
print("================== ================");
print("hashs key001=>value001"+redis.hset("hashs", "key001", "value001"));
print("hashs key002=>value002"+redis.hset("hashs", "key002", "value002"));
print("hashs key003=>value003"+redis.hset("hashs", "key003", "value003"));
print("hashs key004=>4L"+redis.hincrBy("hashs", "key004", 4L));
print("hashs:"+redis.hgetAll("hashs"));
print("=================== ================");
print(" key001:"+redis.hexists("hashs", "key001"));
print(" key001 :"+redis.hget("hashs", "key001"));
print(" key:"+redis.hkeys("hashs"));
print(" value:"+redis.hvals("hashs"));
print("============= ====================");
print(" key001 value001_update:"+redis.hset("hashs", "key001", "value001_update"));
print(" key004 11:"+redis.hincrBy("hashs", "key004", 11L));
print("hashs:"+redis.hvals("hashs"));
print("============= =======================");
print(" key002:"+redis.hdel("hashs", "key002"));
print("hashs:"+redis.hgetAll("hashs"));
}
@Test
public void SortedSetApi(){
//
print("=================SortedSet================");
print(" :"+redis.flushDB());
print("============== ===============");
print(" zsets value1:"+redis.zadd("zsets",1, "value1"));
print(" zsets value2:"+redis.zadd("zsets",2, "value2"));
print(" zsets value3:"+redis.zadd("zsets",3, "value3"));
print(" zsets value4:"+redis.zadd("zsets",4, "value4"));
print(" zsets value4:"+redis.zadd("zsets",4, "value4"));
print("zsets:"+redis.zrange("zsets", 0, -1));
print("=============== ===============");
print(" :"+redis.zcard("zsets"));
print(" :"+redis.zcount("zsets",1,3));
print(" value4 :"+redis.zscore("zsets","value4"));
print("============= ===================");
print("============= ===================");
print(" value4:"+redis.zrem("zsets", "value4"));
print("redis:"+redis.zrange("zsets", 0, -1));
}
public void print(Object obj){
System.out.println(obj);
}
}
ここでは簡単なJava呼び出しRedisのcrudメソッドですが、実は簡単です.その中にはいくつかのjarパッケージを自分でインポートする必要があります.私が使っているのは3.0+のredis jarパッケージです.maven中央倉庫では2.8.1しか見つかりません.pomファイルを使っています.
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
JArパッケージ
コードをgithubに置いたhttps://github.com/xiewenda/javaProject/tree/master/ssm-parent/ssm-redis