Let say that object in the cache need to be evicted. But instead of eviction a new value should be calculated and put in the cache. It is important that until old value should be used until new value is generated to guarantee that there is no lock. Can Ehcache be configured for such behavior?
Let say that object in the cache need to be evicted.
A cache entry could be evicted due to capacity limits and because the cache entry wasn't requested (hit) recently. Resources need to be freed, nothing about the entry is known any more. When the respective key is requested again, the value needs to be computed again and you cannot hide the additional latency.
A cache entry could expire, e.g. because there is a time to live time span configured. Expiry can happen, while there are lots of incoming requests for that entry. Of course, in this case you may not want that the entry gets removed from the cache, you just want that the value to be refreshed.
Refreshing a value automatically when it expired is called refresh ahead or background refresh. The answer about that is here: Refreshing Caches while under load with Spring/EHCache
I am not sure what you mean by "needs to be evicted". The normal Ehcache flow would be the following:
Set an entry in the cache
Things getting this entry
The entry is now obsolete so the application set the new value
Things getting this entry (with the new value)
There is no locking involved. Anything getting the entry will directly get the old or the new value without waiting. Unless you are using a loader.
Related
We are using Redission client for java to get the data from redis but object gets deleted from collection due to TTL.
Example
We are trying the below approach to get the data from Redis with TTL.
final RList rList = client.getList(getEnvCacheKey(cacheKey));
rList.expire(7L, TimeUnit.SECONDS);
rlist.add("Value1");
rlist.add("Value2");
assertThat(rList).containsOnly("Value1", "Value2"); // This condition is true until 7 seconds
Now after 7 seconds
assert rlist.size() == 2 condition becomes false since object references are deleted due to TTL.
Due this we landed up in a production issue. Is there any way we can retain the objects even after TTL? Any sort of help will be appreciated.
The TTL(Time-To-Live) itself sets the expiration of a particular key after which the key can no longer be retrieved. If you want to keep the key in the memory you could simply skip setting rList.expire(7L, TimeUnit.SECONDS); altogether (infinite expiration).
In case you want to extend expiration, you can do so by repeating the expire command. It is also possible to remove the TTL altogether this way, although I could not tell you how to do it specifically with the Redisson.
As for the expired keys, Redis clears them 10 times a second (according to this documentation), so I don't think that you can (consistently) recover the values within the expired keys.
My general advice would be to take a step back and look at your system design. In case you are missing the expired keys, maybe this part of the product should get an extended TTL/no TTL/periodical TTL refresh
I am using com.google.appengine.api.memcache.MemcacheService to work with Google Memcache.
When storing a value, I set an expiration of 60 seconds. But the value is still returned from the cache even after several minutes.
The code for working with Memcache:
// Config
int expirationSeconds = 60;
Expiration expiration = Expiration.byDeltaSeconds(expirationSeconds);
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
// Write operation
memcache.put(id, value, expiration);
// Read operation
memcache.get(id);
I am expecting the value to be absent in this case because the Memcache documentation says that An expired item is removed when someone unsuccessfully tries to retrieve it.
Why the expired value is still returned from Memcache?
The documentation uses two words evicted and removed that could be understood to be interchangeable but they aren't:
By default, all values remain in the cache as long as possible, until evicted due to memory pressure, removed explicitly by the app, or made unavailable for another reason (such as an outage).
And in the note here we can see how removal process works:
The actual removal of expired cache data is handled lazily. An expired item is removed when someone unsuccessfully tries to retrieve it.
At the same place the eviction is explained like that:
The value is evicted no later than this time, though it can be evicted earlier for other reasons. Increment the value stored for an existing key does not update its expiration time.
Eviction is something akin to soft removal where the value is unavailable but is still in the Memcache. Removal does the actual removal.
Intro
This is regarding infinispan cache but I think this is a generic enough question.
In my infinispan cache Im inputting items into cache using putIfAbsent method and removing them with remove method. (jboss doc here)
Basic Behavior
I can put items into my cache and remove items from cache using a id. But if I do not remove the cache item explicitly, infinispan will remove it automatically after the specified life span is passed.
In my remove method I run some custom code before removal.
But when I am not invoking the remove method, infinispan remove the cache entry due to the expiry of provided life span.
In that automatic removal scenario I cannot run my custom code because it happens under the hood. (by infinispan). So what is the possible way? Following code will explain this a bit more hopefully.
I belive I am clear enough. Please give me some insight.
Thank you.
void putToCache(String id){
myCache.putIfAbsent(id,value,LIFESPAN_DURATION,timeUnit);
}
void removeFromCache(String id){
//MY CUSTOM CODE - WANT TO RUN THIS IN LIFE SPAN DURATION EXPIRY ALSO
myCache.remove(id);
}
Before going into the answer I want to clarify between eviction and expiration.
Eviction
Eviction is when an entry is removed from the in memory container due to it growing outside of the configured maximum size (max-entries). Eviction can only be enabled at configuration time and is not controlled at runtime which Expiration can be, please see here. Note that eviction never removes an entry from a store if you have one configured, thus you don't just lose an entry due to having too many if you don't want to.
Infinispan 7 and older versions have a listener for Eviction as you already found, and the event raised would be this one.
Expiration
Expiration defines an entry being removed after a period of inactivity or a set duration. This can be configured through configuration or you can override the configured value by using the API as you did your in example.
Now to answer your question, an Infinispan Expiration event was added with Infinispan 8. It is detailed more here. Note an important piece is that the expiration event will be fired holding the lock for the key that is expiring (unless your listener is defined as async). This is important to guarantee proper ordering.
Also you should keep in mind that expiration events are not guaranteed to fire exactly when the entry expires. Rather the event is only fired upon accessing said expired entry or if the expiration reaper thread finds the expired entry. The expiration reaper defaults to run every minute, you can disable this or change the time by changing the expiration interval configuration setting.
We are using a Guava LoadingCache which is build by a CacheLoader.
What we are looking for, is a Cache which will refresh its content regularly, but also expires keys after a given (longer) timeframe, if the key is not accessed anymore.
Is it possible to use .refresAfterWrite(30, TimeUnit.SECONDS) and also .expireAfterAccess(10,TimeUnit.MINUTES) on the same CacheLoader?
My experience is that the keys are never evicted because of the regular reload through refreshAfterWrite. The documentation leaves me a little uncertain about this point.
This should behave as you desired. From the CacheBuilder docs:
Currently automatic refreshes are performed when the first stale request for an entry occurs. The request triggering refresh will make a blocking call to CacheLoader.reload(K, V) and immediately return the new value if the returned future is complete, and the old value otherwise.
So if a key is queried 30 seconds after its last write, it will be refreshed; if it is not queried for 10 minutes after its last access, it will become eligible for expiration without being refreshed in the meantime.
I have a specific use case with a LoadingCache in Guava.
Expire keys that have not been accessed in 30m
As long as a key is in the cache, keep it fresh irrespective of access
I've been able to get to these semantics only through the use of some external kludge.
https://gist.github.com/kashyapp/5309855
Posting here to see if folks can give better ideas.
Problems
refreshAfterWrite() is triggered only on access
cache.refresh() -> CacheLoader.reload()
updates timers for accessed/written even if we return oldValue
returning an immediateCancelledFuture() causes ugly logging
basically no way for reload() to say that nothing changed
Solution
set expireAfterAccess on the cache
schedule a refreshJob for every key using an external executor service
refreshJob.run() checks if the cache still has the key
(asMap().containsKey()) doesn't update access times
queries upstream, and does a cache.put() only if there is a changed value
Almost there
But this is not exactly what I set out to do, close enough though. If upstream is not changing, then un-accessed keys expire away. Keys which are getting changed upstream do not get expired in the cache.