Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Session_SaveHandler_Cache Class Reference

This save handler allows you to use any of the cache backends for session storage. More...

List of all members.

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-'

Detailed Description

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

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

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.

Parameters:
type$optionsallows passing in a cache backend
    {
        if (isset($options['backend'])) {
            $this->setBackend($options['backend']);
        }
        
        if (isset($options['idPrefix'])) {
            $this->setIdPrefix($options['idPrefix']);
        }
    }

Member Function Documentation

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.

Exceptions:
Zend_Session_SaveHandler_ExceptionIf no cache backend is set
Returns:
Zend_Cache_Backend_Interface Current backend
    {
        $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.

Returns:
bool always true
    {
        return true;
    }
P4Cms_Session_SaveHandler_Cache::destroy ( id)

Destroy Session - remove data from cache backend for the given session id.

Parameters:
string$idThe session id we are removing
Returns:
bool The result of calling remove on our cache backend
    {
        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.

Parameters:
int$maxlifetimenot used
Returns:
bool always true
    {
        return true;
    }
P4Cms_Session_SaveHandler_Cache::getBackend ( )

Returns the the cache backend presently in use or null if none.

Returns:
Zend_Cache_Backend_Interface|null the backend or null
    {
        return $this->_backend;
    }
P4Cms_Session_SaveHandler_Cache::getIdPrefix ( )

Returns the current ID prefix.

See mutator for details.

Returns:
string|null The id prefix in use
    {
        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.

Parameters:
string$savePathnot used
string$namenot used
Returns:
bool always true
    {
        return true;
    }
P4Cms_Session_SaveHandler_Cache::read ( id)

Read session data from our cache backend.

Parameters:
string$idThe session id to return details for.
Returns:
mixed The data the was previously stored or false
    {
        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.

Parameters:
Zend_Cache_Backend_Interface | array | null$backendA backend or null
Returns:
P4Cms_Session_SaveHandler_Cache To maintain a fluent interface
    {
        // 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.

Parameters:
string | null$idPrefixThe id prefix to use or null
Returns:
P4Cms_Session_SaveHandler_Cache To maintain a fluent interface
    {
        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.

Parameters:
string$idThe session id to store under
mixed$dataThe data to store
Returns:
bool The result of calling save on our cache backend
    {
        return $this->_getBackend()->save(
            $data, 
            $this->getIdPrefix() . $id, 
            array(), 
            Zend_Session::getOptions('gc_maxlifetime')
        );
    }

Member Data Documentation

P4Cms_Session_SaveHandler_Cache::$_backend = null [protected]
P4Cms_Session_SaveHandler_Cache::$_idPrefix = 'session-' [protected]

The documentation for this class was generated from the following file: