Redis整合spring總結

來源:互聯網
上載者:User

標籤:解壓   方法   utils   ram   serial   produce   bsd   zip   delete   

  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/)-->實作類別-->Dao115             @Produces({ "application/xml", "application/json" })/116             @Consumes({ "application/xml", "application/json" })/117     

 

Redis整合spring總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.