Spring Data Redis

Spring-data-redisspring你们族的一部分,提供了在srping应用中经过简单的配置访redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各类操做、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。redis

spring-data-redis针对jedis提供了以下功能:spring

 

1.链接池自动管理,提供了一个高度封装的“RedisTemplate”api

 

2.针对jedis客户端中大量api进行了归类封装,将同一类型操做封装为operation接口缓存

 

  ValueOperations:简单K-V操做
  SetOperationsset类型数据操做
  ZSetOperationszset类型数据操做
  HashOperations:针对map类型的数据操做
  ListOperations:针对list类型的数据操做app

 

Demo

1)构建Maven工程  SpringDataRedisDemoide

2)引入Spring相关依赖、引入JUnit依赖测试

3)引入JedisSpringDataRedis依赖spa

 

 1 <!-- 缓存 -->
 2 <dependency> 
 3           <groupId>redis.clients</groupId> 
 4           <artifactId>jedis</artifactId> 
 5           <version>2.8.1</version> 
 6 </dependency> 
 7 <dependency> 
 8           <groupId>org.springframework.data</groupId> 
 9           <artifactId>spring-data-redis</artifactId> 
10           <version>1.7.2.RELEASE</version> 
11 </dependency>    
View Code

 

(4)src/main/resources下建立properties文件夹,创建redis-config.properties code

 

1 redis.host=127.0.0.1 
2 redis.port=6379 
3 # redis通常不须要密码
4 redis.pass= 
5 redis.database=0 
6 redis.maxIdle=300 
7 redis.maxWait=3000 
8 redis.testOnBorrow=true 
View Code

 

5)在src/main/resources下建立spring文件夹 ,建立applicationContext-redis.xmlxml

 

 1    <context:property-placeholder location="classpath*:properties/*.properties" />   
 2    <!-- redis 相关配置 --> 
 3    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
 4      <property name="maxIdle" value="${redis.maxIdle}" />   
 5      <property name="maxWaitMillis" value="${redis.maxWait}" />  
 6      <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
 7    </bean>  
 8    <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
 9        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
10    
11    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
12      <property name="connectionFactory" ref="JedisConnectionFactory" />  
13    </bean>  
View Code

 

  maxIdle :最大空闲数

  maxWaitMillis:链接时的最大等待毫秒数

  testOnBorrow:在提取一个jedis实例时,是否提早进行验证操做;若是为true,则获得的jedis实例均是可用的;

4.5.2值类型操做

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
 3 public class TestValue {
 4     @Autowired
 5     private RedisTemplate redisTemplate;    
 6     @Test
 7     public void setValue(){
 8         redisTemplate.boundValueOps("name").set("itcast");        
 9     }    
10     @Test
11     public void getValue(){
12         String str = (String) redisTemplate.boundValueOps("name").get();
13         System.out.println(str);
14     }    
15     @Test
16     public void deleteValue(){
17         redisTemplate.delete("name");;
18     }    
19 }
View Code

4.5.3 Set类型操做

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
 3 public class TestSet {
 4     
 5     @Autowired
 6     private RedisTemplate redisTemplate;
 7     
 8     /**
 9      * 存入值
10      */
11     @Test
12     public void setValue(){
13         redisTemplate.boundSetOps("nameset").add("曹操");        
14         redisTemplate.boundSetOps("nameset").add("刘备");    
15         redisTemplate.boundSetOps("nameset").add("孙权");
16     }
17     
18     /**
19      * 提取值
20      */
21     @Test
22     public void getValue(){
23         Set members = redisTemplate.boundSetOps("nameset").members();
24         System.out.println(members);
25     }
26     
27     /**
28      * 删除集合中的某一个值
29      */
30     @Test
31     public void deleteValue(){
32         redisTemplate.boundSetOps("nameset").remove("孙权");
33     }
34     
35     /**
36      * 删除整个集合
37      */
38     @Test
39     public void deleteAllValue(){
40         redisTemplate.delete("nameset");
41     }
42 }
View Code

4.5.4 List类型操做

建立测试类TestList

1)右压栈

 

 1     /**
 2      * 右压栈:后添加的对象排在后边
 3      */
 4     @Test
 5     public void testSetValue1(){        
 6         redisTemplate.boundListOps("namelist1").rightPush("刘备");
 7         redisTemplate.boundListOps("namelist1").rightPush("关羽");
 8         redisTemplate.boundListOps("namelist1").rightPush("张飞");        
 9     }
10     
11     /**
12      * 显示右压栈集合
13      */
14     @Test
15     public void testGetValue1(){
16         List list = redisTemplate.boundListOps("namelist1").range(0, 10);
17         System.out.println(list);
18     }
View Code

 

运行结果:

[刘备, 关羽, 张飞]

2)左压栈

 

 1     /**
 2      * 左压栈:后添加的对象排在前边
 3      */
 4     @Test
 5     public void testSetValue2(){        
 6         redisTemplate.boundListOps("namelist2").leftPush("刘备");
 7         redisTemplate.boundListOps("namelist2").leftPush("关羽");
 8         redisTemplate.boundListOps("namelist2").leftPush("张飞");        
 9     }
10     
11     /**
12      * 显示左压栈集合
13      */
14     @Test
15     public void testGetValue2(){
16         List list = redisTemplate.boundListOps("namelist2").range(0, 10);
17         System.out.println(list);
18     }
View Code

 

运行结果:

[张飞, 关羽, 刘备]

(3)根据索引查询元素

1     /**
2      * 查询集合某个元素
3      */
4     @Test
5     public void testSearchByIndex(){
6         String s = (String) redisTemplate.boundListOps("namelist1").index(1);
7         System.out.println(s);
8     }
View Code

(4)移除某个元素的值

1     /**
2      * 移除集合某个元素
3      */
4     @Test
5     public void testRemoveByIndex(){
6         redisTemplate.boundListOps("namelist1").remove(1, "关羽");
7     }
View Code

4.5.5 Hash类型操做

建立测试类TestHash

1)存入值

 

1     @Test
2     public void testSetValue(){
3         redisTemplate.boundHashOps("namehash").put("a", "唐僧");
4         redisTemplate.boundHashOps("namehash").put("b", "悟空");
5         redisTemplate.boundHashOps("namehash").put("c", "八戒");
6         redisTemplate.boundHashOps("namehash").put("d", "沙僧");
7     }
View Code

 

2)提取全部的KEY

 

1     @Test
2     public void testGetKeys(){
3         Set s = redisTemplate.boundHashOps("namehash").keys();        
4         System.out.println(s);        
5     }
View Code

 

运行结果:

[a, b, c, d]

3)提取全部的值

 

1     @Test
2     public void testGetValues(){
3         List values = redisTemplate.boundHashOps("namehash").values();
4         System.out.println(values);        
5     }
View Code

 

运行结果:

[唐僧, 悟空, 八戒, 沙僧]

4)根据KEY提取值

 

1     @Test
2     public void testGetValueByKey(){
3         Object object = redisTemplate.boundHashOps("namehash").get("b");
4         System.out.println(object);
5     }
View Code

 

运行结果:

悟空

5)根据KEY移除值

 

1     @Test
2     public void testRemoveValueByKey(){
3         redisTemplate.boundHashOps("namehash").delete("c");
4     }
5 
6 /*
7  * 运行后再次查看集合内容:
8  * [唐僧, 悟空, 沙僧]
9 */
View Code
相关文章
相关标签/搜索