Tutorial - cache

Cache features supports storing, fetching and clearing temporal all client available data on server. Unlike shared storage where program itself must control memory usage for stored data, cache actually is cache and expires data when memory limits starts to hit. Default cache size is 16 megabytes and is configurable by scached's configuration. As like with shared storage, partitioned scached can have multiple independent caches.

Cache's expiration algorithm is technically a ring where newly inserted and last accessed paths goes to begin while expires being done from end. (Means: not very smart)

Cache supports same path-logic than other functions. Especially when using cache you should consider organizing you cached data so it can be invalidated by clearing containing parent node.

Previous version's documentation expresses that cache-, shared-storage and counter-functions doesn't need real initialized session. This has changed from 0.99 onwards. You need always valid initialized session, but of cource you can you can use some dummy session, common to all - not real private cookie-secured sessions.

This change is done to simplify and speed up partition assignment.

Cache operations

Functions are :

All cache functions above support paths so you can organize you data as subtrees to clear mutually dependent data as complete entities. All cache function are also available through scache_iov interface.

Example code :

$conn = scache_reset('Example');

if (!($item = scache_chget($conn, '/cached/data/item'))) {

      $item = get_item_from_db();
      scache_chput($conn, $item);
}

Subtree deletes :

$conn = scache_reset('Example);

scache_chput($conn, 'cache/messages/data/item-1', get_from_db(1));
scache_chput($conn, 'cache/messages/data/item-2', get_from_db(2));
scache_chput($conn, 'cache/messages/data/item-3', get_from_db(2));
scache_chput($conn, 'cache/messages/index', generate_index());
scache_chput($conn, 'cache/otherdata', gen_random_data());

if (just_messages_changed())
    scache_chclear($conn, '/cached/messages');

Misc notes

Cache has some issues with internal leftover memory nodes after cache expiration process that realizes as memory consumption when creating lots of unique multi-level paths without ever overwriting or clearing parent nodes.

Technically data is stored on nodes which on turn are stored on paths. On expiration data is cleared but containing node is not removed from path's b-trees but from first adjacent up b-tree only.

This will be either addressed on future releases or declared as feature.