linux下小試redis demo
8571 ワード
まずredis-server/etc/redis/redisを起動する.conf
package com.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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;
/**
*
* @author luozhonghua
*
*/
public class RedisClient {
private Jedis jedis;//
private JedisPool jedisPool;//
private ShardedJedis shardedJedis;//
private ShardedJedisPool shardedJedisPool;//
public RedisClient() {
initialPool();
initialShardedPool();
shardedJedis = shardedJedisPool.getResource();
jedis = jedisPool.getResource();
}
/**
*
*/
private void initialPool() {
//
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(1000l);
config.setTestOnBorrow(false);
System.out.println("begin...");
jedisPool = new JedisPool(config, "127.0.0.1", 6379);
System.out.println("end...");
}
/**
*
*/
private void initialShardedPool() {
//
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(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 show() {
KeyOperate();
StringOperate();
ListOperate();
SetOperate();
SortedSetOperate();
HashOperate();
jedisPool.returnResource(jedis);
shardedJedisPool.returnResource(shardedJedis);
}
private void KeyOperate() {
System.out.println("======================key==========================");
//
System.out.println(" :"+jedis.flushDB());
// key
System.out.println(" key999 :"+shardedJedis.exists("key999"));
System.out.println(" key001,value001 :"+shardedJedis.set("key001", "value001"));
System.out.println(" key001 :"+shardedJedis.exists("key001"));
// key
System.out.println(" key002,value002 :"+shardedJedis.set("key002", "value002"));
System.out.println(" :");
Set<String> keys = jedis.keys("*");
Iterator<String> it=keys.iterator() ;
while(it.hasNext()){
String key = it.next();
System.out.println(key);
}
// key, key , 。
System.out.println(" key002: "+jedis.del("key002"));
System.out.println(" key002 :"+shardedJedis.exists("key002"));
// key001
System.out.println(" key001 5 :"+jedis.expire("key001", 5));
try{
Thread.sleep(2000);
}
catch (InterruptedException e){
}
// key , 【 】. -1
System.out.println(" key001 :"+jedis.ttl("key001"));
// key
System.out.println(" key001 :"+jedis.persist("key001"));
System.out.println(" key001 :"+jedis.ttl("key001"));
// key
System.out.println(" key :"+jedis.type("key001"));
/*
* :1、 :jedis.rename("key6", "key0");
* 2、 db key db :jedis.move("foo", 1)
*/
}
private void StringOperate()
{
System.out.println("======================String_1==========================");
//
System.out.println(" :"+jedis.flushDB());
System.out.println("============= =============");
jedis.set("key001","value001");
jedis.set("key002","value002");
jedis.set("key003","value003");
System.out.println(" 3 :");
System.out.println(jedis.get("key001"));
System.out.println(jedis.get("key002"));
System.out.println(jedis.get("key003"));
System.out.println("============= =============");
System.out.println(" key003 :"+jedis.del("key003"));
System.out.println(" key003 :"+jedis.get("key003"));
System.out.println("============= =============");
//1、
System.out.println(" key001 :"+jedis.set("key001","value001-update"));
System.out.println(" key001 :"+jedis.get("key001"));
//2、
System.out.println(" key002 :"+jedis.append("key002","+appendString"));
System.out.println(" key002 "+jedis.get("key002"));
System.out.println("============= , , ( )=============");
/**
* mset,mget , ,
* :
* jedis.set("name","ssss");
* jedis.set("jarorwar","xxxx");
*/
System.out.println(" key201,key202,key203,key204 :"+jedis.mset("key201","value201",
"key202","value202","key203","value203","key204","value204"));
System.out.println(" key201,key202,key203,key204 :"+
jedis.mget("key201","key202","key203","key204"));
System.out.println(" key201,key202:"+jedis.del(new String[]{"key201", "key202"}));
System.out.println(" key201,key202,key203,key204 :"+
jedis.mget("key201","key202","key203","key204"));
System.out.println();
//jedis shardedJedis ,
System.out.println("======================String_2==========================");
//
System.out.println(" :"+jedis.flushDB());
System.out.println("============= =============");
System.out.println(" key301 , key301:"+shardedJedis.setnx("key301", "value301"));
System.out.println(" key302 , key302:"+shardedJedis.setnx("key302", "value302"));
System.out.println(" key302 , key302:"+shardedJedis.setnx("key302", "value302_new"));
System.out.println(" key301 :"+shardedJedis.get("key301"));
System.out.println(" key302 :"+shardedJedis.get("key302"));
System.out.println("============= =============");
// key ,
System.out.println(" key303, 2 "+shardedJedis.setex("key303", 2, "key303-2second"));
System.out.println(" key303 :"+shardedJedis.get("key303"));
try{
Thread.sleep(3000);
}
catch (InterruptedException e){
}
System.out.println("3 , key303 :"+shardedJedis.get("key303"));
System.out.println("============= , =============");
System.out.println("key302 :"+shardedJedis.getSet("key302", "value302-after-getset"));
System.out.println("key302 :"+shardedJedis.get("key302"));
System.out.println("============= =============");
System.out.println(" key302 :"+shardedJedis.getrange("key302", 5, 7));
}
private void ListOperate() {
}
private void SetOperate() {
}
private void SortedSetOperate() {
}
private void HashOperate() {
}
public static void main(String[]args){
new RedisClient().show();
}
}