本文共 7801 字,大约阅读时间需要 26 分钟。
缓存方式 | 对应的 CacheManager | 描述 |
---|---|---|
Collection | SimpleCacheManager | 使用简单的 Collection 来存储缓存,主要用于测试 |
ConcurrentMap | ConcurrentMapCacheManager | 使用 ConcurrentMap 来存储缓存 |
无 | NoOpCacheManager | 仅测试用途,不会实际存储缓存 |
EhCache | EhCacheCacheManager | 使用 EhCache 做为缓存技术 |
GuavaCache | GuavaCacheManager | 使用 Google Guava 的 GuavaCache 作为缓存技术 |
Hazelcast | HazelcastCacheManager | 使用 Hazelcast 作为缓存技术 |
JCache | JCacheCacheManager | 支持 JCache(JSR-107)标准的实现作为缓存技术,如 Apache Commons JCS |
Redis | RedisCacheManager | 使用 Redis 作为缓存技术 |
注解 | 作用 |
---|---|
@Cacheable | 在执行注解了该注解的方法前,查看缓存中是否有目标数据,有则直接返回数据,否则调用方法并将方法的返回值放入缓存中 |
@CachePut | 将方法的返回值放入缓存中;该注解的属性要与 @Cacheable 注解的属性保持一致 |
@CacheEvict | 将一条或多条数据从缓存中删除 |
@Caching | 组合多个注解策略在一个方法上 |
spring.cache.type= #可选 generic、ehcache、hazelcast、infinispan、jcache、redis、guava、simple、nonespring.cache.cache-name= #程序启动时创建缓存名称spring.cache.ehcache.config= # ehcache 配置文件的地址spring.cache.hazelcast.config= # 同上spring.cache.infinispan.config= #同上spring.cache.jcache.config= #同上spring.cache.jcache.provider= # 当多个 jcache 实现在类路径的时候,指定 jcache 实现spring.cache.guava.spec= # guava specs
4.0.0 org.springframework.boot spring-boot-starter-parent 1.3.0.M2 com.pyc mycache 0.0.1-SNAPSHOT mycache jar Demo project for Spring Boot 1.8 com.oracle ojdbc6 11.2.0.2.0 org.springframework.boot spring-boot-starter-cache org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
package com.pyc.mycache.service;import com.pyc.mycache.domain.Person;public interface DemoService { public Person save(Person person); public void remove(Long id); public Person findOne(Person person);}
package com.pyc.mycache.service.impl;import com.pyc.mycache.dao.PersonRepository;import com.pyc.mycache.domain.Person;import com.pyc.mycache.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Servicepublic class DemoServiceImpl implements DemoService { @Autowired PersonRepository personRepository; @Override // 缓存新增的或更新的数据到缓存,缓存名称为 people,数据的 key 时 person 的 id // Caching new or updated data to the cache, //the cache name is people, and the key of the data is the person's id @CachePut(value = "people", key = "#person.id") public Person save(Person person) { Person p = personRepository.save(person); System.out.println("为id,key 为:" + p.getId() + "的数据做了缓存"); return p; } @Override // 从缓存 people 中删除 key 为 id 的数据 // delete the data from cache called people where key value equal of the parameter id @CacheEvict(value = "people") public void remove(Long id) { System.out.println("删除了id, key 为" + id + "的数据缓存"); personRepository.delete(id); } @Override // 缓存 key 为 person.id 的数据到缓存 people 中 // Caching the data that key is parameter person's id into cache called people @Cacheable(value = "people", key = "#person.id") public Person findOne(Person person) { Person p = personRepository.findOne(person.getId()); System.out.println("为 id,key 为" + p.getId() + "的数据做了缓存"); return p; }}
package com.pyc.mycache.web;import com.pyc.mycache.domain.Person;import com.pyc.mycache.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic 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"; }}
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublic class MycacheApplication { public static void main(String[] args) { SpringApplication.run(MycacheApplication.class, args); }}
net.sf.ehcache ehcache
转载地址:http://ulqgn.baihongyu.com/