Using DynaCache APIs to operate the cache
HCL Commerce extensions can interface with the cache by using the DistributedMap interface. The interface provides
methods for operations such as put, get, clear
and
invalidate
.
Obtaining a reference to the cache
object:
InitialContext ctx = new InitialContext();
DistributedMap myCustomCache = (DistributedMap)
ctx.lookup(“services/cache/MyCustomCache”);
Once a reference to the cache object is obtained, the put() methods
can be used to add to the cache. Depending on the cache configuration, the method will
update the local and remote caches.
myCustomCache.put("cacheId", myCacheEntryObject )
final int priority = 1;
// the time in seconds that the cache entry should remain in the cache. The default value is -1 and means the entry does not time out.
final int timeToLive = 1800;
// the time in seconds that the cache entry should remain in the cache if not accessed.
// Inactivity is supported by the local cache but not by the remote cache
final int inactivityTime = 900
// Not supported
final int sharingPolicy = 0;
final String [] dependencyIds = new String [] {"dependencyId1", "dependencyId2"};
myCustomCache.put("cacheId", myCacheEntryObject, priority, timeToLive, inactivityTime, sharingPolicy, dependencyIds );
Use the get() interface to read an object back from the cache. If
local and remote caching is enabled, the HCL Cache will first attempt
to load the object from the local cache, and then from the remote cache only if the
specified cache ID was not found in the local
cache.
Object cachedObject = myCustomCache.get(“cacheKey”);
The clear() method empties the cache. The
invalidate() method removes using a dependency ID or a cache ID.
myCustomCache.clear();
…
myCustomCache.invalidate(“dependencyId1”);
Entries are removed both from the local cache, and from the remote cache. If local caching is used, invalidation messages are sent to remove entries from local caches in other containers.