《spring-data-redis-注解方式实现缓存》

前言:

       持续学习奋进之路

正文:

新建redis.properties文件

redis.properties-内容

#redis 服务器 IP
redis.host=192.168.22.61

#redis 服务器端口
redis.port=6379

#redis 密码
redis.pass=redis#2017

#redis 支持16个数据库(相当于不同用户)可以使不同的应用程序数据彼此分开同时又存储在相同的实例上
redis.dbIndex=0

#redis 缓存数据过期时间单位秒
redis.expiration=3000

#控制一个 pool 最多有多少个状态为 idle 的jedis实例
redis.maxIdle=300

#控制一个 pool 可分配多少个jedis实例
redis.maxActive=600

#当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
redis.maxWait=1000

#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true

 新建spring-redis.xml配置文件

 

 spring-redis.xml-内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 启用缓存注解功能 -->
    <cache:annotation-driven cache-manager="redisCacheManager"/>

    <!-- 以下是spring-data-redis配置方式 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg>
            <bean class="org.springframework.data.redis.connection.RedisStandaloneConfiguration"
                  c:host-name="${redis.host}" c:port="${redis.port}"/>
        </constructor-arg>
    </bean>

    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
          factory-method="create" c:connection-factory-ref="jedisConnectionFactory"/>

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <property name="hashKeySerializer" ref="stringRedisSerializer"/>
    </bean>
    <!--<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"-->
    <!--p:connection-factory-ref="jedisConnectionFactory"/>-->


    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
          p:connection-factory-ref="jedisConnectionFactory"/>


</beans>

 在spring.xml添加redis的文件引用

 

 spring.xml-内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath:service/spring/spring-context.xml"/>
    <import resource="classpath:service/spring/dubbo.xml"/>
    <import resource="classpath:service/spring/datasource.xml"/>
    <import resource="classpath:service/spring/spring-mybatis.xml"/>

    <import resource="classpath:service/spring/spring-redis.xml"/>  //添加spring-redis文件引入

</beans>

 Service实现中添加注解

 查询是添加注解进行缓存


 

 说明:@Cacheable("a")注解的意义就是把该方法的查询结果放到redis中去,下一次再发起查询就去redis中去取,存在redis中的数据的key就是a;

增删改时删除缓存


 

 说明:@CacheEvict(value={"a","b"},allEntries=true) 的意思就是执行该方法后要清除redis中key名称为a,b的数据;

注:

1.这里保存和清除缓存的方式比较简单粗暴,但在并发量小的情况下,这种方式出现问题几率小,但是一旦并发量较高的情况下,除了value中要加入参数,删除时也要根据相应的参数找到key去删除。

2.设置缓存时间,如果不设置缓存时间,当数据库更新,而方法获取数据总是从缓存中获取,就会出现数据不一致的情况。

使用缓存,要保证数据一致性。

结语:     

        缓存一般使用在服务层,在你想缓存的方法上面添加相应的注解即可,下面三个缓存的注解你得掌握。

        @Cacheable  spring 会在其被调用后将返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。

        @CachePut  标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

        @CacheEvict 用来标注在需要清除缓存元素的方法或类上的。

        当然这些注解里面还有很多其他的属性配置,配合 spring-el 表达式能做的事情还有很多,大概只有你想不到,没有做不到。

         在业务规则比较复杂的情况下,缓存 key 的设计相当重要,设计出色可以使你的应用飞起来。

猜你喜欢

转载自blog.csdn.net/yxf15732625262/article/details/81156297
今日推荐