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

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: REDIS12_Spring Cache概述、@Cacheable、@CacheEvict、@Caching、@CachePut的使用(三)
  • ③. 测试得到的结果:


  1. 如果缓存中有,方法不再调用


  1. key是默认自动生成的,包含缓存名字::SimpleKey(自动生成的key值)


  1. 缓存的value的值。默认使用jdk序列化机制,将序列化后的数据存到redis


  1. 默认过期时间是 -1


微信图片_20220109191814.png


image.png


④. 指定自己的key,并设置过期时间


//application.properties
spring.cache.type=redis
#spring.cache.cache-names=
#设置存活时间,ms为单位
spring.cache.redis.time-to-live=3600000


/**
自定义
(1). 指定生成缓存的key key属性指定,接收一个spl表达式
详细文档:https://docs.spring.io/spring-framework/docs/5.2.16.RELEASE/spring-framework-reference/integration.html#cache-spel-context
(2). 指定缓存的数据存活时间(在配置文件中修改了ttl)
*/
@Cacheable(value={"category"},key = "'Level1Categorys'")
@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;
}


image.png


⑤. 如果使用#root.method.name


  @Cacheable(value={"category"},key = "#root.method.name")


微信图片_20220109191920.png

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
3月前
|
存储 缓存 Java
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
594 2
|
6月前
|
NoSQL 网络协议 Redis
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
126 1
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
|
6月前
|
NoSQL Redis 容器
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
|
6月前
|
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')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
|
6月前
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
|
6月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
|
6月前
|
缓存 开发框架 NoSQL
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
|
6月前
|
缓存 NoSQL Unix
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
|
6月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 是否记录具体读/写(Get/Set)或删除(Del)了哪些key呢?
【Azure Redis 缓存】Azure Cache for Redis 是否记录具体读/写(Get/Set)或删除(Del)了哪些key呢?
|
6月前
|
存储 缓存 NoSQL
【Azure Redis 缓存】Azure Cache for Redis 专用终结点, 虚拟网络, 公网访问链路
【Azure Redis 缓存】Azure Cache for Redis 专用终结点, 虚拟网络, 公网访问链路