facebook twitter hatena line email

「Php/Smarty/MemcacheクラスSmarty3」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
行1: 行1:
 
<pre>
 
<pre>
class Smarty_CacheResource_Memcache extends Smarty_CacheResource_Custom
+
<?php
 +
class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore
 
{
 
{
     private $_m;
+
     private $memcache;
  
 
     public function __construct($memcache)
 
     public function __construct($memcache)
 
     {
 
     {
         $this->_m = $memcache;
+
         $this->memcache = $memcache;
 
     }
 
     }
  
     protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
+
     protected function read(array $keys)
 
     {
 
     {
         $key = md5($name . $cache_id . $compile_id);
+
         $result = [];
        $data = $this->_m->getCache($key);
+
        foreach ($keys as $key) {
        if ($data === false) {
+
            $zendKey = md5($key);
            return false;
+
            $data = $this->memcache->getCache($zendKey);
 +
            if ($data !== false) {
 +
                $result[$key] = $data;
 +
            }
 
         }
 
         }
         // 解凍
+
         return $result;
        if (function_exists('gzuncompress')) {
+
            $data = gzuncompress($data);
+
        }
+
        // mtime + content 分離
+
        $pos = strpos($data, "\n");
+
        if ($pos === false) {
+
            return false;
+
        }
+
        $mtime  = (int)substr($data, 0, $pos);
+
        $content = substr($data, $pos + 1);
+
        return true;
+
 
     }
 
     }
  
     protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
+
     protected function write(array $keys, $expire = null)
 
     {
 
     {
         $key = md5($name . $cache_id . $compile_id);
+
         foreach ($keys as $key => $value) {
        // mtime + content
+
            $zendKey = md5($key);
        $data = time() . "\n" . $content;
+
             $this->memcache->setCache(
        // 圧縮
+
                $zendKey,
        if (function_exists('gzcompress')) {
+
                $value,
             $data = gzcompress($data, 1); // 1が一番速い
+
                $expire
 +
            );
 
         }
 
         }
         return $this->_m->setCache($key, $data, $exp_time);
+
         return true;
 
     }
 
     }
  
     protected function delete($name, $cache_id, $compile_id, $exp_time)
+
     protected function delete(array $keys)
 
     {
 
     {
         $key = md5($name . $cache_id . $compile_id);
+
         foreach ($keys as $key) {
        return $this->_m->delCache($key);
+
            $zendKey = $this->normalizeKey($key);
 +
            $this->memcache->delCache($zendKey);
 +
        }
 +
        return true;
 
     }
 
     }
  
行52: 行49:
 
         return true;
 
         return true;
 
     }
 
     }
}
 
 
</pre>
 
</pre>
  

2026年4月8日 (水) 18:51時点における最新版

<?php
class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore
{
    private $memcache;

    public function __construct($memcache)
    {
        $this->memcache = $memcache;
    }

    protected function read(array $keys)
    {
        $result = [];
        foreach ($keys as $key) {
            $zendKey = md5($key);
            $data = $this->memcache->getCache($zendKey);
            if ($data !== false) {
                $result[$key] = $data;
            }
        }
        return $result;
    }

    protected function write(array $keys, $expire = null)
    {
        foreach ($keys as $key => $value) {
            $zendKey = md5($key);
            $this->memcache->setCache(
                $zendKey,
                $value,
                $expire
            );
        }
        return true;
    }

    protected function delete(array $keys)
    {
        foreach ($keys as $key) {
            $zendKey = $this->normalizeKey($key);
            $this->memcache->delCache($zendKey);
        }
        return true;
    }

    protected function purge()
    {
        return true;
    }

呼び出し

require_once APPLICATION_PATH . '/models/MemcacheModel.php';
$cache = MemcacheModel::getInstance();
require_once APPLICATION_PATH . '/../library/Smarty_CacheResource_Memcache.php';
$this->registerCacheResource(
    'memcache',
    new Smarty_CacheResource_Memcache($cache)
);

$cacheのmemcacheはカスタマイズしてるので、それぞれの環境に合わせる。