package
com.lujianing.utils;
import
org.junit.Before;
import
org.junit.Test;
import
redis.clients.jedis.Jedis;
import
redis.clients.jedis.JedisPool;
import
redis.clients.jedis.JedisPoolConfig;
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Map;
/**
* Created by lujianing on 14-2-28.
*/
public
class
JedisUtilTest {
JedisPool pool;
Jedis jedis;
@Before
public
void
setUp() {
pool =
new
JedisPool(
new
JedisPoolConfig(),
"192.168.10.61"
);
jedis = pool.getResource();
}
@Test
public
void
testGet(){
System.out.println(jedis.get(
"lu"
));
}
/**
* Redis存储初级的字符串
* CRUD
*/
@Test
public
void
testBasicString(){
jedis.set(
"name"
,
"minxr"
);
System.out.println(jedis.get(
"name"
));
jedis.append(
"name"
,
"jarorwar"
);
System.out.println(jedis.get(
"name"
));
jedis.set(
"name"
,
"闵晓荣"
);
System.out.println(jedis.get(
"name"
));
jedis.del(
"name"
);
System.out.println(jedis.get(
"name"
));
/**
* mset相当于
* jedis.set("name","minxr");
* jedis.set("jarorwar","闵晓荣");
*/
jedis.mset(
"name"
,
"minxr"
,
"jarorwar"
,
"闵晓荣"
);
System.out.println(jedis.mget(
"name"
,
"jarorwar"
));
}
/**
* jedis操作Map
*/
@Test
public
void
testMap(){
Map<String,String> user=
new
HashMap<String,String>();
user.put(
"name"
,
"minxr"
);
user.put(
"pwd"
,
"password"
);
jedis.hmset(
"user"
,user);
List<String> rsmap = jedis.hmget(
"user"
,
"name"
);
System.out.println(rsmap);
System.out.println(jedis.hmget(
"user"
,
"pwd"
));
System.out.println(jedis.hlen(
"user"
));
System.out.println(jedis.exists(
"user"
));
System.out.println(jedis.hkeys(
"user"
));
System.out.println(jedis.hvals(
"user"
));
Iterator<String> iter=jedis.hkeys(
"user"
).iterator();
while
(iter.hasNext()){
String key = iter.next();
System.out.println(key+
":"
+jedis.hmget(
"user"
,key));
}
}
/**
* jedis操作List
*/
@Test
public
void
testList(){
jedis.del(
"java framework"
);
System.out.println(jedis.lrange(
"java framework"
,
0
,-
1
));
jedis.lpush(
"java framework"
,
"spring"
);
jedis.lpush(
"java framework"
,
"struts"
);
jedis.lpush(
"java framework"
,
"hibernate"
);
System.out.println(jedis.lrange(
"java framework"
,
0
,-
1
));
}
/**
* jedis操作Set
*/
@Test
public
void
testSet(){
jedis.sadd(
"sname"
,
"minxr"
);
jedis.sadd(
"sname"
,
"jarorwar"
);
jedis.sadd(
"sname"
,
"闵晓荣"
);
jedis.sadd(
"sanme"
,
"noname"
);
jedis.srem(
"sname"
,
"noname"
);
System.out.println(jedis.smembers(
"sname"
));
System.out.println(jedis.sismember(
"sname"
,
"minxr"
));
System.out.println(jedis.srandmember(
"sname"
));
System.out.println(jedis.scard(
"sname"
));
}
@Test
public
void
test()
throws
InterruptedException {
System.out.println(jedis.keys(
"*"
));
System.out.println(jedis.keys(
"*name"
));
System.out.println(jedis.del(
"sanmdde"
));
System.out.println(jedis.ttl(
"sname"
));
jedis.setex(
"timekey"
,
10
,
"min"
);
Thread.sleep(
5000
);
System.out.println(jedis.ttl(
"timekey"
));
jedis.setex(
"timekey"
,
1
,
"min"
);
System.out.println(jedis.ttl(
"timekey"
));
System.out.println(jedis.exists(
"key"
));
System.out.println(jedis.rename(
"timekey"
,
"time"
));
System.out.println(jedis.get(
"timekey"
));
System.out.println(jedis.get(
"time"
));
jedis.del(
"a"
);
jedis.rpush(
"a"
,
"1"
);
jedis.lpush(
"a"
,
"6"
);
jedis.lpush(
"a"
,
"3"
);
jedis.lpush(
"a"
,
"9"
);
System.out.println(jedis.lrange(
"a"
,
0
,-
1
));
System.out.println(jedis.sort(
"a"
));
System.out.println(jedis.lrange(
"a"
,
0
,-
1
));
}
}