1. 程式人生 > >Redis整合spring總結

Redis整合spring總結

sca eric str 綁定 apach 編寫 登錄 drive his

  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

Redis整合spring總結