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

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
ページの作成:「<pre> class Smarty_CacheResource_Memcache extends Smarty_CacheResource_Custom { private $_m; public function __construct($memcache) { $this->_m = $me...」
 
編集の要約なし
 
(同じ利用者による、間の2版が非表示)
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 = [];
 
        foreach ($keys as $key) {
        $data = $this->_m->getCache($key);
            $zendKey = md5($key);
 
            $data = $this->memcache->getCache($zendKey);
        if ($data === false) {
            if ($data !== false) {
             return false;
                $result[$key] = $data;
             }
         }
         }
        return $result;
    }


         if (function_exists('gzuncompress')) {
    protected function write(array $keys, $expire = null)
             $content = gzuncompress($data);
    {
        } else {
         foreach ($keys as $key => $value) {
            $content = $data;
             $zendKey = md5($key);
            $this->memcache->setCache(
                $zendKey,
                $value,
                $expire
            );
         }
         }
        $mtime = time();
         return true;
         return true;
     }
     }


     protected function save($id, $name, $cache_id, $compile_id, $content, $mtime)
     protected function delete(array $keys)
     {
     {
         $key = md5($name . $cache_id . $compile_id);
         foreach ($keys as $key) {
 
            $zendKey = $this->normalizeKey($key);
        if (function_exists('gzcompress')) {
             $this->memcache->delCache($zendKey);
             $content = gzcompress($content);
         }
         }
 
         return true;
         return $this->_m->setCache($key, $content);
    }
 
    protected function delete($name, $cache_id, $compile_id, $exp_time)
    {
        $key = md5($name . $cache_id . $compile_id);
        return $this->_m->delCache($key);
     }
     }


51行目: 49行目:
         return true;
         return true;
     }
     }
}
</pre>
</pre>
呼び出し
<pre>
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)
);
</pre>
$cacheのmemcacheはカスタマイズしてるので、それぞれの環境に合わせる。

2026年4月8日 (水) 09: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はカスタマイズしてるので、それぞれの環境に合わせる。