Object Cache

Gipetto edited this page Dec 14, 2011 · 3 revisions

OpenVBX version 1.2 introduces an Object Caching layer that supports caching data in MySQL, APC & Memcache.

The cache implementation is a Generational Cache. Instead of wiping cache clean when old cache becomes invalid the system instead tracks an incremental value that represents a generation of cache. Each group of cache maintains its own generation marker so that smaller groups of data can be invalidated instead of having to invalidate the entire cache whenever a small piece of data changes.

Cache is implemented in two general areas:

General Object Cache

The first is a general object cache for OpenVBX. This layer is for objects used in OpenVBX like Flows, Users, Devices and Settings. Cache settings are manually configured using a cache config file in the OpenVBX config directory.

This layer has a 3 possible modes:

  • Memory (single page load memory, default). This layer is used when either APC or Memcache is not available. It will simply create an cache for the current page load. While it doesn't provide a drastic increase in performance, it does reduce a few database hits during a single page load.
  • APC. This layer uses the standard PHP APC library (http://pecl.php.net/package/apc). This build was developed and tested using APC installed via PECL.
  • Memcache. This layer uses the PECL Memcache library (http://pecl.php.net/package/memcache not the Memcached library though support for both would be easy to add if there's actual demand). Multiple memcache server use is supported via the config file. This build was developed and tested using Memcache installed via PECL.

API Cache

The 2nd is an API cache. At this time the API cache uses only the database to cache the results of API requests. Despite being named API Cache, this cache layer can be used for just about any data that should be transient in nature.

Configuration

Cache is manually enabled and configured through a new file OpenVBX/config/cache.php. There are only a few options to configure:

  • cache_enabled, boolean, default: TRUE. Controls the activity of the object cache. This has no effect on the API cache.
  • cache_type, string, default: memory. Either memory, apc or memcache.
  • default_expires, int, default: 3600. Time in seconds that cache should be held on to for. This is the system default. Individual models can change the TTL for their objects. See the VBX_Call model for an example of setting a custom TTL.
  • memcache, Array. Example settings for using Memcache. Defaults to the host machine and standard Memcache port. Consult the Memcache documentation on the proper way to set up multi-server cache systems.

Object Cache Use

OpenVBX automatically starts the object caches wether they're active or not. This allows code to set & check caches without having to worry about wether the cache is active or not. An inactive cache will act the same as an expired cache.

The two caches are registered with OpenVBX separetely but their use is the same.

$ci =& get_instance();
$ci->cache->... // Object cache
$ci->api_cache->... // API Cache

Both caches have the same interfaces and are generally interchangeable. The API cache uses the database for caching since we always want to cache these results while the object cache availability and mechanism may change based on server configuration.

Every cache object contains the same methods:

  • get($key, $group = null, $tenant_id) - retrieve a cached item. Returns a found object or FALSE if no cache object is found or if the TTL has expired.
  • set($key, $data, $group, $tenant_id, $expires) - set a cached item. Returns TRUE on success, FALSE on failure.
  • delete($key, $group, $tenant_id) - delete a cached item. Returns TRUE on success, FALSE on failure.
  • invalidate($group, $tenant_id) - invalidate the existing cached items in that group. Invalidation works differently depending upon the cache mechanism. For Memory and Database caches the groups items are deleted immediately. For APC & Memcache a generation key is incremented so that the next time an item is looked for the generation key will mismatch causing a failure to find the item.
  • flush() - flush the entire cache.

Misc

  • Regular cache can be turned on and off, but the API cache is always active.
  • While the cache system is modular and can be extended with custom cache types, the loader is not dynamic and will not automatically pick up on new cache types. Modify OpenVBX/libraries/Caches/Abstract.php::load() to add your new cache type to OpenVBX.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.