All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lontime.extredisson.cache.ExtLocalCacheView Maven / Gradle / Ivy

The newest version!
package com.github.lontime.extredisson.cache;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import com.github.lontime.shaded.com.github.benmanes.caffeine.cache.Caffeine;
import com.github.lontime.shaded.org.redisson.RedissonObject;
import com.github.lontime.shaded.org.redisson.api.LocalCachedMapOptions;
import com.github.lontime.shaded.org.redisson.cache.CacheKey;
import com.github.lontime.shaded.org.redisson.cache.CacheValue;
import com.github.lontime.shaded.org.redisson.cache.LFUCacheMap;
import com.github.lontime.shaded.org.redisson.cache.LRUCacheMap;
import com.github.lontime.shaded.org.redisson.cache.LocalCacheView;
import com.github.lontime.shaded.org.redisson.cache.NoneCacheMap;
import com.github.lontime.shaded.org.redisson.cache.ReferenceCacheMap;

/**
 * 
 * @author Nikita Koksharov
 *
 * @param  key type
 * @param  value type
 */
public class ExtLocalCacheView extends LocalCacheView {

    public ExtLocalCacheView(LocalCachedMapOptions options, RedissonObject object) {
        super(options, object);
    }

    @Override
    public ConcurrentMap createCache(LocalCachedMapOptions options) {
        if (options.getCacheProvider() == LocalCachedMapOptions.CacheProvider.CAFFEINE) {
            Caffeine caffeineBuilder = Caffeine.newBuilder();
            if (options.getTimeToLiveInMillis() > 0) {
                caffeineBuilder.expireAfterWrite(options.getTimeToLiveInMillis(), TimeUnit.MILLISECONDS);
            }
            if (options.getMaxIdleInMillis() > 0) {
                caffeineBuilder.expireAfterAccess(options.getMaxIdleInMillis(), TimeUnit.MILLISECONDS);
            }
            if (options.getCacheSize() > 0) {
                caffeineBuilder.maximumSize(options.getCacheSize());
            }
            if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.SOFT) {
                caffeineBuilder.softValues();
            }
            if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.WEAK) {
                caffeineBuilder.weakValues();
            }
            return caffeineBuilder.build().asMap();
        }

        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.NONE) {
            return new NoneCacheMap<>(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.LRU) {
            return new LRUCacheMap<>(options.getCacheSize(), options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.LFU) {
            return new LFUCacheMap<>(options.getCacheSize(), options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.SOFT) {
            return ReferenceCacheMap.soft(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.WEAK) {
            return ReferenceCacheMap.weak(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        throw new IllegalArgumentException("Invalid eviction policy: " + options.getEvictionPolicy());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy