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

cn.fantasticmao.mundo.data.support.MemcacheCacheBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package cn.fantasticmao.mundo.data.support;

import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.rubyeye.xmemcached.MemcachedClient;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * MemcacheCacheBuilder
 *
 * 
 * LoadingCache<Integer, String> cache = MemcacheCacheBuilder.<Integer, String>newBuilder(memcachedClient)
 *     .expireTime(1, TimeUnit.HOURS)
 *     .keyConvert(String::valueOf)
 *     .build(new CacheLoader<Integer, String>() {
 *         public String load(@Nonnull Integer key) throws Exception {
 *             // do something ...
 *             return null;
 *         }
 *      });
 * 
* * @author fantasticmao * @version 1.0 * @since 2018/7/22 */ public class MemcacheCacheBuilder { private MemcachedClient memcachedClient; private Function keyConvert; private int expireAfterWriteSeconds; private MemcacheCacheBuilder(MemcachedClient memcachedClient) { this.memcachedClient = memcachedClient; this.keyConvert = String::valueOf; this.expireAfterWriteSeconds = Math.toIntExact(TimeUnit.DAYS.toSeconds(30)); } public static MemcacheCacheBuilder newBuilder(MemcachedClient memcachedClient) { return new MemcacheCacheBuilder<>(memcachedClient); } public MemcacheCacheBuilder keyConvert(Function keyConvert) { this.keyConvert = keyConvert; return this; } public MemcacheCacheBuilder expireTime(long duration, TimeUnit unit) { long time = unit.toSeconds(duration); this.expireAfterWriteSeconds = Math.toIntExact(time); return this; } public LoadingCache build(CacheLoader cacheLoader) { return new MemcacheLoadingCache<>(memcachedClient, cacheLoader, keyConvert, expireAfterWriteSeconds); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy