facebook twitter hatena line email

Php/Smarty/Redisクラス

提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:18時点における127.0.0.1 (トーク)による版 (ページの作成:「 <?php /** * SmartyRedis * * @ex * require_once 'SmartyRedis.php'; * $redis = new SmartyRedis(); * $redis->connect('localhost', 6379); * $redis->setPrefix(...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索
<?php
/**
 * SmartyRedis
 *
 * @ex
 * require_once 'SmartyRedis.php';
 * $redis = new SmartyRedis();
 * $redis->connect('localhost', 6379);
 * $redis->setPrefix('CACHE1');
 * $smarty = new Smarty();
 * $smarty->caching = 1;
 * $smarty->cache_handler_func = array($redis, 'cacheHandler');
 */
class SmartyRedis
{
 var $_m;
 function SmartyRedis()
 {
   // ref to the redis object
   $this->_m = new Redis();
 }
 function connect($host, $port)
 {
   $this->_m->connect($host, $port);
 }
 function setPrefix($prefix)
 {
   $this->_m->setOption(Redis::OPT_PREFIX, $prefix);
 }
 function cacheHandler($action, &$smarty_obj, &$cache_content, $tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
 {
   // the key to store cache_ids under, used for clearing
   $key = 'smarty_caches';
   // check redis object
   if (get_class($this->_m) != 'Redis') {
         $smarty_obj->trigger_error("redis_cache_handler: \"$this->_m\" is not a redisd object");
     return false;
   }
   // unique cache id
   $cache_id = md5($tpl_file . $cache_id . $compile_id);
   switch ($action) {
   case 'read':
     // grab the key from redisd
     $contents = $this->_m->get($cache_id);
     // use compression
     if ($smarty_obj->use_gzip && function_exists("gzuncompress")) {
       $cache_content = gzuncompress($contents);
     } else {
       $cache_content = $contents;
     }
     $return = true;
     break;
   case 'write':
     // use compression
     if ($smarty_obj->use_gzip && function_exists("gzcompress")) {
       $contents = gzcompress($cache_content);
     } else {
       $contents = $cache_content;
     }
     // add the cache_id to the $key string
     $caches = $this->_m->get($key);
     if (!is_array($caches)) {
       $caches = array($cache_id);
       $this->_m->set($key, $caches);
     } else if (!in_array($cache_id, $caches)) {
       array_push($caches, $cache_id);
       $this->_m->set($key, $caches);
     }
     // store the value in redisd
     $stored = $this->_m->set($cache_id, $contents);
     if (!$stored) {
       $smarty_obj->trigger_error("redis_cache_handler: set failed.");
     }
     $return = true;
     break;
   case 'clear':
     if (empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
       // get all cache ids
       $caches = $this->_m->get($key);
       if (is_array($caches)) {
         $len = count($caches);
         for ($i = 0; $i < $len; $i++) {
           // assume no errors
           $this->_m->delete($caches[$i]);
         }
         // delete the cache ids
         $this->_m->delete($key);
         $result = true;
       }
     } else {
       $result = $this->_m->delete($cache_id);
     }
     if (!$result) {
       $smarty_obj->trigger_error("redis_cache_handler: clear query failed.");
     }
     $return = true;
     break;
   default:
     // error, unknown action
     $smarty_obj->trigger_error("redis_cache_handler: unknown action \"$action\"");
     $return = false;
     break;
   }
   return $return;
 }
}