Used Google Guava Cache to Spring

因工作需求有用到快取機制,但可能因 Multiple Cache Container 相互衝突關係 (與 EhCache ),造成無法使用 Google Guava Cache 整合 Spring,所以記錄在此方便以後使用:

以下為 Java Config 方式:

  1. Add dependency to Maven pom.xml file:
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    
  2. Create CacheConfig file:
    @EnableCaching
    @Configuration
    public class CacheConfig implements CachingConfigurer {
    
        @Bean
        public CacheManager cacheManager() {
            SimpleCacheManager cacheManager = new SimpleCacheManager();
            GuavaCache findAllTypeCache = new GuavaCache(CacheConstant.FIND_ALL_TYPE, getDefaultCacheBuilder().build());
            GuavaCache findOneTypeCache = new GuavaCache(CacheConstant.FIND_ONE_TYPE, getDefaultCacheBuilder().build());
            cacheManager.setCaches(Arrays.asList(findAllTypeCache, findOneTypeCache));
            return cacheManager;
        }
    
        @Override
        public KeyGenerator keyGenerator() {
            return new SimpleKeyGenerator();
        }
    
        public CacheBuilder getDefaultCacheBuilder() {
            return CacheBuilder.newBuilder()
                    .maximumSize(10000L)
                    .expireAfterWrite(30L, TimeUnit.MINUTES);
        }
    
    }
    
  3. Add @Cacheable, @CacheEvict annotation to method to be cached:
    @Service
    public class TypeManager extends AbstractManager<Type, Long> {
    
        private final Comparator<Type> comparator = (t1, t2) -> t1.getSort().compareTo(t2.getSort());
    
        @Autowired
        private TypeRepository repository;
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, key = "#type.sid"),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public Type save(Type type) {
            return super.save(type);
        }
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, key = "#type.sid"),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public Type saveAndFlush(Type type) {
            return super.saveAndFlush(type);
        }
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, allEntries = true),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public List<Type> save(List<Type> tList) {
            return super.save(tList);
        }
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, key = "#type.sid"),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public Type save(Type type, User loginUser) {
            return super.save(type, loginUser);
        }
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, key = "#type.sid"),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public Type saveAndFlush(Type type, User loginUser) {
            return super.saveAndFlush(type, loginUser);
        }
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, allEntries = true),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public List<Type> save(List<Type> types, User loginUser) {
            return super.save(types, loginUser);
        }
    
        @Caching(evict = {
                @CacheEvict(value = CacheConstant.FIND_ONE_TYPE, key = "#sid"),
                @CacheEvict(value = CacheConstant.FIND_ALL_TYPE, allEntries = true)
        })
        @Override
        public void cacheEvict(Long sid) {
            super.cacheEvict(sid);
        }
    
        @Cacheable(CacheConstant.FIND_ONE_TYPE)
        @Override
        public Type getBySid(Long sid) {
            return super.getBySid(sid);
        }
    
        @Cacheable(CacheConstant.FIND_ALL_TYPE)
        @Override
        public List<Type> getList() {
            System.err.println("get read data!");
            return super.getList();
        }
        ...
    }
    
  4. enjoy~~~

 


參考資料:

spring-4-guava-cache-integration-example-with-guavacachemanager-and-guavacache
add-guava-cache-to-spring-boot-to-cache
spring-31-caching-and-0
CacheEvict example

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料