Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。...
生活随笔
收集整理的這篇文章主要介紹了
Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
maven使用:
<!--redis jar包--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.6.2.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-ehcache</artifactId><version>1.0.0</version></dependency>?
?
1:redis的連接信息
#訪問地址 redis.host=127.0.0.1 #訪問端口 redis.port=6379 #注意,如果沒有password,此處不設置值,但這一項要保留 redis.password=#最大空閑數,數據庫連接的最大空閑時間。超過空閑時間,數據庫連接將被標記為不可用,然后被釋放。設為0表示無限制。 redis.maxIdle=300 #連接池的最大數據庫連接數。設為0表示無限制 redis.maxActive=600 #最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。 redis.maxWait=1000 #在borrow一個jedis實例時,是否提前進行alidate操作;如果為true,則得到的jedis實例均是可用的; redis.testOnBorrow=true2:spring和redis的整合
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:util="http://www.springframework.org/schema/util" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 11 http://www.springframework.org/schema/util 12 http://www.springframework.org/schema/util/spring-util-4.3.xsd 13 http://www.springframework.org/schema/mvc 14 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 15 http://www.springframework.org/schema/aop 16 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 17 http://www.springframework.org/schema/context 18 http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 19 20 21 <!-- 引入properties配置文件 --> 22 <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> 23 24 <!-- redis連接池 --> 25 <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig"> 26 <property name="maxTotal" value="${redis.maxActive}"></property> 27 <property name="maxIdle" value="${redis.maxIdle}"></property> 28 <property name="maxWaitMillis" value="${redis.maxWait}"></property> 29 <property name="testOnBorrow" value="${redis.testOnBorrow}"></property> 30 </bean> 31 32 <!-- redis連接工廠 --> 33 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 34 <property name="hostName" value="${redis.host}"></property> 35 <property name="port" value="${redis.port}"></property> 36 <property name="password" value="${redis.password}"></property> 37 <property name="poolConfig" ref="jedisConfig"></property> 38 </bean> 39 <!-- redis操作模板,這里采用盡量面向對象的模板 --> 40 <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 41 <property name="connectionFactory" ref="connectionFactory"/> 42 <!-- 如果不配置Serializer,那么存儲的時候只能使用String,如果用對象類型存儲,那么會提示錯誤 can't cast to String!!!--> 43 <property name="keySerializer"> 44 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> 45 </property> 46 <property name="valueSerializer"> 47 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 48 </property> 49 <!--開啟事務--> 50 <property name="enableTransactionSupport" value="true"/> 51 </bean> 52 </beans>3:在業務代碼中使用
1 /** 2 * 使用redis的緩存機制 3 */ 4 @Autowired 5 private RedisTemplate redisTemplate; 6 7 /** 8 * 分頁查詢所有 9 10 * @return 11 */ 12 @Override 13 public List<Goods> listGoods() { 14 List<Goods> goods = goodsDao.listGoods(); 15 for(Goods goods1:goods){ 16 //把每一個goods對象保存到redis中的List中。 17 redisTemplate.opsForList().rightPush("goodsList", goods1); 18 } 19 return goods; 20 }4:測試
@Testpublic void testListGoods(){List<Goods> goods = redisTemplate.opsForList().range("goodsList", 0, -1);if(goods.size()==0){List<Goods> goods1 = goodsService.listGoods();for (Goods goods2:goods1){System.out.println(goods2);}}else {// System.out.println(goods);for(Goods goods1:goods){System.out.println(goods1);}}}可以看到已經保存到redis的數據庫中了。
?
?5:編寫一個RedisTemplate的公用類
package com.betteryanwo.util;import org.springframework.data.redis.core.*;import java.util.List; import java.util.Map; import java.util.Set;/*** Create by 六* Date:18-7-16* RedisTemplate的公用類*/ public class RedisTemplateUtil {private RedisTemplate redisTemplate;public RedisTemplateUtil(RedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}public void set(String key, Object value) {ValueOperations valueOperations = redisTemplate.opsForValue();valueOperations.set(key, value);//BoundValueOperations的理解對保存的值做一些細微的操作 // BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key); }public Object get(String key) {return redisTemplate.opsForValue().get(key);}public void setList(String key, List<?> value) {//Operation 操作。ListOperations listOperations = redisTemplate.opsForList();listOperations.leftPush(key, value);}public Object getList(String key) {return redisTemplate.opsForList().leftPop(key);}public void setSet(String key, Set<?> value) {SetOperations setOperations = redisTemplate.opsForSet();setOperations.add(key, value);}public Object getSet(String key) {return redisTemplate.opsForSet().members(key);}public void setHash(String key, Map<String, ?> value) {HashOperations hashOperations = redisTemplate.opsForHash();hashOperations.putAll(key, value);}public Object getHash(String key) {return redisTemplate.opsForHash().entries(key);}public void delete(String key) {redisTemplate.delete(key);}public void clearAll() {redisTemplate.multi();} }?
轉載于:https://www.cnblogs.com/bulrush/p/9318982.html
總結
以上是生活随笔為你收集整理的Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK1.10+scala环境的搭建之l
- 下一篇: 《VC++深入详解》学习笔记 第一章 W