一、关于Spring Cache

缓存在现在的应用中越来越重要,

Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术,并支持使用JCache(JSR-107)注解简化我们开发。

通过SpringCache,可以快速嵌入自己的Cache实现,主要是@Cacheable、@CachePut、@CacheEvict、@CacheConfig、@Caching等注解来实现。

  • @Cacheable:作用于方法上,用于对于方法返回结果进行缓存,如果已经存在该缓存,则直接从缓存中获取,缓存的key可以从入参中指定,缓存的value为方法返回值。
  • @CachePut:作用于方法上,无论是否存在该缓存,每次都会重新添加缓存,缓存的key可以从入参中指定,缓存的value为方法返回值,常用作于更新。
  • @CacheEvict:作用于方法上,用于清除缓存。
  • @CacheConfig:作用在类上,统一配置本类的缓存注解的属性。
  • @Caching:作用于方法上,用于一次性设置多个缓存。
  • @EnableCaching:作用于类上,用于开启注解功能。

二、演示示例

欲使用Spring Cache,需要先引入Spring Cache的依赖。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cache依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

然后在启动类上,我们需要使用@EnableCaching来声明开启缓存。

@EnableCaching //开启缓存
@SpringBootApplication
public class SpringbootApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
} }

这样就可以使用注解来操作缓存了,创建CacheService类,其中dataMap的Map存储数据,省去了数据库的操作。

@Slf4j
@Service
public class CacheService { private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
{
for (int i = 1; i < 100 ; i++) {
User u = new User("code" + i, "name" + i);
put(i, u);
}
}
}; // 获取数据
@Cacheable(value = "cache", key = "'user:' + #id")
public User get(int id){
log.info("通过id{}查询获取", id);
return dataMap.get(id);
} // 更新数据
@CachePut(value = "cache", key = "'user:' + #id")
public User set(int id, User u){
log.info("更新id{}数据", id);
dataMap.put(id, u);
return u;
} //删除数据
@CacheEvict(value = "cache", key = "'user:' + #id")
public User del(int id){
log.info("删除id{}数据", id);
dataMap.remove(id);
return u;
} }

get方法模拟查询,@Cacheable用于添加缓存,set方法用于修改,@CachePut更新缓存,del方法用于删除数据, @CacheEvict删除缓存。需要注意的是,注解的value表示缓存分类,并不是指缓存的对象值。

然后在创建CacheApi,用于调用CacheService进行测试。

@RestController
@RequestMapping("cache")
public class CacheApi { @Autowired
private CacheService cacheService; @GetMapping("get")
public User get(@RequestParam int id){
return cacheService.get(id);
} @PostMapping("set")
public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
User u = new User(code, name);
return cacheService.set(id, u);
} @DeleteMapping("del")
public void del(@RequestParam int id){
cacheService.del(id);
} }

然后我们打开swagger-ui界面(http://localhost:10900/swagger-ui.html)进行测试,多次调用查询,可以看到, CacheService的get方法,对于同一id仅仅执行一遍。然后再调用更新,再次get时,即可发现数据已经更新,而调用del,则可以清除缓存,再次查询又会调用方法。

源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

最新文章

  1. JavaScript:数组大全
  2. Android 隐式意图的配置
  3. java编译错误 程序包javax.servlet不存在javax.servlet.*
  4. iOS中JS 与OC的交互(JavaScriptCore.framework)
  5. 朝花夕拾-android 获取当前手机的内存卡状态和网络连接状态
  6. Metasploit是一款开源的安全漏洞检测工具,
  7. LightOJ1119 Pimp My Ride(状压DP)
  8. Linux下添加磁盘创建lvm分区
  9. 3. QT窗体间值的传递(续)
  10. 二十七、oracle 例外
  11. python xml sendEmail
  12. SourceTree 实现 git flow 流程
  13. NOSQL schema创建原则
  14. 【Alpha】Scrum Meeting 9
  15. 剑指Offer 3. 从尾到头打印链表 (链表)
  16. 在Docker环境下部署Kafka
  17. 使用Powershell实现计算机名称及IP地址修改
  18. InnoDB存储引擎介绍-(2)redo和undo学习
  19. 移动端开发时默认样式reset
  20. innodb_locks_unsafe_for_binlog分析

热门文章

  1. 巨杉Tech | Hbase迁移至SequoiaDB 实战
  2. 排序入门练习题3 谁考了第k名 题解
  3. DirectX12 3D 游戏开发与实战第四章内容(下)
  4. django配置静态文件的两种方法
  5. JVM的一些工具的简要使用
  6. Python虚拟环境管理工具virtualenvwrapper安装及配置
  7. OAuth2.0摘要
  8. Integer对象大小比较问题
  9. Hadoop入门 之 Hadoop的安装
  10. javascript数组/对象数组的深浅拷贝问题