Search in sources :

Example 1 with ResourcePoolsBuilder

use of org.ehcache.config.builders.ResourcePoolsBuilder in project sagacity-sqltoy by chenrenfei.

the class TranslateEhcacheManager method put.

@Override
public void put(TranslateConfigModel cacheConfig, String cacheName, String cacheKey, HashMap<String, Object[]> cacheValue) {
    if (cacheManager == null) {
        return;
    }
    synchronized (cacheName) {
        Cache<String, HashMap> cache = cacheManager.getCache(cacheName, String.class, HashMap.class);
        // 缓存没有配置,自动创建缓存(不建议使用)
        if (cache == null) {
            ResourcePoolsBuilder resBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder();
            // 堆内内存大小(20000条)
            resBuilder = resBuilder.heap((cacheConfig.getHeap() < 1) ? 1000 : cacheConfig.getHeap(), EntryUnit.ENTRIES);
            if (cacheConfig.getOffHeap() > 0) {
                resBuilder = resBuilder.offheap(cacheConfig.getOffHeap(), MemoryUnit.MB);
            }
            if (cacheConfig.getDiskSize() > 0) {
                resBuilder = resBuilder.disk(cacheConfig.getDiskSize(), MemoryUnit.MB, true);
            }
            cache = cacheManager.createCache(cacheName, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, HashMap.class, resBuilder).withExpiry(cacheConfig.getKeepAlive() > 0 ? ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(cacheConfig.getKeepAlive())) : ExpiryPolicyBuilder.noExpiration()).build());
        }
        // 清除缓存(一般不会执行,即缓存值被设置为null表示清除缓存)
        if (cacheValue == null) {
            if (StringUtil.isBlank(cacheKey)) {
                cache.clear();
            } else {
                cache.remove(cacheKey);
            }
        } else // 更新缓存
        {
            cache.put(StringUtil.isBlank(cacheKey) ? cacheName : cacheKey, cacheValue);
        }
    }
}
Also used : HashMap(java.util.HashMap) ResourcePoolsBuilder(org.ehcache.config.builders.ResourcePoolsBuilder)

Aggregations

HashMap (java.util.HashMap)1 ResourcePoolsBuilder (org.ehcache.config.builders.ResourcePoolsBuilder)1