User Tools

Site Tools


sib:cache

SIB Cache

Overview

The sib_cache is a module for caching data in the SIB. The cache module can listen to a setter topic which will update the cache according to a set of rules, and it will listen to a getter topic that will return cached data to the responseTopic of the calling message.

Details

Modulesib_cache
Version1.3.3

Workflow

  • Listen to settopic
    • on message
      • if .custom.set run .custom.set on data
      • else merge the msg.value data into the cache object
  • Listen to gettopic
    • on message
      • if .custom.get run .custom.get on data
      • else get the msg.key object from the cache and return the data

The SIBCache get method uses the msg.key data to do a cache lookup and adds a msg.cache object to the message structure on the format:

	msg.cache = {
		lookup: true,		// Tells the next service that a cace lookup was done
		hit: true, 		// Boolean, true on cache hit, false otherwise
		data: [data]		// The data retreived from the cache, null on cache miss
	};

The cache set method expects an msg.cache object of the same format:

	msg.cache = {
		key: "key_to_set",	// The key to set in the cache
		data: [data]		// The data to set, can be of "any" type
	};

Custom configuration

sibcache/custom.js
"use strict";
var CacheCustom = {
	_id: "cacheSample:1",
	_name: "SIB Cache sample",
	checkTTL: (cache, ttl, cb)=>{cb();},
	get: (cache, topic, msg, cb)=>{cb({data:123});},
	getsize: (cache, cb)=>{cb( cache.length );},
 
	loggerCfg: {
		dest: {
			file: false,
			console: true,
			mqtt: true
		}
	},
	mqtt: "mqtt://localhost",
	set: (cache, topic, msg, cb)=>{cache.value=1;cb();},
	settopic: "SIB/CACHE/SET",
	ttl: 120,
	ttlcheckinterval: 10,
	topic: "SIB/CACHE/GET",
};
 
module.exports = CacheCustom;
  • _id: The unique ID of this service
  • _name: A plaintext description of this service
  • checkTTL: Method that overrides the default checkTTL method. Executed every ttlcheckinterval and responsible for clearing out all old data in the cache
  • get: Method to override the standard cache function for getting information from the cache
  • getsize: Method for getting the size of the cache, in case the standard cache function is overridden
  • loggerCfg: A JSON Object defining what logger actions should be active for this service (see Logger Configuration
  • mqtt: The MQTT connection string for the internal MQTT transport
  • set: Method to override the standard cache function for setting information to the cache
  • settopic: The MQTT topic to subscribe to on the internal MQTT transport for setting information to the cache. If this is an array all the topics will be subscribed to.
  • topic: The topic to listen to for get statements
  • ttl: Number of seconds that the data will live in the cache
  • ttlcheckinterval: Number of seconds between the checkTTL executions, ie resolution of ttl

Default behaviour

When the behaviour of the SIBCache is not overridden by overriding the checkTTL, get, getsize and set methods the default behaviour of the cache is used. Here is a short description of the inner workings of the default cache.

The SIBCache.cache object contains the in-memory cache as an object. When setting the cache the SIBCache.cache[msg.cache.key] element is set to a new object on the form:

{data:msg.cache.data, t:Date.now()}

This allows the checkTTL method to loop through all elements of the SIBCache.cache object and check the t value against the current time and TTL settings, removing all elements of the SIBCache.cache object that are too old.

The setSize method simply checks the number of elements in the SIBCache.chace object and reports that as the cache length.

The get method simply checks if the msg.cache.key key exists in the SIBCache.cache object. If so the SIBCache.cache[msg.key].data element is returned in the response message.

sib/cache.txt · Last modified: 2019/11/06 12:01 by hubbe