How to fetch all the elements in a MemoryStore of an existing cache [EHCache] - ehcache

I'm new to EHCache. I have implemented EHCache for my application. I have configured all the cache configuration's. I found that in my cache's MemoryStore there are certain number of elements existing in it. I would like to fetch them all for further manipulation. Could anyone help me out as to how to access elements in MemoryStore of my existing cache.
Thank You in Advance.

Here is a snippet to get you started:
cacheKeys = CacheInfo.getCacheKeys();
cacheKeys.forEach((String k) -> {
});

There is no public API to access elements of a specific store in Ehcache. The whole point of the store system is to improve read performance, in a transparent fashion.

Related

What is the formal difference between a Cache and Map in version 5.0 of Hazelcast?

While implementing Hazelcast for the first time in set of web APIs, the usage of Map and Cache is inconsistent.
For example, creating a cache using SpringCacheManager results in the creation of a map
var sCache = springCacheManager.getCache("testCache");
sCache.putIfAbsent("test", "test2");
However, creating a cache using the CachingProvider CacheManager results in the creation of an actual cache that must be opened and closed (as per the documentation)
try (var cache = Caching.getCachingProvider().getCacheManager(null, null,
HazelcastCachingProvider.propertiesByInstanceName("hazelcache")).createCache("actualCache", config)) {
cache.putIfAbsent("test", "test");
}
Another example, using the #Cacheable annotation will create a map, even though the documentation outlines the usage of a Cache. The following code will successfully return the first computed value using a Map in hazelcast. A cache is never used.
#Cacheable(value = "counter")
public Boolean test(Integer addTo) {
counter += addTo;
return counter % 2 != 0;
}
Is there a formal definition within Hazelcast of a cache vs a map? Are both usable for the same purpose?
The image below contains a view into a test Hazelcast Management Center that shows the above components, namely the maps and caches. These are all generated by the same client.
test
There are Cache, Spring Cache and Map to consider here.
For Cache, Hazelcast is an implementation provider for the Java caching standard, JSR107.
These show as "Cache" on the Management Center, and if you run hazelcastInstance.getDistributedObjects() they'll be of type ICache. It's in the Hazelcast documentation here.
For Map, Hazelcast provides a data structure IMap which is mostly a superset of java.util.Map. These show as "Map" on the Management Center.
Spring also provides caching, and you can set CacheType for JSR107 or directly with Hazelcast, or allow Spring to pick. When Spring uses Hazelcast directly, it will use IMap not ICache for storage.
If you pick JCache or configure Spring to use JCache, then you get standards compliant behaviour. You have caching, and can easily swap caching provider from Hazelcast to something else should you want to.
Map gives you operations such as executeOnKey to update one or more entries in situ. If the entry is a compound object but on a small part is changing, this can be a more efficient way to update it than sending the whole value.

springboot + infinispan invalidation mode : How can used with shared cache storage?

I have a springboot project used infinispan to run the invalidation mode under the cluster for cache.
The questtion is about infinispan.
In fact, I read the official document: "In invalidation, the caches on different nodes do not actually share any data" and now I am in this situation.
I use the method a provided: Cache.putForExternalRead(key, value) and this method can solve the problem that when I puts the data into the cache of the Node A, the B node invalidates it, But I can't use the springboot annotations, such as #Cacheable.
I also read "Invalidation mode can be used with a shared cache store." from document but I don't know how to do this and I hope you can provide some help.
The goal I hope to achieve is that in the invalidation mode, I put a data into the cache of Node A, Node B will accept a copy data from A.Can I do this with invalidation mode ?
I try use invalidation mode with opening CLusterLoader but there is a risk of getting old value when node get data from other nodes.
I use replicated mode now. However, "replication practically only performs well in small clusters(under 10 nodes)" and "Asynchronous replication is not recommended".So I just can use synchronous replication.
Which performance will be better for invalidation and synchronous replication ?
Looking forward to your help. Thanks
Spring annotations won't fully support INVALIDATION mode unless you use a ClusterLoader. Under the hood annotations use put, we might consider adding a feature to support putForExternalRead behavior in the future, but it won't be there very soon.
Annotations work well with LOCAL, REPL and DIST modes.
ConfigurationBuilder b = new ConfigurationBuilder();
b.persistence()
.addClusterLoader()
.remoteCallTimeout(500);
If you are afraid about getting stale values and not being performant enough with a replicated cache, you might consider using a distributed cache.

How to flush all the cache entries in simple spring memcached

This question is with reference to Simple Spring memcached.
I have a scenario where a list of deals are cached for user using the userId as the key. Now in case a deal data is updated I need to flush the cache for all users since this would affect deals data for all the users.
How can I achieve this with SSM annotations. The invalidate*cache and update*cache options seem to invalidate/update key specific cache entries whereas I need to clear the entire cache.
Currently it's impossible in plain SSM to flush entire cache using annotations, if you require such option please create a feature request on: https://code.google.com/p/simple-spring-memcached/issues/list
There is another way to flush entire cache by using SSM with Spring Cache as describer here: https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started#Spring_3.1_Cache_Integration.
Just change allowClear to 'true' and use #CacheEvict(value = YOUR_CACHE_NAME, allEntries = true)

Flushing entire cache at once with Enterprise Caching Block

I am looking into using Enterprise Caching Block for my .NET 3.5 service to cache a bunch of static data from the database.
From everything I have read, it seems that FileDependency is the best option for storing static data that does not expire too often. However, when the file changes and the cache is flushed, I need to get a callback once to do some post processing for that particular cache. If I implement ICacheItemRefreshAction and register it during adding an item to the cache, I get a callback for each one of them.
Is there a way to register a callback for the entire cache so that I dont see thousands of callbacks being invoked when the cache flushes?
Thanks
To address your follow up for a better way than FileDependency: you could wrap a SqlDependency in an ICacheItemExpiration. See SqlCacheDependency with the Caching Application Block for sample code.
That approach would only work with SQL Server and would require setting up Service Broker.
In terms of a cache level callback, I don't see an out of the box way to achieve that; almost everything is geared to the item level. What you could do would be to create your own CacheManager Implementation that features a cache level callback.
Another approach might be to have a ICacheItemRefreshAction that only performs any operations when the cache is empty (i.e. the last item has been removed).

Save items in IIS application cache

In my C# web application I need to save some items in the application cache and then retrieve those items by key. To save those items I'm using the following code:
HttpContext.Current.Items["urlname"] = "something";
That saves the item fine in the Items hashtable. However I'm not able to get the value of "urlname" in another location in the application.
Could anybody tell me what I'm doing wrong or maybe there's a better way of doing this?
Thanks.
[EDIT] Ok, now I know that the Items hashtable is a storage with very short lifetime. The contents will be deleted once the response is sent to the browser. So, how can I save my data in other way?
HttpContext.Current.Items is a per request store.
You should use HttpContext.Current.Session if you want to store and access values across multiple pages.
Have a read of this - https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx
Other thing to consider is something like memcached - CouchBase have a Windows implementation

Resources