Skip to content

Instantly share code, notes, and snippets.

@jlogsdon
Created July 9, 2010 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlogsdon/470088 to your computer and use it in GitHub Desktop.
Save jlogsdon/470088 to your computer and use it in GitHub Desktop.
<?php
class Sg_Session_SaveHandler_Cache implements Zend_Session_SaveHandler_Interface {
protected $_cache;
/**
* Session lifetime
*
* @var int
*/
protected $_lifetime = false;
/**
* Session save path
*
* @var string
*/
protected $_sessionSavePath;
/**
* Session name
*
* @var string
*/
protected $_sessionName;
public function __construct(Zend_Cache_Core $cacheBackend, $options = array()) {
$this->_cache = $cacheBackend;
if ($options instanceof Zend_Config) {
$options = $options->toArray ();
}
foreach ($options as $key => $value) {
switch (strtolower($key)) {
case 'lifetime':
$this->setLifetime($value);
break;
}
}
if ($this->_lifetime === false) {
$this->setLifetime();
}
}
/**
* Close Session (for override)
*
* @return boolean
*/
public function close() {
return true;
}
/**
* Destroy a Session
*
* @param string $id
*/
public function destroy($id) {
return ( boolean ) $this->_cache
->remove ( $id );
}
/**
* Runs Garbage Collection
*
* @param int $maxlifetime <unused>
* @return void
*/
public function gc($maxlifetime) {
$this->_cache
->clean ( 'old' );
}
/**
* Opens Session Handler (good for file caching, not useful here)
*
* @return boolean
*/
public function open($save_path, $name) {
$this->_sessionSavePath = $save_path;
$this->_sessionName = $name;
return true;
}
/**
* Read session data
*
* @param string $id
* @return string
*/
public function read($id) {
return $this->_cache->load($id, false, true);
}
/**
* Write session data
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data) {
return (bool) $this->_cache->save($data, $id, 'Session', $this->_lifetime);
}
public function setLifetime($lifetime = null) {
if (0 > $lifetime) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception ();
} else if (empty ( $lifetime )) {
$this->_lifetime = ( int ) ini_get ( 'session.gc_maxlifetime' );
} else {
$this->_lifetime = ( int ) $lifetime;
}
}
public function getLifetime() {
return $this->_lifetime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment