「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 | <?php | ||
class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore | |||
{ | { | ||
private $ | private $memcache; | ||
public function __construct($memcache) | public function __construct($memcache) | ||
{ | { | ||
$this-> | $this->memcache = $memcache; | ||
} | } | ||
protected function | protected function read(array $keys) | ||
{ | { | ||
$key = md5($ | $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; | return true; | ||
} | } | ||
protected function | protected function delete(array $keys) | ||
{ | { | ||
$key = | foreach ($keys as $key) { | ||
$zendKey = $this->normalizeKey($key); | |||
$this->memcache->delCache($zendKey); | |||
$ | |||
} | } | ||
return true; | |||
return | |||
} | } | ||
| 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はカスタマイズしてるので、それぞれの環境に合わせる。