I've previously implemented caching in my application, to be used with three separate get methods. These get methods are getAllProfiles()
, getProfilesByID()
, and getProfileByFields()
. Because of this, my code looks like this:
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);
}
To streamline my work, however, I need to make one cache that is created at start-up using getAllProfiles()
, for the whole table. All three methods will then use this one cache to work with.
I think I can just reuse the code for loadingCache2 to create the cache in the first place:
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);
}
}
);
and pass in null for the size, so the SQL statement on the DAO will be 'SELECT * FROM Profiles'. The issue will come from the other methods; I have no idea how to point these methods to this cache given the differing input requirements. Has anyone done anything like this previously?
Edit:
As suggested by Louis Wasserman, I'm making a single Cache object that takes Object as a generic key. From there, the service should use the if statement to detect the input object type and use the oppropriate method to retrieve the contents of the cache, depending on the method used.
As of right now, though, it fails on getAllProfiles, with a null pointer exception, so I need to figure that out.
Based on the code below, does it look like I'm on the right track? I'm using Cache as opposed to LoadingCache for this object:
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);
}