- 一、缓存是什么?
- 二、Simple。
- 三、Ehcache。
- 四、Redis。
一、缓存是什么?
缓存是一种介于数据永久存储介质与数据应用之间的数据临时存储介质
使用缓存可以有效的减少低速数据读取过程的次数(例如磁盘IO),提高系统性能
缓存不仅可以用于提高永久性存储介质的数据读取效率,还可以提供临时的数据存储空间
导入缓存技术对应的starter
org.springframework.boot spring-boot-starter-cache
启用缓存
@SpringBootApplication
@EnableCaching
public class Springboot19CacheApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot19CacheApplication.class, args);
}
}
设置当前操作的结果数据进入缓存
@Cacheable (value="cacheSpace" ,key="#id")
public Book getById( Integer id) {
return bookDao.selectById(id);
}
三、Ehcache。
加入Ehcache坐标
net.sf.ehcache ehcache
缓存设定为使用Ehcache
spring: cache: type: ehcache ehcache: config: ehcache.xml
提供ehcache配置文件ehcache.xml
四、Redis。
加入redis坐标
org.springframework.boot spring-boot-starter-data-redis
设置redis相关配置
spring: redis: host: localhost port: 6379 cache : type: redis redis: use-key-prefix: true #是否使用前缀名(系统定义前缀名) key-prefix: sms_ #追加自定义前缀名 time-to-live: 10s #有效时长 cache-null-values: false #是否允许存储空值



