使用RedisTemplate操作Redis

使用RedisTemplate操作Redis

  • 补充:List,Set,Map 区别
    • List,Set都继承自Collection接口,Map不是
    • List特点:元素有放入顺序,元素可以重复
    • Set特点:元素无放入顺序,元素不可重复,重复元素会被覆盖。
      • (元素虽然放入无顺序,但是元素在set中的位置是由该元素的HashCode决定的,其位置其实是固定的)
    • Map特点:适合存储键值对的数据
  • Redis配置:spring-redis.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- jedis-2.7.2.jar 依赖jar包 commons-pool2-2.3.jar
jedis基于 commons-pool2-2.3.jar 自己实现了一个资源池。
配置参数 详见 http://blog.csdn.net/liang_love_java/article/details/50510753
-->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--maxIdle 最大空闲资源数,默认值 8 (int类型)-->
<property name="maxIdle" value="1" />
<!--maxTotal 允许创建资源的最大数量,默认值 8,-1 代表无数量限制(int类型)-->
<property name="maxTotal" value="5" />
<!--blockWhenExhausted 默认值 true ,当资源耗尽时,是否阻塞等待获取资源-->
<property name="blockWhenExhausted" value="true" />
<!--maxWaitMillis 获取资源时的等待时间,单位毫秒。当 blockWhenExhausted 配置为 true 时,
此值有效。 -1 代表无时间限制,一直阻塞直到有可用的资源。(long类型)-->
<property name="maxWaitMillis" value="30000" />
<!--testOnBorrow 默认值 false ,当设置为true时,
每次从池中获取资源时都会调用 factory.validateObject() 方法 -->
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<!--property name="hostName" value="10.1.8.200" /-->
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
</beans>
  • Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Created by Neko on 2017/11/7.
*/
public class Test1 {

public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
final RedisTemplate<String,Object> redisTemplate = ctx.getBean("redisTemplate",RedisTemplate.class);
//添加一个key
ValueOperations<String,Object> value = redisTemplate.opsForValue();
value.set("key1","HELLO!!");
//获取这个Key的值
System.out.println(value.get("key1"));

//添加一个Hash集合
//HashOperations<Key,hashkey,value>
//key用来区分是来自哪个HashMap
//hashkey是HashMap中的key
HashOperations<String,Object,Object> hash = redisTemplate.opsForHash();
Map<String,Object> map1 = new HashMap<String,Object>();
Map<String,Object> map2 = new HashMap<String,Object>();
map1.put("name","Apple");
map1.put("age","40");
map2.put("name","Orange");
map2.put("age","60");
hash.putAll("map1",map1);
hash.putAll("map2",map2);
//获取map
System.out.println("MAP1: "+hash.entries("map1"));
System.out.println("MAP2: "+hash.entries("map2"));

//添加一个list列表
ListOperations<String,Object> list = redisTemplate.opsForList();
list.rightPush("oplist1","1st");
list.rightPush("oplist1","2nd");
list.rightPush("oplist1","3rd");
//输出list
System.out.println(list.range("oplist1",0,2));
list.leftPush("oplist2","1st");
list.leftPush("oplist2","2nd");
list.leftPush("oplist2","3rd");
System.out.println(list.range("oplist2",0,2));

list.rightPush("oplist3","1st");
list.rightPush("oplist3","2nd");
list.rightPush("oplist3","3rd");
System.out.println(list.range("oplist3",0,2));
list.leftPush("oplist3","1st");
list.leftPush("oplist3","2nd");
list.leftPush("oplist3","3rd");
System.out.println(list.range("oplist3",0,5));

//添加一个set集合
SetOperations<String,Object> set = redisTemplate.opsForSet();
set.add("opSet","Apple");
set.add("opSet","13");
set.add("opSet","13");//由于是集合,相同的元素会覆盖掉
//输出set集合
System.out.println(set.members("opSet"));

//添加有序的set集合
ZSetOperations<String,Object> zset = redisTemplate.opsForZSet();
zset.add("opZset","Apple",2);
zset.add("opZset","Orange",0);
zset.add("opZset","Banana",1);
//输出有序set集合
System.out.println(zset.rangeByScore("opZset",0,2));
}
}
# ,
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×