Notice: Use of undefined constant ‘file - assumed '‘file' in /home/jpalio/vhosts/jpaliowikien.torn.com.pl/LocalSettings.php on line 154

Notice: Use of undefined constant bi’ - assumed 'bi’' in /home/jpalio/vhosts/jpaliowikien.torn.com.pl/LocalSettings.php on line 154
Module:Cache - Jpaliowiki EN

Module:Cache

From Jpaliowiki EN

Jump to: navigation, search

Contents

Introduction

The Cache module is dedicated to buffer frequently used data into random-access memory. Usually buffering applies to some dictionary data from database. There are 2 types of buffers defined:

  • SIMPLE - stores all the data till their removal or till buffer clearing
  • VOLUME - stores the specified amount of data, when the limit is exceeded then the earliest element used is automatically removed

User can create his own buffers or use the generally available buffer of type SIMPLE without a name.

Creating buffers

The following methods are used to create and apply buffers

  • Boolean isCacheCreated(String cacheName) - checks whether the buffer named cacheName already exists
  • void createCache(String cacheName, String type, Object[] cacheParams) - creates the buffer of type type with parameters cacheParams named cacheName; the type parameter accept one of the following values: SIMPLE, VOLUME; cacheParams for the buffer of type SIMPLE is an empty array, and for the buffer of type VOLUME it is singleton array, which contains the number of elements that can be stored within the buffer
  • Boolean containsKey(String cacheName, Object key) - checks whether the buffer named cacheName contains an object with the key key
  • void put(String cacheName, Object key, Object value) - stores an element with the key key and value value into the buffer named cacheName
  • void putAll(String cacheName, Map map) - stores all the elements from the map map into the buffer named cacheName
  • void remove(String cacheName, Object key) - removes an element with the key key from the buffer named cacheName

It is recommended to create buffers in synchronized manner.

Example

$executeSynchronized({
  $if((Boolean)$not((Boolean)$cache.isCacheCreated("MIMETypes")), {
    $// Create buffer if not exists.
    $cache.createCache("MIMETypes", "SIMPLE", [])
    $// Store all MIME types data into the buffer.
    $for(MIMEType, (LinkedList)$sql.read("select id, name from p_mime_types"), {
      $cache.put("MIMETypes", $MIMEType[1], $MIMEType[0])
    })
  })
})

After jPALIO restart buffers are not restored, thats why the code to create and fill buffers (as in the previous example) should be placed within the header of pages that use this buffer. Acting this way, buffer will be created on first access to the page.

Using buffers

To read data from buffer, the method Object get(String cacheName, Object key) has been defined. It returns value of an element having key key within the buffer named cacheName.

Example

 $=(GIFMIMETypeId, $cache.get("MIMETypes", "image/gif"))

The above example demonstrates how to get an identifier of the MIME type image/gif seen in the database. Instead of reading from the database, random-access memory is used.

Refreshing buffers

Typically buffers are used to cache data from the database. Due to content changes in the database, it is necessary to take care of buffers to contain up-to-date data. This requires cache refreshing when database content changes. To refresh the cache means to empty its content and to refill it with current data. To empty the cache named cacheName one has to use the method void clear(String cacheName). Good practice is to use jPALIO tasks to refresh cache(s).

Example

$executeSynchronized({
  $if((Boolean)$cache.isCacheCreated("MIMETypes"), {
    $// Empty the buffer.
    $cache.clear("MIMETypes")
    $// Store all up-to-date MIME types data into the buffer.
    $for(MIMEType, (LinkedList)$sql.read("select id, name from p_mime_types"), {
      $cache.put("MIMETypes", $MIMEType[1], $MIMEType[0])
    })
  })
})

Removing buffers

To remove the buffer one has to use void deleteCache(String cacheName) method, which removes the buffer named cacheName.

Example

$cache.deleteCache("MIMETypes")

Summary

The Cache module is a perfect solution when there is need to use frequently some rarely modified data from the database, which is typical for dictionary data. The Cache module then allows to achieve much faster access and to reduce database load.

Personal tools