Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface haloadmin_cache_ICache {
- public function exists($key);
- public function get($key);
- public function set($key, $value = null, $expires = null);
- public function delete($key);
- }
- $cache1 = new haloadmin_cache_SimpleCache();
- $cache2 = new haloadmin_cache_RedisCache();
- cache_this($cache1, 'hello', 'world');
- cache_this($cache2, 'hello', 'world');
- function cache_this(haloadmin_cache_ICache $cache, $key, $value = null) {
- $cache->set($key, $value);
- }
- abstract class haloadmin_cache_AbstractClass implements haloadmin_cache_ICache {
- final public function get($key) { return unserialize($this->getRaw($key)); }
- final public function set($key, $value = null, $expires = null) {
- $this->setRaw($key, serialize($value), $expires);
- }
- abstract public function getRaw($key);
- abstract public function setRaw($key, $value = null, $expires = null);
- }
- class haloadmin_cache_SimpleCache extends haloadmin_cache_AbstractClass {
- public function exists($key) { return isset($this->map[$key]); }
- public function delete($key) { unset($this->map[$key]; }
- public function getRaw($key) { return $this->map[$key]; }
- public function setRaw($key, $value = null, $expires = null) {
- $this->map[$key] = $value;
- }
- }
- require_once('haloadmin_cache_AbstractCache.php');
- require_once('Predis.php');
- require_once('dd_logging_LogFactory.php');
- class haloadmin_cache_RedisCache extends haloadmin_cache_AbstractClass {
- /**
- * @var dd_logging_ILogger
- */
- static public $LOGGER;
- public function __construct($url = null, $auth = null) {
- $this->redis = Predis\Client::create($url);
- }
- public function exists($key) { return $this->redis->exists($key); }
- public function delete($key) { $this->redis->delete($key); }
- public function getRaw($key) { return $this->redis->get($key); }
- public function setRaw($key, $value = null, $expires = null) {
- $this->redis->set($key, $value);
- if ( $expires ) {
- if ( self::$LOGGER->isInfoEnabled() ) {
- self::$LOGGER->info('Setting "' . $key . '" to expire in ' . $expires . ' seconds');
- }
- $this->redis->expire($key, $expires);
- }
- }
- }
- haloadmin_cache_RedisCache::$LOGGER = dd_logging_LogFactory::get('haloadmin_cache_RedisCache');
Add Comment
Please, Sign In to add comment