Reputation: 179
I am using spring cache where my cache should be updated and deleted for a particular key.
below is my code for inserting value in cache
@Cacheable(value = CACHE_NAME, key = "")
public InputStream getFiles(String fileName, String id) {
clearCache(fileName, id);
return restTemplate.getForObject(configurationFileUrl, InputStream.class, id);
}
for deleting
@CacheEvict(value = CACHE_NAME, key = " ")
public void clearCache(String fileName, String id) {
}
Here I am trying to pass the key as SpEL with regular expression which will delete the key starting with same file name but different than latest updated key. e.g if the cache had below entry krishna1 -> obj1 now obj got modified in some other projects DB and now it is maintaining record Krishna2 for reference to modified Obj1
So once call come to my service I need to check for combination name+key in cache, if it is not there I should insert new entry(Krishna2), but now my old entry with key name Krishna1 will never be used, so How do I delete it.
I can not create Key with filename otherwise it will not able to identify if file got modified or not.
Problem Statement: suppose that I have one micro service which is maintaining the DB access and is deployed inside my domain name it Service1. The DB is containing 3 java class class1 -> for drawing rectancle, class2-> drawing circle and class3 -> for triangle. Now think you have micro service deployed outside somewhere and it is getting request to draw shape. Take the scenario that it got request to draw circle and it got config saying class2 and V1(version info) is responsible to draw circle. so it will make a rest call to service1 download class2 and compile it and cache it .class, so that next request it can serve directly if there is no change in draw circle logic(class2). The change in class2 may occur in future but rarely. Now coming to the problem next time once it got the request it will first check if it already has the class with same version if yes then draw the circle otherwise it will make rest call to get the update java file and will compile and cache it in memory. This is the reason I appended Version(id) appended to key so that it can differentiate if file is already there and changed.
Upvotes: 0
Views: 2162
Reputation: 7981
Yeah, I am with @Hanno Binder on this one; seems you may have a design problem, specifically in the way you are trying to version objects in the Cache using the key to distinguish versions.
(Disclaimer) I am not fully understanding your requirements here, but it seems you could just simplify things a bit by keeping the latest (updated) version of an object in the cache using the same key. Why do the users of the cached object care what version is in use as long as they get the latest when it is available.
In this case, it is real simple to make sure the latest version is the one cached (and returned from @Cacheable methods) without having to resort to "special" eviction criteria by using the Spring @CachePut annotation. Then, you only need to make sure that all updates are routed though the method annotated with @CachePut
. Subsequently, all @Cacheable methods called will get the latest "version".
Again, I apologize if I missed the mark on your requirements.
Upvotes: 0