Rediska

Follow me on twitter…

Added Rediska instance manager and refactored a lot

August 25, 2010

Last week I pushed Rediska instance manager and a lot of other changes to github.

Instance manager

There can be different components (cache, sessions and etc) in you application, which may need their own Rediska instance with specific options (namespace, servers and etc).

Manager class is designed to store these Rediska instances. Manager can also store an array of Rediska options and Rediska instance will be created on a first request (lazy-loaded).

There are two new options in Rediska:

  • name - instance name. It will be default if other is not specified
  • addToManager - whether or not to add Rediska instance to manager when it's created. Defaults to true
<?php

// Create 'default' instance
$rediska = new Rediska();

// Get 'default' instance from manager
$rediska = Rediska_Manager::get();
print $rediska->getName(); #=> default

// Create 'cache' instance
$rediska = new Rediska(array('name' => 'cache', 'namespace' => 'Cache_'));

// Get 'cache' instance from manager
$rediska = Rediska_Manager::get('cache');
print $rediska->getName(); #=> cache

// Add 'sessions' instance though options
Rediska_Manager::add(array('name' => 'sessions', 'namespace' => 'Sessions_'));
// Object created when it needed
$rediska = Rediska_Manager::get('sessions');
print $rediska->getName(); #=> sessions

?>

Key objects

I sligtly changed constructors of Rediska_Key, Rediska_Key_List, Rediska_Key_Set, Rediska_Key_SortedSet, Rediska_Key_Hash. Second argument became an array of options:

  • expire - Expire time
  • expireIsTimestamp - Expire time is timestamp (in seconds). For default false
  • serverAlias - Server alias or connection object
  • rediska - Rediska instance name, Rediska object or Rediska options for new instance. default if not specified
<?php

// Create 'cache' instance
$rediska = new Rediska(array('name' => 'cache'));

// Create key with rediska 'cache' instance
$cache = new Rediska_Key_Hash('cacheKeyName', array('rediska' => 'cache'));

// Or pass Rediska object to 'rediska' option
$cache = new Rediska_Key_Hash('cacheKeyName', array('rediska' => $rediska));

// Or pass Rediska option to create new instance
$cache = new Rediska_Key_Hash('cacheKeyName', array('rediska' => array('namespace' => 'Cache_')));

?>

Zend Framework integration

Now you can specify a few Rediska instances in a application.ini and you can reference their names in other resources configuration.

; Initialize Rediska cache instance resources.rediska.instances.cache.namespace = 'Cache_' resources.rediska.instances.cache.servers.0.host = 127.0.0.1 resources.rediska.instances.cache.servers.0.port = 6379 resources.rediska.instances.cache.servers.1.host = 127.0.0.2 resources.rediska.instances.cache.servers.1.port = 6379 ; Initialize Rediska sessions instance resources.rediska.instances.sessions.namespace = 'Sessions_' ; Initialize cache resources.cachemanager.redis.frontend.name = Core resources.cachemanager.redis.backend.name = Rediska_Zend_Cache_Backend_Redis ; Reference to Rediska 'cache' instance resources.cachemanager.redis.backend.options.rediska = cache resources.cachemanager.redis.backend.customBackendNaming = true resources.cachemanager.redis.frontendBackendAutoload = true ; Initialize sessions resources.session.saveHandler.class = "Rediska_Zend_Session_SaveHandler_Redis" ; Reference to Rediska 'sessions' instance resources.session.saveHandler.options.rediska = sessions

Rediska_Autoloader и Rediska_Commands

Autoload and command manager are in separate classes.

All Rediska commands in your IDE autocomplete

There is a script scripts/add_command_methods.php which generates Rediska command methods in following classes Rediska, Rediska_Pipeline, Rediska_Connection_Specified, Rediska_Transaction. This way IDE autocomplete and phpDocumentator will work properly.

Comments