2026/1/10 1:51:49
网站建设
项目流程
网站开发工具论文,中国建设银行网站开通短信,织梦网站首页怎么修改,工程造价定额在哪查目录一、Spring Cache是什么1、核心优势2、基本使用1. 添加依赖2. 启用缓存二、核心注解详解1、EnableCaching2、Cacheable3、CachePut4、CacheEvict一、Spring Cache是什么
Spring Cache 是 Spring 框架提供的缓存抽象层#xff0c;让你可以轻松地在应用程序中添加缓存功能EnableCaching2、Cacheable3、CachePut4、CacheEvict一、Spring Cache是什么Spring Cache是Spring框架提供的缓存抽象层让你可以轻松地在应用程序中添加缓存功能而无需关心底层缓存实现细节。Spring Cache 是一个框架实现了基于注解的缓存功能只需要简单地加一个注解就能实现缓存功能。1、核心优势特点说明声明式缓存使用注解即可实现缓存功能抽象层设计支持多种缓存实现Redis、EhCache、Guava等与 Spring 集成完美集成 Spring 生态方法级缓存基于方法返回值进行缓存缓存策略灵活支持条件缓存、同步刷新等2、基本使用1. 添加依赖!-- Spring Boot Starter Cache --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency!-- Redis 作为缓存实现 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency2. 启用缓存SpringBootApplicationEnableCaching// 启用缓存支持publicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}二、核心注解详解1、EnableCachingEnableCaching是Spring 框架中的一个注解用于启用 Spring 的缓存支持。当你在配置类上添加此注解时Spring 会创建一个缓存相关的切面以拦截带有缓存注解的方法调用。主要作用启用缓存机制在 Spring 应用中激活缓存功能自动配置根据项目依赖自动配置合适的CacheManager注解驱动支持使用Cacheable,CacheEvict,CachePut等注解ConfigurationEnableCachingpublicclassCacheConfig{// Spring Boot 会自动配置合适的 CacheManager// 手动配置示例BeanpublicCacheManagercacheManager(){returnnewConcurrentMapCacheManager(users,products);}}2、CacheableCacheable是Spring 缓存中最核心的注解用于标记方法的返回结果应该被缓存。解释在方法执行前先查询缓存中是否有数据如果有数据则直接返回缓存数据如果没有缓存数据调用方法并将方法返回值放到缓存中GetMappingCacheable(valueuserCache,key#id)publicUsergetById(Longid){UseruseruserMapper.getById(id);returnuser;}Cacheable(value userCache,key #id)中value的属性是存在redis中的名称存到redis的时候会创建value::的key在redis中。Cacheable(value userCache,key #id)中的key是和value代码作为::后面的参数。我们查看源码的时候可以看到如下的情况key可以用多种情况展示参数名称当有多个参数的时候可以使用#p0#p1单一参数的时候可以使用#root参数名称等多种情况。数据验证Debug运行项目查看项目的执行流程。使用Swagger进行代码调试http://localhost:8888/doc.html#/home当数据没有存到redis时候会直接查询数据库如果存到redis之后会直接从redis中查找并且不经过代码直接返回。redis没有存储信息接口会直接进入到代码当中返回结果redis中的结果现在redis中已经有了key值我们再执行以下逻辑根据耗时可以看出直接从redis中获取了3、CachePutCachePut注解用于更新缓存它总是会执行方法体并将返回结果放入缓存中。与Cacheable不同的是无论缓存中是否已存在都会执行方法并更新缓存。参数详解参数说明示例value/cacheNames缓存名称userskey缓存键#user.id,#result.idkeyGenerator键生成器customKeyGeneratorcacheManager缓存管理器redisCacheManagercacheResolver缓存解析器customResolvercondition执行条件#user ! nullunless排除条件#result null参数使用方法PostMappingCachePut(valueuserCache,key#user.id)publicUsersave(RequestBodyUseruser){userMapper.insert(user);returnuser;}CachePut(value “userCache”,key “#user.id”)可以根据user参数进行解析出来。数据验证接口方法如下接口调试方法如下方法进入接口中结果如下4、CacheEvictCacheEvict用于清除缓存中的数据。当数据发生变化删除、更新时需要清除旧缓存以保证数据一致性。参数特征参数说明默认值示例value/cacheNames缓存名称-userskey要清除的缓存键空#id,#user.idallEntries是否清除所有条目falsetruebeforeInvocation调用前还是调用后清除falsetruecondition清除条件空#id ! nullkeyGenerator键生成器空customKeyGeneratorcacheManager缓存管理器空redisCacheManagercacheResolver缓存解析器空customResolver接口方法1CacheEvict(valueuserCache,key#id)DeleteMappingpublicvoiddeleteById(Longid){userMapper.deleteById(id);}接口方法2CacheEvict(valueuserCache,allEntriestrue)DeleteMapping(/delAll)publicvoiddeleteAll(){userMapper.deleteAll();},key #id是删除单个keyallEntriestrue的时候是删除所有的key运行结果