缓存专家的缓存问题。
语境
我们已经使用 OpenSymphony 的 OsCache 多年,并考虑转向更好/更强/更快/积极开发的缓存产品。
问题
我们使用了 OsCache 的“组条目”功能,在其他地方没有找到。
简而言之,OsCache 允许您在“条目插入时间”指定一个或多个组。稍后您可以使“一组条目”无效,而无需知道每个条目的键。
OsCache 示例
以下是使用此机制的示例代码:
Object[] groups = {"mammal", "Northern Hemisphere", "cloven-feet"}
myCache.put(myKey, myValue , groups );
// later you can flush all 'mammal' entries
myCache.flushGroup("mammal")
// or flush all 'cloven-foot'
myCache.flushGroup("cloven-foot")
替代方案:匹配器机制
我们使用另一个由前团队成员编写的本地缓存,它使用“键匹配器”模式来使条目无效
在这种方法中,您将定义您的“键”和匹配器类,如下所示:
public class AnimalKey
{
String fRegion;
String fPhylum;
String fFootType;
..getters and setters go here
}
匹配器:
public class RegionMatcher implements ICacheKeyMatcher
{
String fRegion;
public RegionMatcher(String pRegion)
{
fRegion=pRegion;
}
public boolean isMatch(Obect pKey)
{
boolean bMatch=false;
if (pKey instanceof AnimalKey)
{
AnimalKey key = (AninmalKey) pKey);
bMatch=(fRegion.equals(key.getRegion());
}
}
}
用法:
myCache.put(new AnimalKey("North America","mammal", "chews-the-cud");
//remove all entries for 'north america'
IKeyMatcher myMatcher= new AnimalKeyMatcher("North America");
myCache.removeMatching(myMatcher);
这种机制实现简单,但有一个性能缺点:它必须遍历每个条目才能使组无效。(尽管它仍然比通过数据库旋转要快)。
问题
- (警告:这可能听起来很愚蠢)你怎么称呼这个功能?OsCache 将其称为“缓存组”。JbossCache 和 EhCache 似乎既没有定义也没有实现它。领域?地区?王国?
- 这种“缓存组/区域”范式是否存在标准模式?
- 后起之秀的缓存产品(如ehcache、coherence、jbosscache)如何处理这个问题
- 这个范例不在 jcache 规范中,对吧?(JSR-107)
- 你如何处理“大规模失效”?缓存很棒,直到它们变得陈旧。允许您使大范围无效的 API 是一个很大的帮助。(例如,管理员想要按下按钮并清除所有缓存的帖子条目,例如,特定论坛)
谢谢
将要