java - Guava - 缓存表并在该缓存上使用 get 方法
问题描述
我之前在我的应用程序中实现了缓存,与三个单独的 get 方法一起使用。这些 get 方法是getAllProfiles()
、getProfilesByID()
和getProfileByFields()
。因此,我的代码如下所示:
private LoadingCache<int[], List<Profile>> loadingCache = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<int[] ids, List<Profile>>() {
@Override
public List load(int[] ids) throws Exception {
return profileDAO.getProfilesById(ids);
}
}
);
private LoadingCache<Integer, List<Profile>> loadingCache2 = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<Integer, List<Profile>>() {
@Override
public List<Profile> load(Integer size) throws Exception {
return profileDAO.getAllProfiles(size);
}
}
);
private LoadingCache<Profile, List<Profile>> loadingCache3 = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<Profile, List<Profile>>() {
@Override
public List<Profile> load(Profile profile) throws Exception {
return profileDAO.getProfileByFields(profile);
}
}
);
public ProfileManagerImpl(ProfileDAO profileDAO) {
this.profileDAO = profileDAO;
}
public List<Profile> getAllProfiles(Integer size) throws Exception {
return loadingCache2.get(size);
}
public List<Profile> getProfilesById(int[] idArray) throws Exception {
return loadingCache.get(idArray);
}
public List<Profile> getProfileByFields(Profile profile) throws Exception {
return loadingCache3.get(profile);
}
然而,为了简化我的工作,我需要getAllProfiles()
为整个表创建一个在启动时使用 , 创建的缓存。然后,所有三种方法都将使用这个缓存来处理。
我想我可以重用 loadCache2 的代码来首先创建缓存:
private LoadingCache<Integer, List<Profile>> loadingCache2 = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<Integer, List<Profile>>() {
@Override
public List<Profile> load(Integer size) throws Exception {
return profileDAO.getAllProfiles(size);
}
}
);
并传入 null 作为大小,因此 DAO 上的 SQL 语句将是“SELECT * FROM Profiles”。问题将来自其他方法;鉴于不同的输入要求,我不知道如何将这些方法指向此缓存。以前有没有人做过这样的事情?
编辑:
正如 Louis Wasserman 所建议的,我正在制作一个将 Object 作为通用键的 Cache 对象。从那里,服务应该使用 if 语句来检测输入对象类型并使用适当的方法来检索缓存的内容,具体取决于所使用的方法。
不过,到目前为止,它在 getAllProfiles 上失败了,出现了一个空指针异常,所以我需要弄清楚这一点。
根据下面的代码,看起来我走在正确的轨道上吗?对于这个对象,我使用的是 Cache 而不是 LoadingCache:
public Cache<Object, List<Profile>> cache =
CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(new CacheLoader<Object, List<Profile>>() {
@Override
public List<Profile> load(Object k) throws Exception {
if (k instanceof Integer) {
return profileDAO.getAllProfiles((Integer) k);
}
else if (k instanceof int[]) {
return profileDAO.getMultipleProfiles((int[]) k);
}
else if (k instanceof Profile)
return profileDAO.getProfileByFields((Profile) k);
}
});
public List<Profile> getAllProfiles(Integer size) throws Exception {
return cache.getIfPresent(size);
}
解决方案
我认为缓存概念存在一些误用。缓存会加载,只有当它没有找到元素时,所以通过创建一个缓存,谁的键是一个列表,你正在使缓存概念无效(因为列表是一个对象,除非你传递相同的列表,否则你将永远不会重用值因此总是从数据库加载而不使用缓存)。我认为您只需要使用一个缓存,使用 id 作为键(如@Ben Manes 建议的那样),并用辅助方法包装它(此代码未经测试,因此请小心尝试)。
/** This is whatever class you intended to put the cache in anyways */
public class CacheWrapper {
private LoadingCache<Integer, Profile> loadingCache;
public CacheWrapper(/* whatever arguments the class needs*/) {
loadingCache = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(100).build(
new CacheLoader<Integer, Profile>() {
@Override
public List<Profile> load(Integer id) throws Exception {
return profileDAO.getProfileById(id);
}
}
);
// init the cache here
for (Profile profile : profileDAO.getAllProfiles(null)) {
loadingCache.put(profile.getId(), profile);
}
/* other construction logic here */
}
public List<Profile> getAllProfiles() throws Exception {
return new ArrayList(loadingCache.asMap().values());
}
public List<Profile> getProfilesById(int[] idArray) throws Exception {
List<Profile> profiles = new ArrayList(idArray.length());
for(int i = 0; i < idArray.length(); i++) {
profiles.add(loadingCache.get(idArray[i]));
}
return profiles;
}
public List<Profile> getProfileByFields(Profile profile) throws Exception {
List<Profile> profiles = new ArrayList(idArray.length());
for (Profile cachedProfile : loadingCache.asMap().values()) {
if (profile.hasEqualFields(cachedProfile)) {
profiles.add(cachedProfile);
}
}
return profiles;
}
}
您还可以通过使用 as 键构建第二个缓存Profile
并将匹配配置文件列表作为值来进一步优化这一点,然后您可以用getProfileByFields
与您所做的类似的方法替换 - 使用Profiles
数据库中的类似字段加载缓存。最后,请注意像这样从缓存加载会产生别名,因此您需要小心处理数据。
希望这很清楚,无论如何都有帮助。
推荐阅读
- txt - 我想一次将字符添加到txt文档中所有行的开头和结尾
- drools - jBPM 登录失败:未授权:所有预定义用户都不起作用
- reactjs - 使用 CRSF 和自定义标头在 React 中重定向
- kendo-ui - 向 Kendo Grid 指定列中的所有单元格添加更改事件
- java - 如何使webview返回上一页并且不显示about:空白?
- screen-scraping - 当我使用 selenium webdriver.Chrome().close() 时,它会切换到之前的窗口吗?
- c++ - Eigen:我需要定义一个具有定义大小的矩阵 - 不是动态(不是堆)而是固定大小(在堆栈上)
- flutter - 颤动的TexteditingController在ios上不起作用
- java - 如何在 firebase 数据库中隐藏类似数据?
- python - 在字符串中查找多个子字符串而不对其进行多次迭代