intra-mart Accel Platform / Script Development Model Programming Guide

«  Lock Service   ::   Contents   ::   Execution of program created in previous version  »

Cache Service

Meaning of Cache

Cache is a function where the object can be saved using the memory on application server.
It is possible to improve the performance of the application by caching the acquired result such as database access, file access, etc.

Specifications

As a standard EHCache is used for Cache implementation.
Refer to http://ehcache.org for EHCache.

The object registered in Cache is removed when the upper limit of the number of components or size specified in the configuration file exceeds.
Moreover, the object will be removed if the validity period is exceeded.
When using Cache, it is necessary to place the xml file of any name under the <CONTEXT_PATH>/WEB-INF/conf/im-ehcache-config/folder for Cache settings.
Following is an example of configuration file.
<?xml version="1.0" encoding="UTF-8"?>
<im-ehcache-config xmlns="http://www.intra-mart.jp/cache/ehcache/config"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.intra-mart.jp/cache/ehcache/config im-ehcache-config.xsd ">

   <cache name="myCache"
         enable="true"
         max-bytes-memory="10m"
         max-elements-on-memory="100"
         overflow-to-disk="true"
         max-bytes-disk="50m"
         max-elements-on-disk="500"
         time-to-idle-seconds="600"
         time-to-live-seconds="3600" />

</im-ehcache-config>

Warning

Save it with character encoding as UTF-8.
Following are the details for each setting.
Attribute Name Description
name Sets the Cache name.
enable Specifies true or false. When specified as false, the corresponding Cache is disabled.
max-bytes-memory Specifies the maximum size for storing an object in memory. 1k, 10M, 50G, etc., can be declared.
max-elements-on-memory Specifies the maximum number of objects that can be cached in memory.
overflow-to-disk Sets whether to write in disk when upper limit of the area wherein the data is cached in memory; exceeds.
max-bytes-disk Specifies the maximum size for storing an object in disk. 1k, 10M, 50G, etc., can be declared.
max-elements-on-disk Specifies the maximum number of objects that can be cached in disk.
time-to-idle-seconds Specifies idle period (Seconds). If an object is not referred in specified time, it will be removed.
time-to-live-seconds Specifies life span (Seconds). The object is removed if the specified life span exceeds.

Programming Techniques

function getUsers() {
  // Creates cache instance. For 'myCache' attribute, it is necessary to specify the Cache name defined in im-ehcache-config.
  var cache = new Cache('myCache');
  // Attempts to acquire the information from cache. Returns that value if acquired.
  var users = cache.get('key');
  if(users != null){
    return users;
  }
  // Since the information was not present in cache, acquires the information from database.
  var database = new TenantDatabase();
  var result = database.select('SELECT user_cd FROM b_m_account_b');
  // Returns the blank array when failed to acquire the information from the database.
  // Usually log output, returning the error information are necessary. Implement appropriately as per the objective.
  if(result.error){
    return [];
  }
  // Returns after saving the acquired information in cache.
  users = [];
  for(var i = 0, length = result.data.length; i < length; i++){
    var record = result.data[i];
    users.push(record.user_cd);
  }
  // Stores in cache and returns.
  cache.put('key', users);
  return users;
}
This is the example of acquiring the data from database.

Note

Life span of cache complies with the contents mentioned in the configuration file.
When it is necessary to remove Cache explicitly, it is possible by calling Cache#remove or Cache#removeAll.

«  Lock Service   ::   Contents   ::   Execution of program created in previous version  »