I'm using Ehcache 3.9 and have a 24h expiration limit on the entries of the cache. I would like the expired entries to be removed at least some time after expiration (within a few hours), as there might not be any interaction with the cache for days.
The documentation is not clear about the actual removal of expired entries from the cache: https://www.ehcache.org/documentation/3.10/index.html. It says nothing about the actual removal of entries once expired and so they might still be kept in cache preventing GC. Is there a background thread periodically cleaning up expired entries from the cache? If so, how often is this check performed?
PS: With Caffeine it was needed to provide a thread to a Caffeine scheduler, which would periodically remove expired entries from the cache: https://www.programcreek.com/java-api-examples/?api=com.github.benmanes.caffeine.cache.Scheduler
EDIT: Actually https://www.ehcache.org/documentation/3.10/caching-concepts.html does mention the following:
"Ehcache can assist you with reducing the likelihood that stale data is used by your application by expiring cache entries after some amount of configured time. Once expired, the entry is automatically removed from the cache."
"Automatically" suggests that nothing additional has to be done to clean the cache, but it is unclear how often this "automatic" check is performed...
Related
Background
I am working on a spring boot application. In this application, we have two different caffeine caches - dealDetailsCache and trackingDetailsCache.
Both of these caffeine caches have single keys, let us say, X and Y respectively. We just keep updating the values for the same keys while refreshing.
Application Flow
In every 5 minutes, there is a scheduled job that runs.
This job fetches some data from an external source and upon successful retrieval, updates the 2 caffeine caches (mentioned above)
Currently, I am manually doing a put operation on each of the above caffeine caches to refresh the data for their respective keys:
put(X, <<data>>) for updating dealDetailsCache
put(Y, <<data>>) for updating trackingDetails
Expected QPS is about 50 per second.
What am I looking for
I am looking for a way to refresh the caffeine caches (just like buildAsync)
such that it does not impact the application and there should be no
downtime.
If put is not the right way to do this, then would someone please
suggest the right way to update the cache in such a way that there is
absolutely no downtime.
I read about CacheEvict, but there is a risk associated with it. It evicts and then refreshes. There could be some time between the two operations and any requests that come in during this period (after eviction and before new data is loaded) would fail.
Ultimate aim is that the requests should always find the data, even if it is old for the time being. Would someone please suggest a clean mechanism for manual cache refreshes?
I have some meta data which rarely updates, but it can update once in a while. Within my application the user access this information very frequently, which puts quite some load on my servers.
I'd like to refetch it only eg once a day to check if something changed.
My expected behavior is watchQuery should immediately return, even though cache is expired. Only if cache is expired it refetches to update cache
Is this anyhow possible?
My first idea was to tell cache-and-network or cache-first to only use network if the last network check is eg older than an day or cache got invalidated, but could not find anything on this in the document.
Thx I really appreciate your expertise.
I am trying to implement temporary ip blocking for my ASP.NET Core application and everything I have read on how to do this uses caching.
I have followed this article to learn about in-memory caching in asp.net core:
https://learn.microsoft.com/en-us/aspnet/core/performance/caching/memory
The code is running okay, but the cache is emptied immediately after an action is completed.
If a put a break-point on this line:
_cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
I can see that _cache indeed has the proper value. However, that value is gone the next time I reload the page. Regardless of how long I set the expiration time to or even if I set
CacheItemPriority.NeverRemove
in the cacheEntryOptions.
I have also tried registering a callback for the eviction of the item from the cache to see if it was being evicted immediately due to memory pressure. However, the callback is never called.
Is there some server options I need to enable to allow caching?
I met the similar issue today. And found that the cache only save the reference of the value. So if you save a List in to the cache and Clear the List later. The cached List will also be cleared, but key is still there.
Source code is here:https://source.dot.net/#Microsoft.Extensions.Caching.Abstractions/MemoryCacheExtensions.cs,bc5684b7eae179d4
I have a default cache that is fairly small and static. It contains just string keys and a string objects.
Since I won't be using anywhere near the allowed amount of memory, I'd like to just preload all of the objects into the cache on startup and have them never expire. I added a log message on start indicating that the cache was loaded.
Right now the project is still in development so the cache isn't being hit often (other than by web spiders/crawlers/scripts). The problem I'm seeing is that every hour to few hours, I'm seeing the log message that my cache was loaded. I'd expect it to load once and then not reload until I force it to.
Is there any way to keep the cache "alive" so that it doesn't have to frequently reload? Is it like an IIS worker process that dies out after some amount of inactivity?
FYI I have the cache configured for Expiry Policy: Never, Time: 0min, Eviction: Disabled. Also the way I check if the cache is still alive is that on load I add a special object to the cache. Then I check to see if that object exists and if it doesn't I assume the cache needs to be reloaded.
For anyone else who stumbles across this I ended up creating a scheduled task that hit the cache every 5 minutes. Since then, I haven't seemed to have any issues with it reloading. Not sure if this is the best answer, but it worked for me.
we are using Appfabric cache in our project, and we ran into 2 major problems.
First - we are using named caches (no explicitly created regions). One of them, created as Expirable=false, Eviction=none, TTL=525600 is used for objects that should be always available (populated at application start, via the Put method). But from time to time (i couldnt identify exact timespan, nor connection to certain actions in application) all object in this cache suddenly expires - i can see this from performance counters - object count for this cache goes to 0, total expired objects counter increases of the amount of objects in this cache at the same time. Am i missing some other settings ? I tried both inserting them via Put() without timespan, and Put with timespan "a year". Still expires after several minutes...
The second problem - when I tried to solve first problem, i decided to use ETW trace logging feature to see in log, what is happening. I have created tracelog via logman and started it, waited for cache to expire, stopped the log, and have used tracerpt to create dumpfile from etl. Everything ok so far. But this dumpfile is useless, because there are no readable data, only 4400690073007400720....... After some quick research, i figured out, that I need to supply an PDB or TMF file to tracerpt, so it can "decode" binaryeventdata to readable eventdata. Is it possible to get some of these for appfabric cache ? Or there is some other way to use ETW with appfabric to get some usefull readable log ?
I found out what the problem is when your cache is expiring nearly instantly.
If your memory is low the cache gets cleared.
Check in the eventvwr -> Applicatin and Service -> Microsoft -> Windows - Application Server System Services and select Operational.
Look for warnings like:
Service available memory low - Cache private bytes percent {2} Cache working set percent {1} Cache data size percent {0} Available memory percent {21} CLR Generation2 count {2013} Released memory percent {0}.
There is an explanation in here to convert log file to cvs: http://msdn.microsoft.com/en-us/library/ff921010.aspx
But I can not even use tracelog tool.