Java:多个自刷新缓存对象

时间:2014-01-31 09:46:40

标签: java caching factory-pattern

我想为工厂创建一个缓存对象,用于多个表。每个缓存都有自己的生命周期,这些生命周期彼此不同。例如,City对象的生命周期约为一年,设施列表对象的生命周期约为1天。

以下是我的缓存类

public abstract class CacheAbstract<E, I, C> {

private Long timestamp;
private Class<E> entityClass;
private Class<I> idClass;
private Class<C> cacheClass;
protected Integer TTL = 60; // In minutes. defaults to 60 mins

private Map<I, C> cache;
private Timer timer;

public CacheAbstract(Class<E> entityClass, Class<I> idClass, Class<C> cacheClass) {
    this.entityClass = entityClass;
    this.idClass = idClass;
    this.cacheClass = cacheClass;
}

public Boolean isExpired() {
    return (this.timestamp + TTL * 60000) < new Date().getTime();
}

private void verifyCache() {
    if (this.cache == null || isExpired()) {
        this.load();
    }
}

private void onTimer() {
    this.timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            throw new UnsupportedOperationException("Not supported yet."); //What should I do here? I can't call this class' load(), can I?;
        }
    }, this.TTL * 60000);
}


public void load() {
    // Load all data here
    this.timestamp = new Date().getTime();
}

public C get(I id) {
    this.verifyCache();
    return this.cache.get(id);
}

public List<C> getAll() {
    this.verifyCache();
    List<C> list = new ArrayList<>(this.cache.values());
    return list;
}

protected abstract C transform(E entity);

protected abstract I getID(E entity);

public abstract String getName();
}

这个类不是单例,并且将由工厂实例化。比方说,CacheFactory

public final class CacheFactory {

private Map<String, CacheAbstract> caches;

private CacheFactory() {
}

public void registerCache(CacheAbstract cache) {
    if (caches == null) {
        caches = new HashMap<>();
    }
    if (caches.containsKey(cache.getName())) {
        this.caches.get(cache.getName()).load();
    } else {
        this.caches.put(cache.getName(), cache);
    }
}

public CacheAbstract getCache(String name) throws ExceptionInvalidCacheName {
    if (caches.containsKey(name)) {
        return caches.get(name);
    } else {
        throw new ExceptionInvalidCacheName("KOLOM SERVER [cache]: Cache '" + name + "' not registered");
    }
}

public Boolean isExist(String name) {
    return caches.containsKey(name);
}

public List<String> getNames() {
    return new ArrayList<>(caches.keySet());
}

public static CacheFactory getInstance() {
    return CacheFactoryHolder.INSTANCE;
}

private static class CacheFactoryHolder {

    private static final CacheFactory INSTANCE = new CacheFactory();
}
}

现在,我如何实现算法让CacheAbstract在需要时刷新自己?

1 个答案:

答案 0 :(得分:0)

是的,您只需致电load()

private void onTimer() {
    timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
             load(); // Call the containing class method
        }
    }, TTL * 60000);
}

因为每个匿名类实际上也是一个内部类,所以它引用了声明它的包含类。

但是,如果你真的想要活动到期,你只能使用一个Timer。由于在verifyCache()的每次调用中呼叫get(),您已经延迟到期。也许这足以满足您的需求。