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

com.github.bingoohuang.westcache.manager.GuavaCacheManager Maven / Gradle / Ivy

package com.github.bingoohuang.westcache.manager;

import com.github.bingoohuang.westcache.base.WestCache;
import com.github.bingoohuang.westcache.base.WestCacheItem;
import com.github.bingoohuang.westcache.utils.Guavas;
import com.github.bingoohuang.westcache.utils.WestCacheOption;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.Callable;

/**
 * @author bingoohuang [[email protected]] Created on 2016/12/23.
 */
public class GuavaCacheManager extends BaseCacheManager {
    public GuavaCacheManager() {
        super(new ExpireAfterWritableWestCacheAdaptor(new GuavaWestCache()));
    }

    public static class GuavaWestCache implements WestCache {
        private Cache cache
                = CacheBuilder.newBuilder().build();

        @Override
        public WestCacheItem get(WestCacheOption option,
                                 String cacheKey,
                                 Callable callable) {
            return Guavas.cacheGet(cache, cacheKey, callable);
        }

        @Override
        public WestCacheItem getIfPresent(WestCacheOption option,
                                          String cacheKey) {
            return cache.getIfPresent(cacheKey);
        }

        @Override
        public void put(WestCacheOption option,
                        String cacheKey,
                        WestCacheItem cacheValue) {
            cache.put(cacheKey, cacheValue);
        }

        @Override
        public void invalidate(WestCacheOption option,
                               String cacheKey, String version) {
            cache.invalidate(cacheKey);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy