缓存解决方案—Redis

一、Redis,Jedis,Spring Data Redis

  1.1 Redis

  redis是一款开源的Key-value数据库,运行在内存中,由ANSIC编写。企业开发通常使用Redis来实现缓存。同类产品还有memcache、memcached、MongoDB等。

  1.2 Jedis

  Jedis是Redis官方退出的一款面向Java的客户端,提供了很多借口供Java语言调用。可以在Redis官网下载。还有一些开源爱好者提供的客户端,如Jredis、SRP等等。

  1.3 Spring Data Redis

  Spring Data Redis是spring家族的而一部分,提供了在spring应用中通过简单的配置访问Redis服务,对Redis底层开发包(Jedis、Jredis、RJC)进行了高度封装,RedisTeamplate提供了redis各种操作,异常处理及序列化,支持发布订阅,并对spring3.1cache进行了实现。

  spring-data-redis针对Jedis提供了如下功能:

  1.连接池自动管理,提供了一个高度封装的RedisTemplate类

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

  ValueOperation:简单K-V操作

  SetOperation:set类型数据操作

  ZSetOperation:zset类型数据操作

  HashOperation:针对map类型数据操作

  ListOperation:针对list类型的数据操作

二、Spring Data Redis入门小Demo

  2.1 准备工作

  • 构建Maven工程,SpringDataRedisDemo
  • 引入Spring、JUnit相关坐标依赖
  • 引入Jedis和SpringDataRedis依赖
 1 <!-- spring与Junit相关依赖 -->
 2  3 
 4 <!-- 缓存 -->
 5 <dependency>
 6     <groupId>redis.clients</groupId>
 7     <artifactId>jedis</artifactId>
 8     <version>2.8.1</version>
 9 </dependency>
10 
11 <dependency>
12     <groupId>org.springframework.data</groupId>
13     <artifactId>spring-data-redis</artifactId>
14     <version>1.7.2.RELEASE</version>
15 </dependency>
  • 创建redis-config.properties文件
 1 redis.host=127.0.0.1
 2 
 3 redis.port=6379
 4 
 5 redis.password=  // 密码,这里没有设置
 6 
 7 redis.database=0
 8 
 9 redis.maxIdle=300 // 最大空闲数
10 
11 redis.maxWait=3000 // 连接时的最大等待毫秒数
12 
13 redis.testOnBorrow=true // 在提取一个Jedis实例时,是否提前进行验证操作;true代表是得到的Jedis实例均是可用的
  • 创建applicationContext-redis.xml文件
 1 <?xml version="1.0" encoding="UTF-8"?> 
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
 4   xmlns:context="http://www.springframework.org/schema/context" 
 5   xmlns:mvc="http://www.springframework.org/schema/mvc" 
 6   xmlns:cache="http://www.springframework.org/schema/cache"
 7   xsi:schemaLocation="http://www.springframework.org/schema/beans   
 8             http://www.springframework.org/schema/beans/spring-beans.xsd   
 9             http://www.springframework.org/schema/context   
10             http://www.springframework.org/schema/context/spring-context.xsd   
11             http://www.springframework.org/schema/mvc   
12             http://www.springframework.org/schema/mvc/spring-mvc.xsd 
13             http://www.springframework.org/schema/cache  
14             http://www.springframework.org/schema/cache/spring-cache.xsd">  
15   
16    <context:property-placeholder location="classpath*:properties/*.properties" />   
17   
18    <!-- redis 相关配置 --> 
19    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
20      <property name="maxIdle" value="${redis.maxIdle}" />   
21      <property name="maxWaitMillis" value="${redis.maxWait}" />  
22      <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
23    </bean>  
24    
25   
26    <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
27        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
28    
29    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
30         <property name="connectionFactory" ref="JedisConnectionFactory" />  
31    </bean>  
32       
33 </beans>  

  2.2 RedisTemplate相关操作

  1.值类型操作

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

  2.Set类型操作

 1     /**
 2      * 存值
 3      */
 4      @Test
 5      public void setValue(){
 6          redisTemplate.boundSetOps("nameset").add("曹操");
 7          redisTemplate.boundSetOps("nameset").add("刘备");
 8          redisTemplate.boundSetOps("nameset").add("孙权");
 9      }
10      
11      /**
12      * 取值
13      */
14      @Test
15      public void getValue(){
16          Set members = redisTemplate.boundSetOps("nameset").members();
17          System.out.println(members);
18      }
19      
20      /**
21      * 删除集合中的某一个值
22      */
23      @Test
24      public void deleteValue(){
25          redisTemplate.boundSetOps("nameset").remove("孙权");
26      }
27      
28      /**
29      * 删除整个集合
30      */
31      @Test
32      public void deleteAllValue(){
33          redisTemplate.delete("nameset");
34      }

  3.List类型操作

  • 右压栈
 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      }
  • 左压栈
 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      }
  • 根据索引查询元素
1 /**
2       * 查询集合中的元素
3       */
4      @Test
5      public void testSearchByIndex(){
6          String s = (String) redisTemplate.boundListOps("namelist1").index(1);
7          System.out.println(s);
8      }
  • 移除元素
1    /**
2      * 移除集合某个元素
3      */
4      @Test
5      public void testRemoveByIndex(){
6          redisTemplate.boundListOps("namelist1").remove(1, "关羽");
7      }

  4.Hash类型操作

  • 存值
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      }
  • 提取所有的KEY
1     @Test
2      public void testGetKeys(){
3          Set s = redisTemplate.boundHashOps("namehash").keys();
4          System.out.println(s); 
5      }
  • 提取所有的值
1     @Test
2      public void testGetValues(){
3          List values = redisTemplate.boundHashOps("namehash").values();
4          System.out.println(values); 
5      }
  • 根据KEY提取值
1     @Test
2      public void testGetValueByKey(){
3          Object object = redisTemplate.boundHashOps("namehash").get("b");
4          System.out.println(object);
5      }
  • 根据kEY移除值
1     @Test
2      public void testRemoveValueByKey(){
3          redisTemplate.boundHashOps("namehash").delete("c");
4      }

  当我们的业务中有需要用到redis的时候我们就可以考虑使用Spring Data Redis来实现我们的需求啦!:)

猜你喜欢

转载自www.cnblogs.com/buwei/p/10011691.html