package com.test;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.junit.Before;
9 import org.junit.Test;
10
11 import redis.clients.jedis.Jedis;
12
13 public class TestRedis {
14 private Jedis jedis;
15
16 @Before
17 public void setup() {
18 //鏈接redis服務器,192.168.0.100:6379
19 jedis = new Jedis("192.168.0.100", 6379);
20 //權限認證
21 jedis.auth("admin");
22 }
23
24 /**
25 * redis存儲字符串
26 */
27 @Test
28 public void testString() {
29 //-----添加數據----------
30 jedis.set("name","xinxin");//向key-->name中放入了value-->xinxin
31 System.out.println(jedis.get("name"));//執行結果:xinxin
32
33 jedis.append("name", " is my lover"); //拼接
34 System.out.println(jedis.get("name"));
35
36 jedis.del("name"); //刪除某個鍵
37 System.out.println(jedis.get("name"));
38 //設置多個鍵值對
39 jedis.mset("name","liuling","age","23","qq","476777XXX");
40 jedis.incr("age"); //進行加1操做
41 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"));
42
43 }
44
45 /**
46 * redis操做Map
47 */
48 @Test
49 public void testMap() {
50 //-----添加數據----------
51 Map<String, String> map = new HashMap<String, String>();
52 map.put("name", "xinxin");
53 map.put("age", "22");
54 map.put("qq", "123456");
55 jedis.hmset("user",map);
56 //取出user中的name,執行結果:[minxr]-->注意結果是一個泛型的List
57 //第一個參數是存入redis中map對象的key,後面跟的是放入map中的對象的key,後面的key能夠跟多個,是可變參數
58 List<String> rsmap = jedis.hmget("user", "name", "age", "qq");
59 System.out.println(rsmap);
60
61 //刪除map中的某個鍵值
62 jedis.hdel("user","age");
63 System.out.println(jedis.hmget("user", "age")); //由於刪除了,因此返回的是null
64 System.out.println(jedis.hlen("user")); //返回key爲user的鍵中存放的值的個數2
65 System.out.println(jedis.exists("user"));//是否存在key爲user的記錄 返回true
66 System.out.println(jedis.hkeys("user"));//返回map對象中的全部key
67 System.out.println(jedis.hvals("user"));//返回map對象中的全部value
68
69 Iterator<String> iter=jedis.hkeys("user").iterator();
70 while (iter.hasNext()){
71 String key = iter.next();
72 System.out.println(key+":"+jedis.hmget("user",key));
73 }
74 }
75
76 /**
77 * jedis操做List
78 */
79 @Test
80 public void testList(){
81 //開始前,先移除全部的內容
82 jedis.del("java framework");
83 System.out.println(jedis.lrange("java framework",0,-1));
84 //先向key java framework中存放三條數據
85 jedis.lpush("java framework","spring");
86 jedis.lpush("java framework","struts");
87 jedis.lpush("java framework","hibernate");
88 //再取出全部數據jedis.lrange是按範圍取出,
89 // 第一個是key,第二個是起始位置,第三個是結束位置,jedis.llen獲取長度 -1表示取得全部
90 System.out.println(jedis.lrange("java framework",0,-1));
91
92 jedis.del("java framework");
93 jedis.rpush("java framework","spring");
94 jedis.rpush("java framework","struts");
95 jedis.rpush("java framework","hibernate");
96 System.out.println(jedis.lrange("java framework",0,-1));
97 }
98
99 /**
100 * jedis操做Set
101 */
102 @Test
103 public void testSet(){
104 //添加
105 jedis.sadd("user","liuling");
106 jedis.sadd("user","xinxin");
107 jedis.sadd("user","ling");
108 jedis.sadd("user","zhangxinxin");
109 jedis.sadd("user","who");
110 //移除noname
111 jedis.srem("user","who");
112 System.out.println(jedis.smembers("user"));//獲取全部加入的value
113 System.out.println(jedis.sismember("user", "who"));//判斷 who 是不是user集合的元素
114 System.out.println(jedis.srandmember("user"));
115 System.out.println(jedis.scard("user"));//返回集合的元素個數
116 }
117
118 @Test
119 public void test() throws InterruptedException {
120 //jedis 排序
121 //注意,此處的rpush和lpush是List的操做。是一個雙向鏈表(但從表現來看的)
122 jedis.del("a");//先清除數據,再加入數據進行測試
123 jedis.rpush("a", "1");
124 jedis.lpush("a","6");
125 jedis.lpush("a","3");
126 jedis.lpush("a","9");
127 System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1]
128 System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //輸入排序後結果
129 System.out.println(jedis.lrange("a",0,-1));
130 }
131
132 @Test
133 public void testRedisPool() {
134 RedisUtil.getJedis().set("newname", "中文測試");
135 System.out.println(RedisUtil.getJedis().get("newname"));
136 }
137 }
Redis鏈接池:
@Service("redisSessionFactory")
public class RedisSessionFactoryImpl implements RedisSessionFactory {
private static final Logger logger = LoggerFactory.getLogger(RedisSessionFactoryImpl.class);
@Resource
private JedisSentinelPool jedisPool;
@Override
public Jedis getRedisClient() {
try {
Jedis shardJedis = jedisPool.getResource();
return shardJedis;
}
catch (Exception e) {
logger.error("getRedisClent error", e);
}
return null;
}
@Override
public void returnResource(Jedis jedis) {
jedisPool.returnResource(jedis);
}
public void setJedisPool(JedisSentinelPool jedisPool) {
this.jedisPool = jedisPool;
}
}
applicationContext.xml中配置:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="200"/>
<property name="maxIdle" value="150"/>
<property name="maxWaitMillis" value="1000"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool"
scope="singleton">
<constructor-arg index="0" value="${redis.masterName}"/>
<constructor-arg index="1">
<set>
<value>${redis.sentinel1}</value>
<value>${redis.sentinel2}</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="jedisPoolConfig"/>
<constructor-arg index="3" value="${redis.password}"/>
</bean>java