Sping cache 資料(一。Spring4.1新特性——Spring快取框架增強)
Spring 4.1提供了對jcache的支援,並對cache抽象部分進行了一些簡單的增強。在整合jcache時是非常費勁的,版本之間各種不相容,不建議用於正式環境,在正式環境中可以使用如Guava Cache或Ehcache。
jcache依賴:
Java程式碼- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-jcache</artifactId>
- <version>${ehcache-jcache.version}</version>
- </dependency>
- <dependency>
- <groupId>javax.cache</groupId>
- <artifactId>cache-api</artifactId>
- <version>${javax.cache.version}</version>
- </dependency>
- <dependency>
- <groupId>org.jsr107.ri</groupId>
- <artifactId>cache-ri-impl</artifactId>
- <version>${cache-ri-impl.version}</version>
- </dependency>
<javax.cache.version>1.0.0</javax.cache.version>、<cache-ri-impl.version>1.0.0</cache-ri-impl.version>、<ehcache-jcache.version>1.2</ehcache-jcache.version>,具體請參考原始碼。
1、Jcache整合
建立Cache:
Java程式碼- javax.cache.CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
- MutableConfiguration<Object, Object> mutableConfiguration = new MutableConfiguration<Object, Object>();
- mutableConfiguration.setStoreByValue(false); // otherwise value has to be Serializable
- cacheManager.createCache("user", mutableConfiguration);
- cacheManager.createCache("user2", mutableConfiguration);
- cacheManager.createCache("user3", mutableConfiguration);
- JCacheCacheManager jCacheCacheManager = new JCacheCacheManager(cacheManager);
- return jCacheCacheManager;
Java Config方式提供了CachingConfigurer用於提供配置回撥:
Java程式碼- @Configuration
- @ComponentScan(basePackages = "com.sishuok.spring.service")
- @EnableCaching(proxyTargetClass = true)
- public class AppConfig implements CachingConfigurer {
- @Bean
- @Override
- public CacheManager cacheManager() {
- javax.cache.CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
- MutableConfiguration<Object, Object> mutableConfiguration = new MutableConfiguration<Object, Object>();
- mutableConfiguration.setStoreByValue(false); // otherwise value has to be Serializable
- cacheManager.createCache("user", mutableConfiguration);
- cacheManager.createCache("user2", mutableConfiguration);
- cacheManager.createCache("user3", mutableConfiguration);
- JCacheCacheManager jCacheCacheManager = new JCacheCacheManager(cacheManager);
- return jCacheCacheManager;
- }
- @Bean
- @Override
- public CacheResolver cacheResolver() {
- return new MyCacheResolver();
- }
- @Bean
- @Override
- public KeyGenerator keyGenerator() {
- return new SimpleKeyGenerator();
- }
- @Override
- public CacheErrorHandler errorHandler() {
- return new CacheErrorHandler() {
- @Override
- public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
- System.out.println("cache get error");
- }
- @Override
- public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
- System.out.println("cache put error");
- }
- @Override
- public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
- System.out.println("cache evict error");
- }
- @Override
- public void handleCacheClearError(RuntimeException exception, Cache cache) {
- System.out.println("cache clear error");
- }
- };
- }
- }
2、@CacheConfig指定全域性Cache配置
Spring 4.1之前需要每個方法上都指定:
Java程式碼- @Service
- public class UserService {
- Set<User> users = new HashSet<User>();
- @CachePut(value = "user", key = "#user.id")
- public User save(User user) {
- users.add(user);
- return user;
- }
- @CachePut(value = "user", key = "#user.id")
- public User update(User user) {
- users.remove(user);
- users.add(user);
- return user;
- }
- @CacheEvict(value = "user", key = "#user.id")
- public User delete(User user) {
- users.remove(user);
- return user;
- }
- @CacheEvict(value = "user", allEntries = true)
- public void deleteAll() {
- users.clear();
- }
- @Cacheable(value = "user", key = "#id")
- public User findById(final Long id) {
- System.out.println("cache miss, invoke find by id, id:" + id);
- for (User user : users) {
- if (user.getId().equals(id)) {
- return user;
- }
- }
- return null;
- }
- }
Spring 4.1時可以直接在類級別使用@CacheConfig指定:
Java程式碼- @Service
- @CacheConfig(cacheNames = {"user", "user2"})
- public class UserService {
- Set<User> users = new HashSet<User>();
- @CachePut(key = "#user.id")
- public User save(User user) {
- users.add(user);
- return user;
- }
- @CachePut(key = "#user.id")
- public User update(User user) {
- users.remove(user);
- users.add(user);
- return user;
- }
- @CacheEvict(key = "#user.id")
- public User delete(User user) {
- users.remove(user);
- return user;
- }
- @CacheEvict(allEntries = true)
- public void deleteAll() {
- users.clear();
- }
- @Cacheable(key = "#id")
- public User findById(final Long id) {
- System.out.println("cache miss, invoke find by id, id:" + id);
- for (User user : users) {
- if (user.getId().equals(id)) {
- return user;
- }
- }
- return null;
- }
- }
3、CacheResolver
其名字已經暗示了其是Cache解析器,用於根據實際情況來動態解析使用哪個Cache,如:
Java程式碼- public class MyCacheResolver implements CacheResolver {
- @Autowired
- private CacheManager cacheManager;
- @Override
- public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
- List<Cache> caches = new ArrayList<Cache>();
- for(String cacheName : context.getOperation().getCacheNames()) {
- caches.add(cacheManager.getCache(cacheName));
- }
- if(context.getTarget() instanceof UserService2) {
- caches.add(cacheManager.getCache("user2"));
- caches.add(cacheManager.getCache("user3"));
- }
- return caches;
- }
- }
context中存放了當前cache的操作型別、目標物件、目標方法、引數資訊,這樣我們可以根據這些資訊來決定使用那些cache; context.getOperation().getCacheNames()得到當前目標物件/目標方法上配置的cache Name;然後我們可以在此基礎上新增額外的cache。
此處需要注意的是即使配置了CacheResolver,也必須在@CacheConfig或方法上的如@CachePut上指定至少一個Cache Name。
4、CacheErrorHandler
用於捕獲從Cache中進行CRUD時的異常的回撥處理器。