2013-10-31 45 views
0

减少样板我有几十个像PersonDao的数据访问对象有相似的方法:方式使用番石榴缓存

Person findById(String id) {} 
List<Person> search(String firstName, LastName, Page) {} 
int searchCount(String firstName, LastName) {} 

我已通过添加番石榴缓存,这些类的一个尝试,它是非常好的,但有很多样板。

这里做findById看在缓存第一个例子:

private final LoadingCache<String, Person> cacheById = CacheBuilder.newBuilder() 
    .maximumSize(maxItemsInCache) 
    .expireAfterWrite(cacheExpireAfterMinutes, TimeUnit.MINUTES) 
    .build(new CacheLoader<String, Person>() { 
    public Person load(String key) { 
     return findByIdNoCache(key); 
    }); 
//.... and update findById to call the cache ... 
@Override 
public Person findById(String id) { 
    return cacheById.getUnchecked(id); 
} 

如此,因为每种方法都有不同的参数和返回类型,我结束了创建一个单独的CacheLoader每个方法!

我尝试将所有内容整合到一个单一的CacheLoader中,该单元返回Object类型并接受一个Map对象,但最后我得到了很大的难看的if/else来找出调用哪个方法来加载缓存。

我正在努力寻找一种优雅的方式来为这些数据访问对象添加缓存,有什么建议吗?也许番石榴缓存不适合这种用例?

回答

4

试试这个。不幸的是,由于泛型,有编译器警告......但是我们可能会禁止它们,因为我们知道什么都不会发生。

public class CacheContainer { 

    private static final long maxItemsInCache = 42; 
    private static final long cacheExpireAfterMinutes = 42; 
    private final Map<String, LoadingCache> caches = Maps.newHashMap(); 


    public <K, V> V getFromCache(String cacheId, K key, CacheLoader<K, V> loader) throws ExecutionException { 
     LoadingCache<K, V> cache = caches.get(cacheId); 
     if (cache == null) { 
      cache = CacheBuilder.newBuilder(). 
        maximumSize(maxItemsInCache). 
        expireAfterWrite(cacheExpireAfterMinutes, TimeUnit.MINUTES). 
        build(loader); 
      caches.put(cacheId, cache); 
     } 
     return cache.get(key); 
    } 
} 

然后在你的道:

private final CacheContainer cacheContainer = new CacheContainer(); 


public Person findById(String id) { 
    cacheContainer.getFromCache("personById", id, new CacheLoader<String, Person>() { 
     @Override 
     public Person load(String key) { 
      return findByIdNoCache(key); 
     }); 
} 

以同样的方式的其他方法。我认为你不能再减少样板。

+0

漂亮!谢谢。 – Upgradingdave

+0

很高兴我能帮忙:) – siledh

1

为每个想要缓存结果的方法创建一个CacheLoader(和单独的缓存)是必要的。您可以通过创建一个带有所需缓存配置的单个CacheBuilder来简化一些事情,然后创建如下所示的每个缓存:

private final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder() 
    .maximumSize(maxItemsInCache) 
    .expireAfterWrite(cacheExpireAfterMinutes, TimeUnit.MINUTES); 

private final LoadingCache<String, Person> cacheById = builder.build(
    new CacheLoader<String, Person>() { 
     // ... 
    }); 

private final LoadingCache<Search, List<Person>> searchCache = builder.build(
    new CacheLoader<Search, List<Person>>() { 
     // ... 
    }); 

    // etc.