REDIS12_Spring Cache概述、@Cacheable、@CacheEvict、@Caching、@CachePut的使用(二)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: REDIS12_Spring Cache概述、@Cacheable、@CacheEvict、@Caching、@CachePut的使用(二)

②. Spring cache入门案列@Cacheable


  • ①. 导入pom文件,在主启动类上添加@EnableCaching


<!--引入redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--cache-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>   


@EnableCaching
@SpringBootApplication
public class testSpringCache{
    public static void main(String[] args) {
        SpringApplication.run(testSpringCache.class, args);
    }
}


//application.properties
spring.cache.type=redis
#spring.cache.cache-names=


②. 业务类


 /**
  * @Cacheable
  *(代表当前方法的结果需要缓存,如果缓存中有,方法不用调用
  *如果缓存中没有,会调用方法,最后将方法的结果放入到缓存)
  *1、每一个需要缓存的数据我们都来指定要放到那个名字的缓存(缓存的分区,按照业务类型分)
  *2、默认行为
  *   (1). 如果缓存中有,方法不再调用
  *   (2). key是默认自动生成的,包含缓存名字::SimpleKey(自动生成的key值)
  *   (3). 缓存的value的值。默认使用jdk序列化机制,将序列化后的数据存到redis
  *   (4). 默认过期时间是 -1
  */
@Cacheable(value={"category"})
@Override
public List<CategoryEntity> getLevel1Category() {
    long l= System.currentTimeMillis();
    QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_cid",0);
    List<CategoryEntity> entities = baseMapper.selectList(wrapper);
    log.info("消耗时间:"+(System.currentTimeMillis()-l));
    return entities;
}
相关文章
|
4月前
|
存储 缓存 NoSQL
Spring Cache缓存框架
Spring Cache是Spring体系下的标准化缓存框架,支持多种缓存(如Redis、EhCache、Caffeine),可独立或组合使用。其优势包括平滑迁移、注解与编程两种使用方式,以及高度解耦和灵活管理。通过动态代理实现缓存操作,适用于不同业务场景。
416 0
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
211 2
|
NoSQL Unix 网络安全
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
151 0
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
|
缓存 弹性计算 NoSQL
【Azure Redis 缓存 Azure Cache For Redis】Redis连接池
【Azure Redis 缓存 Azure Cache For Redis】Redis连接池
128 0
|
存储 缓存 Java
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
3384 2
|
NoSQL 网络协议 Redis
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
296 1
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
|
NoSQL Redis 容器
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
123 3
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
118 0
|
缓存 开发框架 NoSQL
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
142 0
|
缓存 NoSQL Unix
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
114 0