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

win.doyto.query.cache.CacheWrapper Maven / Gradle / Ivy

There is a newer version: 0.2.2.1-RELEASE
Show newest version
package win.doyto.query.cache;

import org.springframework.cache.Cache;

/**
 * CacheWrapper
 *
 * @author f0rb
 */
public interface CacheWrapper {
    @SuppressWarnings("unchecked")
    static  V invoke(Cache cache, Object key, Invocable invocable) {
        if (cache == null || key == null) {
            return invocable.invoke();
        }
        Cache.ValueWrapper valueWrapper = cache.get(key);
        if (valueWrapper != null) {
            return (V) valueWrapper.get();
        }
        V value = invocable.invoke();
        cache.put(key, value);
        return value;
    }

    static  DefaultCacheWrapper createInstance() {
        return new DefaultCacheWrapper<>();
    }

    default T execute(Object key, Invocable invocable) {
        return CacheWrapper.invoke(getCache(), key, invocable);
    }

    void setCache(Cache cache);

    Cache getCache();

    default void evict(Object key) {
        getCache().evict(key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy