@Data
public class Person {
private Long id;
private String name;
}
@EnableCaching
spring:
cache:
type: simple #所以 无需配置也行
@RestController
public class CacheController {
@Autowired
DemoService demoService;
//第一个方法,放入缓存,任何时候都放入
@RequestMapping("/put")
public Person put(Person person) {
return demoService.save(person);
}
//第二个是查询,首次放入,第二次访问,直接用 缓存,返回。
@RequestMapping("/able")
public Person cacheable(Person person) {
return demoService.findOne(person);
}
//清理
@RequestMapping("/evit")
public String evit(Long id) {
demoService.remove(id);
return "ok";
}
}
@Service
public class DemoService {
//缓存名称随便起,数据的key是 person.id
@CachePut(value = "person", key = "#person.id")
public Person save(Person person) {
System.out.println("为id、key为:" + person.getId() + "数据做了缓存");
return person;
}
//第二个方法是:如果首次访问没缓存,就放入 缓存。有,就返回缓存。
@Cacheable(value = "person", key = "#person.id")
public Person findOne(Person person) {
System.out.println("为id、key为:" + person.getId() + "数据做了缓存");
return person;
}
@CacheEvict(value = "person")
public void remove(Long id) {
System.out.println("删除了id、key为" + id + "的数据缓存");
}
}
http://localhost:8080/put?id=1&name=张三
为id、key为:1数据做了缓存
http://localhost:8080/able?id=1
http://localhost:8080/able?id=2&name=李四
http://localhost:8080/able?id=2&name=李四
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-redisartifactId>
<version>1.3.0.M2version>
dependency>
spring:
redis:
host: 192.168.44.146
port: 6379
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.44.146
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000
Person implements Serializable{} //继承序列化,否则 redis 应该会报错吧
@Cacheable(value = "person", key = "#person.id",unless="#result == null")
http://localhost:8080/put?id=2&name=李四
http://localhost:8080/able?id=2 //上个链接放入了 缓存到redis,可以查到。这个连接调用,不会走service方法。
@Cacheable
@CachePut
@CacheEvit
@Caching
组合多个注解策略 在一个方法上
默认使用 SimpleCacheConfiguration
新版 好像都删除了
CacheManager
cache.Cache
@SpringBootApplication
@EnableCaching //开启 声明式 缓存
public class Ch85Application {
public static void main(String[] args) {
new SimpleCacheManager();
new ConcurrentMapCacheManager();
new NoOpCacheManager();
new EhCacheCacheManager();
new GuavaCacheManager();
new HazelcastCacheManager();
new JCacheCacheManager();
new RedisCacheManager();
SpringApplication.run(Ch85Application.class, args);
}
}
@Configuration
@EnableCaching //开启缓存,有用。
public class AppConfig {
@Bean
public EhCacheCacheManager cacheManager() {
return new EhCacheCacheManager();
}
}
spring:
cache:
type: redis #simple
cache-names: myCache #程序启动时候,缓存名称
ehcache:
config: ehcache.xml #配置文件的路径
hazelcast:
config: ehcache.xml
infinispan:
config: ehcache.xml
jcache:
config: ehcache.xml
provider: #xxx ,当有多个jcache实现在路径中的时候,指定jcache实现
guava:
spec: #guava specs