How to Use and Configure Caching

Caching can improve performance of the project when some of the data is not updated frequently. Caching can also minimize network load by limiting calls to the backend.

The Caching option is defined on the project level, i.e., enable caching or do not enable. However, if you choose to enable caching, you will be able to define which data to cache and which data should be retrieved (or written) to the legacy backend.

You can select the Caching Engine to be used through the dropdown. Ehcache is provided with the OpenLegacy application and is used as the default engine, but you can use any caching engine by defining and configuring it outside the OpenLegacy application.

Please note that enabling caching requires system resources, even if no data is cached. Therefore, if you are certain that no data caching is required for your project, do not enable caching.

Enable Caching

When creating an SDK or an API project using the Create New Project wizard, you are given the option to enable caching or not.

  1. To enable caching, check the checkbox
  2. From the Caching Backend dropdown list, select the Caching Engine to be used. In this example, we have three options available
  3. Click Finish



Caching Definitions

A central cache can be shared by both APIs and the SDK.

@OLCacheable notes that an entity is to be picked by the Cache mechanism. More control is available using the @OLCache/@OLCacheEvict attributes.

  • cacheName - Holds the cache name. By default it will be empty. For entities, the default cache name is the full name of the entity class, and for APIs the default is a combination of service class full name and the method name separated by `-`.

@OLCache - this annotation can be written above the Service Method annotation or within the action with the attributes below.

  • cacheName - as above (will override @OLCacheable if both are defined.
  • timeToLive - holds the lifespan of the data in milisec, after which the data is evicted from the cache. If no time is defined, the data will remain in cache indefinitely.





When using the caching annotations on the SDK, the annotations will be placed on the entities @Action annotation.
When defining an action that saves the response in cache and fetches the response from cache when it is performed, the @Action definition is set as defined in the example to the right.

@Action(action = EXECUTE.class, path = "ITEMDET", displayName = "Execute", alias = "execute", olCache= @OLCache(...))

When defining an action that evicts the cache when it is performed, the definition can be set as defined in the follow example to the right.

@Action(action = UPDATE.class, displayName = "UPDATE", alias = "update", path = "ITEMDET",
        olCache = @OLCache(timeToLive = 1000), olCacheEvict = @OLCacheEvict(evicts = {@CacheEvict()}))

Full example of cache configured on an entity:

@RpcEntity(name="CacheItemde", language=Languages.COBOL)
@OLCacheable(cacheName = "shared")
@RpcEntity(name = "CacheItemde", language = Languages.COBOL)
@RpcActions(actions = {
        @Action(action = UPDATE.class, displayName = "UPDATE", alias = "update", path = "ITEMDET",
                olCache = @OLCache(timeToLive = 1000), olCacheEvict = @OLCacheEvict(evicts = {@CacheEvict()})),
        @Action(action = DELETE.class, displayName = "DELETE", alias = "update", path = "ITEMDET",
                olCacheEvict = @OLCacheEvict(evicts = {@CacheEvict(allEntries = true)})),
        @Action(action = EXECUTE.class, displayName = "Execute", alias = "execute", path = "ITEMDET",
                olCache = @OLCache(timeToLive = 1000))})

Note, that for Screens projects we support the caching on the entity, while ignoring the navigation actions to it.  This option doesn't support eviction.

@OLCacheable
@ScreenEntity(olCache = @OLCache(timeToLive = 1000))

Example using these annotations on Service Methods:

Save & Fetch response from cache example:

@OLCache(...)
@ServiceMethod(name = "getGetItemDe")
public GetItemDeOut getGetItemDe(GetItemDeIn getItemDeIn) throws Exception {
	...
}

Evict existing cache example:

@OLCacheEvict(evicts = @CacheEvict(cacheName = "test", allEntries = true))
public void updateAndEvict() {
}

Cache Eviction (Clear Cache)

The behavior of Cleaning is similar to inserting, just replacing the attribute cache with the attribute cacheEvict and the annotations respectively.

@OLCacheEvict 

  • evicts - list of @CacheEvict
    • cacheName - list of cache names to evict.
    • allEntries - when this attribute is True, all entries will be evicted from cache. default: false

See example to the right:





@Action(action = EXECUTE.class, path = "ITEMDET", displayName = "Execute", alias = "execute", olCacheEvict= @OLCacheEvict(...))