Spring注解式缓存

目录

前言:spring+redis集成已完成的前提下​编辑Spring注解式缓存

1. spring注解式缓存使用步骤

  1.1 配置缓存管理器 

  1.2 配置自定义Key生成器CacheKeyGenerator           缓存的Java对象一定要重写hashCode和eqauls

  1.3 启用缓存注解功能

  1.4 在需要的地方进行注解缓存   

2. 缓存注解

  2.1 @CacheConfig        它是一个类级别的注解,允许共享缓存的名称、KeyGenerator、CacheManager和CacheResolver

   2.2 @Cacheable  

        2.3 @CachePut

  2.4 @CacheEvict

3. Spring-Cache key设置

  3.1 基本形式     

  3.2 组合形式

  3.3 对象形式

  3.4 自定义Key生成器

5. redis缓存的一些注意事项

6. 根据用户ID或公司ID进行查询(此想法未测试)


 

前言:spring+redis集成已完成的前提下Spring注解式缓存

 Redis是key-value存储的非关系型数据库。Spring Data Redis包含了多个模板实现,用来完成Redis数据库的数据存取功能



1. spring注解式缓存使用步骤

  1.1 配置缓存管理器
 

    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg name="redisOperations" ref="redisTemplate" />
        <!--redis缓存数据过期时间单位秒-->
        <property name="defaultExpiration" value="${redis.expiration}" />
        <property name="usePrefix" value="true"/>

        <property name="cachePrefix">
            <bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix">
                <constructor-arg index="0" value="-cache-"/>
            </bean>
        </property>

    </bean>

  1.2 配置自定义Key生成器CacheKeyGenerator

           缓存的Java对象一定要重写hashCode和eqauls

 <bean id="cacheKeyGenerator" class="com.zking.ssm.redis.CacheKeyGenerator"></bean>

  1.3 启用缓存注解功能

     <cache:annotation-driven cache-manager="redisCacheManager" key-generator="cacheKeyGenerator"/>

  1.4 在需要的地方进行注解缓存
   

2. 缓存注解


  2.1 @CacheConfig  
      它是一个类级别的注解,允许共享缓存的名称、KeyGenerator、CacheManager和CacheResolver

      value:缓存位置的一段名称,不能为空
      key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL

 
  2.2 @Cacheable  


      配置在方法或类上,作用:本方法执行后,先去缓存看有没有数据,如果没有,从数据库中查找出来

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值