网站备案是怎么回事,电商运营自学网站,南京建设企业网站的公司,wordpress 在线音乐播放器学习的最大理由是想摆脱平庸#xff0c;早一天就多一份人生的精彩#xff1b;迟一天就多一天平庸的困扰。各位小伙伴#xff0c;如果您#xff1a; 想系统/深入学习某技术知识点… 一个人摸索学习很难坚持#xff0c;想组团高效学习… 想写博客但无从下手#xff0c;急需… 学习的最大理由是想摆脱平庸早一天就多一份人生的精彩迟一天就多一天平庸的困扰。各位小伙伴如果您 想系统/深入学习某技术知识点… 一个人摸索学习很难坚持想组团高效学习… 想写博客但无从下手急需写作干货注入能量… 热爱写作愿意让自己成为更好的人… 文章目录 一、Spring Cache是什么二、常用注解三、使用步骤1.导入依赖2.CachePut的使用3.Cacheable的使用4.CacheEvict的使用5.EnableCaching的使用 总结 一、Spring Cache是什么
Spring Cache是一个框架实现了基于注解的缓存功能只需要简单地加一个注解就能实现缓存功能。 Spring Cache提供了一层抽象底层可以切换不同的缓存实现例如
EHCacheCaffeineRedis
二、常用注解 三、使用步骤
1.导入依赖
Spring Cache缓存框架的maven导入
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId
/dependencySpring Cache缓存中间件的导入
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency总体pom文件如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.3/versionrelativePath//parentgroupIdcom.itheima/groupIdartifactIdspringcache-demo/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdscopecompile/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.76/version/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.6/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.0/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.1/version/dependencydependencygroupIdcom.github.xiaoymin/groupIdartifactIdknife4j-spring-boot-starter/artifactIdversion3.0.2/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.7.3/version/plugin/plugins/build
/project
2.CachePut的使用
CachePut将方法的返回值放到缓存中 案例代码 PostMapping//CachePut(cacheNames userCache,key #user.id)CachePut(cacheNames userCache,key #result.id)public User save(RequestBody User user){userMapper.insert(user);return user;}其中#这种写法是spring规范的。 cacheName缓存名称是个字符串 key缓存数据 如果使用Spring Cache缓存数据key的生成userCache缓存数据
3.Cacheable的使用
Cacheable在方法执行前先查询缓存中是否有数据如果有数据则直接返回缓存数据如果没有缓存数据调用方法并将方法返回值放到缓存中 案例代码 GetMappingCacheable(cacheNames userCache,key #id)//key的生成userCache::10public User getById(Long id){User user userMapper.getById(id);return user;}4.CacheEvict的使用
CacheEvict将一条或多条数据从缓存中删除 清理一条数据案例代码 DeleteMappingCacheEvict(cacheNames userCache,key#id)//key的生成userCache::10public void deleteById(Long id){userMapper.deleteById(id);}清理多条数据cacheNames定义的名字下的所有数据都删除案例代码 DeleteMapping(/delAll)CacheEvict(cacheNames userCache,allEntries true)public void deleteAll(){userMapper.deleteAll();}5.EnableCaching的使用
EnableCaching开启缓存注解功能通常加在启动类上 案例代码
SpringBootApplication
EnableTransactionManagement //开启注解方式的事务管理
Slf4j
EnableCaching//开启缓存注解功能
public class SkyApplication {public static void main(String[] args) {SpringApplication.run(SkyApplication.class, args);log.info(server started);}
}总结
以上就是Spring Cache缓存框架的相关知识点希望对你有所帮助。 积跬步以至千里积怠惰以至深渊。时代在这跟着你一起努力哦