Perforce Chronicle 2012.2/486814
API Documentation
|
This save handler allows you to use any of the cache backends for session storage. More...
Public Member Functions | |
__construct ($options=null) | |
If the passed options array contains a 'backend' or 'idPrefix' key the value will be passed to 'setBackend' or 'setIdPrefix' respectively. | |
close () | |
Simply returns true. | |
destroy ($id) | |
Destroy Session - remove data from cache backend for the given session id. | |
gc ($maxlifetime) | |
This is intended to remove session data older than $maxlifetime (in seconds). | |
getBackend () | |
Returns the the cache backend presently in use or null if none. | |
getIdPrefix () | |
Returns the current ID prefix. | |
open ($savePath, $name) | |
Simply returns true. | |
read ($id) | |
Read session data from our cache backend. | |
setBackend ($backend) | |
Used to set a cache backend to be used for session storage. | |
setIdPrefix ($idPrefix) | |
You can specify an id prefix to use when storing session details in the cache backend. | |
write ($id, $data) | |
Write Session - commit/update data to our cache backend If you are using a shared cache backend these IDs could potentially collide with other entries; consider setting an id prefix to reduce collision likelyhood. | |
Protected Member Functions | |
_getBackend () | |
Returns the cache backend or throws if none is set. | |
Protected Attributes | |
$_backend = null | |
$_idPrefix = 'session-' |
This save handler allows you to use any of the cache backends for session storage.
We intend it primarily to allow memcached to be used for session storage via the MemcacheTagged backend.
To enable this save handler from the application.ini use the format: resources.session.savehandler.class = P4Cms_Session_SaveHandler_Cache resources.session.savehandler.options.backend.name = P4Cms_Cache_Backend_MemcachedTagged resources.session.savehandler.options.backend.customBackendNaming = 1
P4Cms_Session_SaveHandler_Cache::__construct | ( | $ | options = null | ) |
If the passed options array contains a 'backend' or 'idPrefix' key the value will be passed to 'setBackend' or 'setIdPrefix' respectively.
No other options are presently supported.
type | $options | allows passing in a cache backend |
{ if (isset($options['backend'])) { $this->setBackend($options['backend']); } if (isset($options['idPrefix'])) { $this->setIdPrefix($options['idPrefix']); } }
P4Cms_Session_SaveHandler_Cache::_getBackend | ( | ) | [protected] |
Returns the cache backend or throws if none is set.
This differs from the public accessor which will return null when no backend is set.
Zend_Session_SaveHandler_Exception | If no cache backend is set |
{ $backend = $this->getBackend(); if (!$backend) { throw new Zend_Session_SaveHandler_Exception('No cache has been set'); } return $backend; }
P4Cms_Session_SaveHandler_Cache::close | ( | ) |
Simply returns true.
It doesn't appear we need this method but the interface requires an implementation.
{ return true; }
P4Cms_Session_SaveHandler_Cache::destroy | ( | $ | id | ) |
Destroy Session - remove data from cache backend for the given session id.
string | $id | The session id we are removing |
{ return $this->_getBackend()->remove($this->getIdPrefix() . $id); }
P4Cms_Session_SaveHandler_Cache::gc | ( | $ | maxlifetime | ) |
This is intended to remove session data older than $maxlifetime (in seconds).
Our cache backends doen't have this capability so we have simply stubbed out the method.
int | $maxlifetime | not used |
{ return true; }
P4Cms_Session_SaveHandler_Cache::getBackend | ( | ) |
Returns the the cache backend presently in use or null if none.
{
return $this->_backend;
}
P4Cms_Session_SaveHandler_Cache::getIdPrefix | ( | ) |
Returns the current ID prefix.
See mutator for details.
{
return $this->_idPrefix;
}
P4Cms_Session_SaveHandler_Cache::open | ( | $ | savePath, |
$ | name | ||
) |
Simply returns true.
It doesn't appear we need this method but the interface requires an implementation.
string | $savePath | not used |
string | $name | not used |
{ return true; }
P4Cms_Session_SaveHandler_Cache::read | ( | $ | id | ) |
Read session data from our cache backend.
string | $id | The session id to return details for. |
{ return $this->_getBackend()->load($this->getIdPrefix() . $id); }
P4Cms_Session_SaveHandler_Cache::setBackend | ( | $ | backend | ) |
Used to set a cache backend to be used for session storage.
You can pass in a backend instance or an array of config details. The array format matches that of the 'backend' section used by the Zend_Cache_Manager.
Zend_Cache_Backend_Interface | array | null | $backend | A backend or null |
{ // deal with object or null input if ($backend instanceof Zend_Cache_Backend_Interface || $backend === null) { $this->_backend = $backend; return $this; } // if we don't have a valid array by here, we have to throw if (!is_array($backend) || !isset($backend['name'])) { throw new InvalidArgumentException('Can not set invalid backend'); } // use the zend_cache factory to convert array input to a backend // this is mainly useful for dealing with shorthand backend names. $frontend = Zend_Cache::factory( new Zend_Cache_Core, $backend['name'], array(), isset($backend['options']) ? $backend['options'] : array(), false, isset($backend['customBackendNaming']) ? $backend['customBackendNaming'] : false ); $this->_backend = $frontend->getBackend(); return $this; }
P4Cms_Session_SaveHandler_Cache::setIdPrefix | ( | $ | idPrefix | ) |
You can specify an id prefix to use when storing session details in the cache backend.
This prefix will only be used under the hood, it won't have any impact on the session key sent as a cookie to the end user.
This is intended to allow namespacing the session data when sharing your cache backend with other data.
string | null | $idPrefix | The id prefix to use or null |
{ if (!is_string($idPrefix) && !is_null($idPrefix)) { throw new InvalidArgumentException('ID prefix must be a string or null'); } $this->_idPrefix = $idPrefix; return $this; }
P4Cms_Session_SaveHandler_Cache::write | ( | $ | id, |
$ | data | ||
) |
Write Session - commit/update data to our cache backend If you are using a shared cache backend these IDs could potentially collide with other entries; consider setting an id prefix to reduce collision likelyhood.
string | $id | The session id to store under |
mixed | $data | The data to store |
{ return $this->_getBackend()->save( $data, $this->getIdPrefix() . $id, array(), Zend_Session::getOptions('gc_maxlifetime') ); }
P4Cms_Session_SaveHandler_Cache::$_backend = null [protected] |
P4Cms_Session_SaveHandler_Cache::$_idPrefix = 'session-' [protected] |