facebook twitter hatena line email

Php/Smarty/Memcacheクラス

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

Smartyのキャッシュにmemcachedを使用する方法

クラス化された物がなかったので http://swag.dk/swag/kode/を参考にSmartyMemcacheクラスを作成した。 使い方は下の@exを参照

注意:clear_all_cache()を使うとmemcache_cache_handler: clear query failed.のエラーが出る

php4でも使えるように書いてますが未検証

SmartyMemcache.php
<?php
/**
 * SmartyMemcache
 *
 * @ex
 * require_once 'SmartyMemcache.php';
 * $memcache = new SmartyMemcache();
 * $memcache->connect('localhost', 11211);
 * $smarty = new Smarty();
 * $smarty->caching = 1;
 * $smarty->cache_handler_func = array($memcache, 'cacheHandler');
 *
 * @url http://f0000.nonip.info/wiki/work/index.php?php%2FSmarty%2FMemcache%E3%82%AF%E3%83%A9%E3%82%B9
 * @refurl http://swag.dk/swag/kode/
 */
class SmartyMemcache
{
  var $_m;
  function SmartyMemcache()
  {
    // ref to the memcache object
    $this->_m = new Memcache();
  }
  function connect($host, $port)
  {
    $this->_m->connect($host, $port);
  }
  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 memcache object
    if (get_class($this->_m) != 'Memcache') {
          $smarty_obj->trigger_error("memcache_cache_handler: \"$this->_m\" is not a memcached object");
      return false;
    }
    // unique cache id
    $cache_id = md5($tpl_file . $cache_id . $compile_id);
    switch ($action) {
    case 'read':
      // grab the key from memcached
      $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 memcached
      $stored = $this->_m->set($cache_id, $contents);
      if (!$stored) {
        $smarty_obj->trigger_error("memcache_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("memcache_cache_handler: clear query failed.");
      }
      $return = true;
      break;
    default:
      // error, unknown action
      $smarty_obj->trigger_error("memcache_cache_handler: unknown action \"$action\"");
      $return = false;
      break;
    }
    return $return;
  }
}
?>