美文网首页
Spring-data-redis简单学习

Spring-data-redis简单学习

作者: dwwl | 来源:发表于2019-03-20 08:31 被阅读0次

依赖

<!-- 缓存 -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.2.RELEASE</version>
</dependency>

配置文件:

redis-config.properties

# Redis settings 
# server IP 
redis.host=127.0.0.1
# server port 
redis.port=6379
# server pass 
redis.pass= 
# use dbIndex
redis.database=0

redis.maxIdle=300

redis.maxWait=3000

redis.testOnBorrow=true

applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <context:property-placeholder location="classpath*:properties/*.properties" />   
  
   <!-- redis 相关配置 --> 
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
     <property name="maxIdle" value="${redis.maxIdle}" />   
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
     <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
   </bean>  
  
   <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
       p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
   
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        <property name="connectionFactory" ref="JedisConnectionFactory" />  
   </bean>  
      
</beans>  

再父子模块开发中,

因为redis缓存具有通用性,所以放在单独的模块中

当其他模块引用该模块时,通过web.xml中的这种配置进行配置classpath*代表加载本模块以及其引用jar包中的classpath下的配置文件

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationContext*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

使用

@Autowired
private RedisTemplate redisTemplate;

注意在增删改时,删除redis相关的缓存,因为在后台界面常见增删改,都会进行列表的重新刷新,所以对于一些特殊格式的缓存,在后台重新读取列表的方法中进行删缓存的操作

实际的一个应用场景:

在做商品搜索功能时, 搜索条件.png

做上面这种效果时,将品牌数据,规格数据缓存在redis中,因为这种数据不经常发生变化,这种情况比较好的做法是,在操作这些数据的service层中,在增删改方法里添加加入更新缓存的方法,

相关文章

网友评论

      本文标题:Spring-data-redis简单学习

      本文链接:https://www.haomeiwen.com/subject/omeamqtx.html