Redis整合spring总结

  1 一:Redis简介:
  2         Redis是一个开源(BSD许可)的内存数据结构存储,用作数据库,缓存和消息代理。
  3     简单来说,它是一个以(key,value)的形式存储数据的数据库.
  4     官网:https://redis.io/download 去下载对应的版本
  5 二:使用流程:
  6     1.解压:redis-2.4.5-win32-win64.zip
  7         运行对应的系统版本(32bit/64bit)中的redis-server.exe和redis-cli.exe文件来启动服务和输入相应的操作.
  8     2.图形化界面jedis下载网址:https://github.com/xetorthio/jedis。
  9     3.Spring Data Redis 的使用介绍 官网:http://projects.spring.io/spring-data-redis/
 10         3.1引入相关的jar包文件
 11             <dependency>
 12                 <groupId>redis.clients</groupId>
 13                 <artifactId>jedis</artifactId>
 14                 <version>2.6.2</version>
 15             </dependency>
 16             <dependency>
 17                 <groupId>org.apache.commons</groupId>
 18                 <artifactId>commons-pool2</artifactId>
 19                 <version>2.4.1</version>
 20             </dependency>
 21             <dependency>
 22                 <groupId>org.springframework.data</groupId>
 23                 <artifactId>spring-data-redis</artifactId>
 24                 <version>1.5.1.RELEASE</version>
 25             </dependency>
 26         3.2配置applicationContext.xml中的redisTemplate
 27             <!-- 扫描redis的服务类 -->
 28             <context:component-scan base-package="cn.itcast.redis.service" />
 29             <!-- spring管理redis缓存管理器 -->
 30             <bean id="redisCacheManager" 
 31                 class="org.springframework.data.redis.cache.RedisCacheManager">
 32                 <constructor-arg index="0" ref="redisTemplate" />
 33             </bean>
 34             <cache:annotation-driven cache-manager="redisCacheManager"/>
 35             <!-- jedis连接池 -->
 36              <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
 37                 <property name="maxIdle" value="300" />        
 38                 <property name="maxWaitMillis" value="3000" />  
 39                 <property name="testOnBorrow" value="true" />  
 40             </bean>
 41             <!-- jedis连接工厂 -->
 42             <bean id="connectionFactory"  
 43                 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
 44                 p:host-name="localhost" p:port="6379" p:pool-config-ref="poolConfig"  
 45                 p:database="0" />      
 46             <!-- spring-data-redis提供模板 -->
 47             <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
 48                 <property name="connectionFactory" ref="connectionFactory" /> 
 49                 <property name="keySerializer">
 50                     <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
 51                 </property>
 52                 <property name="valueSerializer">
 53                     <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"> 
 54                     </bean>
 55                 </property> 
 56             </bean>
 57         3.3发送邮件保存激活码24H到redis中
 58             //在action类中注入RedisTemplate<String,String>
 59             @Autowired
 60             private RedisTemplate<String,String> redisTemplate;
 61             // 生成激活码
 62             String activecode = RandomStringUtils.randomNumeric(32);
 63             // 将激活码保存到redis,设置24小时失效
 64             redisTemplate.opsForValue().set(model.getTelephone(), activecode, 24,
 65                     TimeUnit.HOURS);
 66         3.4判断激活码是否有效
 67             // 属性驱动
 68             private String activecode;
 69 
 70             public void setActivecode(String activecode) {
 71                 this.activecode = activecode;
 72             }
 73             @Action("customer_activeMail")
 74             public String activeMail() throws IOException {
 75                 ServletActionContext.getResponse().setContentType(
 76                         "text/html;charset=utf-8");
 77                 // 判断激活码是否有效
 78                 String activecodeRedis = redisTemplate.opsForValue().get(
 79                         model.getTelephone());
 80                 if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) {
 81                     // 激活码无效
 82                     ServletActionContext.getResponse().getWriter()
 83                             .println("激活码无效,请登录系统,重新绑定邮箱!");
 84                 } else {
 85                     // 激活码有效
 86                     // 防止重复绑定
 87                     // 调用CRM webService 查询客户信息,判断是否已经绑定
 88                     Customer customer = WebClient
 89                             .create("http://localhost:9002/crm_management/services"
 90                                     + "/customerService/customer/telephone/"
 91                                     + model.getTelephone())
 92                             .accept(MediaType.APPLICATION_JSON).get(Customer.class);
 93                     if (customer.getType() == null || customer.getType() != 1) {
 94                         // 没有绑定,进行绑定
 95                         WebClient.create(
 96                                 "http://localhost:9002/crm_management/services"
 97                                         + "/customerService/customer/updatetype/"
 98                                         + model.getTelephone()).get();
 99                         ServletActionContext.getResponse().getWriter()
100                                 .println("邮箱绑定成功!");
101                     } else {
102                         // 已经绑定过
103                         ServletActionContext.getResponse().getWriter()
104                                 .println("邮箱已经绑定过,无需重复绑定!");
105                     }
106 
107                     // 删除redis的激活码
108                     redisTemplate.delete(model.getTelephone());
109                 }
110                 return NONE;
111             }
112         3.5编写服务器类实现对应的方法(结合WebService技术实现数据的查询与操作)
113             编写实体类(@XmlRootElement)-->服务类的接口
114             (@Path&@Get/@Post/@Push/@Delete/)-->实现类-->Dao
115             @Produces({ "application/xml", "application/json" })/
116             @Consumes({ "application/xml", "application/json" })/
117     
原文地址:https://www.cnblogs.com/yshang/p/8081842.html