Caching State

Commonly, a webserver is stateless: after a request has been completed, no data is retained on the server. This is also the case for the Matlab Production Server and calls to Python via WSGI. Therefore, the calls to gui_event are also stateless.

If it is necessary or desirable to retain the state of the application server-side, Simian GUI provides a caching mechanism that can be used to store and retrieve data for a session.

  • setCache: Cache data under a given name to be retrieved later.
  • getCache: Get cached data for a given name.

Syntax

utils.setCache(meta_data, name, data)
data, is_found = utils.getCache(meta_data, name)
utils.setCache(metaData, name, data)
[data, isFound] = utils.getCache(metaData, name);

Arguments

  • metaData: the session_id from the meta data is used to support multiple sessions
  • name: the name of the cache entry
  • data: data to store in the cache
  • isFound: boolean value indicating whether the cache entry was found

Local session

When running an application in Python locally, the cached information is stored in a dictionary object in the Python session. When running an application in Matlab locally, the cache persistently stores the data in the workspace of the Matlab session.

Python - Redis

For applications run from a server (or even locally) it is possible to store the cached data on a Redis server. To enable Redis caching add a cache key to the meta_data dict containing a dict with key enabled set to True, a key type set to "redis", and an options dict with key-value pairs being the named inputs of the redis.Redis class constructor of redis-py.

Matlab Production Server

When deploying an application to the Matlab Production Server the cache data is stored in Redis, an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

The default Matlab Production Server Redis cache is configured to not evict any data from memory and the maximum amount of memory is not limited. After a restart of the Redis cache, all cached data is lost.

The maximum size of a value stored in Redis is limited to 512 MiB. Depending on the data type, the required amount of memory for storing the value in cache may be larger or smaller compared to the variable in the Matlab workspace.

Please refer to the Redis documentation and MPS documentation for more information about the Redis cache on the Matlab Production Server.