Skip to content

Instantly share code, notes, and snippets.

@drewgillson
Created December 25, 2011 00:25
Show Gist options
  • Save drewgillson/1518557 to your computer and use it in GitHub Desktop.
Save drewgillson/1518557 to your computer and use it in GitHub Desktop.
Redis for TinyBrick Lightspeed - cache server model
<?php
class TinyBrick_LightSpeed_Model_Server_Redis {
protected $_server;
protected $_enabled = false;
public function getServer() {
if (!$this->_server) {
require_once($_SERVER['DOCUMENT_ROOT'] . '/lib/Credis/Client.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/lib/Zend/Cache/Backend.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/lib/Zend/Cache/Backend/Interface.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/lib/Zend/Cache/Backend/ExtendedInterface.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/app/code/core/Zend/Cache/Backend/Redis.php');
$options = array();
$options['server'] = (string)Mage::getConfig()->getNode('lightspeed/cache/backend_options/server');
$options['port'] = (int)Mage::getConfig()->getNode('lightspeed/cache/backend_options/port');
$options['database'] = (int)Mage::getConfig()->getNode('lightspeed/cache/backend_options/database');
$options['timeout'] = (int)Mage::getConfig()->getNode('lightspeed/cache/backend_options/timeout');
$options['force_standalone'] = (int)Mage::getConfig()->getNode('lightspeed/cache/backend_options/force_standalone');
$options['automatic_cleaning_factor'] = (int)Mage::getConfig()->getNode('lightspeed/cache/backend_options/automatic_cleaning_factor');
$this->_enabled = true;
$this->_server = new Zend_Cache_Backend_Redis($options);
}
return $this->_server;
}
public function save($key, $data, $expires=0, array $tags=array()) {
$server = $this->getServer();
if ($server && $this->_enabled) {
$server->save(serialize($data), $key, $tags, $expires);
}
}
public function cleanByTag($tag) {
if ($this->_server) {
$this->_server->_removeByMatchingTags($tag);
}
}
public function clean($tags=array()) {
if ($this->_server) {
if (count($tags) && !in_array('LIGHTSPEED', $tags)) {
$this->_server->_removeByMatchingTags($tags);
}
else {
$this->_server->clean(Zend_Cache::CLEANING_MODE_ALL, $tags);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment