Source for file Memcached.php

Documentation is available at Memcached.php

  1. <?php
  2. /**
  3.  *  Memcached.php
  4.  *
  5.  *  @author     Sotaro Karasawa <sotaro.k /at/ gmail.com>
  6.  *  @package    Ethna
  7.  *  @version    $Id: 1fbc96e645255b34b8b3d8f9e60bc75fb270f213 $
  8.  */
  9.  
  10. /**
  11.  *  キャッシュマネージャクラス(pecl::memcached 版)
  12.  *
  13.  *  @author     Sotaro Karasawa <sotaro.k /at/ gmail.com>
  14.  *  @access     public
  15.  *  @package    Ethna
  16.  */
  17. {
  18.     /**#@+  @access private */
  19.  
  20.     /** @var    object  Memcached    Memcached オブジェクト */
  21.     private $m null;
  22.  
  23.     /** @var    array   plugin configure */
  24.     public $config_default = array(
  25.         'host' => 'localhost',
  26.         'port' => '11211',
  27.         'use_pconnect' => false,
  28.         'retry' => 3,
  29.         'timeout' => 3,
  30.     );
  31.  
  32.     protected $_get_data_cache = array();
  33.  
  34.     /**#@-*/
  35.  
  36.     /**
  37.      *  _load
  38.      *
  39.      *  @access protected
  40.      */
  41.     protected function _load()
  42.     {
  43.         parent::_load();
  44.  
  45.         if ($this->config['use_pconnect']{
  46.             $this->new Memcached($this->ctl->getAppId());
  47.         }
  48.         else {
  49.             $this->new Memcached();
  50.         }
  51.  
  52.         if (isset($this->config['servers']&& is_array($this->config['servers'])) {
  53.             $this->m->addServers($this->config['servers']);
  54.         }
  55.         else {
  56.             $this->m->addServer($this->config['host']$this->config['port']);
  57.         }
  58.  
  59.         $this->m->setOption(Memcached::OPT_CONNECT_TIMEOUT$this->config['timeout'1000);
  60.         //$this->m->setOption(Memcached::OPT_CONNECT_TIMEOUT, $this->config['retry']);
  61.     }
  62.  
  63.     /**
  64.      *  get cached data
  65.      *
  66.      *  キャッシュに値が設定されている場合はキャッシュ値
  67.      *  が戻り値となる。キャッシュに値が無い場合やlifetime
  68.      *  を過ぎている場合、エラーが発生した場合はEthna_Error
  69.      *  オブジェクトが戻り値となる。
  70.      *
  71.      *  @access public
  72.      *  @param  string  $key        cache key
  73.      *  @param  int     $lifetime   cache lifetime
  74.      *  @param  string  $namespace  namespace
  75.      *  @return mixed   value
  76.      */
  77.     public function get($key$lifetime null$namespace null)
  78.     {
  79.         $cache_key $this->_getCacheKey($namespace$key);
  80.         if ($cache_key == null{
  81.             return Ethna::raiseError('invalid cache key (too long?)'E_CACHE_NO_VALUE);
  82.         }
  83.  
  84.         if (isset($this->_get_data_cache[$cache_key])) {
  85.             return $this->_get_data_cache[$cache_key]['data'];
  86.         }
  87.  
  88.         $value $this->m->get($cache_key);
  89.         if (!$value{
  90.             return Ethna::raiseWarning(
  91.                 sprintf('no such cache, key="%s", message="%s"'$key$this->m->getResultMessage()),
  92.                 E_CACHE_NO_VALUE
  93.             );
  94.         }
  95.  
  96.         $time $value['time'];
  97.         $data $value['data'];
  98.  
  99.         // ライフタイムチェック
  100.         if ($lifetime !== null{
  101.             if (($time $lifetimetime()) {
  102.                 return Ethna::raiseWarning('lifetime expired'E_CACHE_EXPIRED);
  103.             }
  104.         }
  105.  
  106.         // result cache
  107.         $this->_get_data_cache[$cache_key$value;
  108.  
  109.         return $data;
  110.     }
  111.  
  112.     /**
  113.      *  キャッシュの最終更新日時を取得する
  114.      *
  115.      *  @access public
  116.      *  @param  string  $key        cache key
  117.      *  @param  string  $namespace  cache namespace
  118.      *  @return int     unixtime
  119.      */
  120.     public function getLastModified($key$namespace null)
  121.     {
  122.         $cache_key $this->_getCacheKey($namespace$key);
  123.         if ($cache_key == null{
  124.             return Ethna::raiseError('invalid cache key (too long?)'E_CACHE_NO_VALUE);
  125.         }
  126.  
  127.         $value $this->get($cache_key);
  128.         if (Ethna::isError($value)) {
  129.             return $value;
  130.         }
  131.  
  132.         return $value['time'];
  133.     }
  134.  
  135.     /**
  136.      *  check cache exists
  137.      *
  138.      *  @access public
  139.      *  @param  string  $key        cache key
  140.      *  @param  int     $lifetime   lifetime
  141.      *  @param  string  $namespace  namespace
  142.      */
  143.     public function isCached($key$lifetime null$namespace null)
  144.     {
  145.         $r $this->get($key$lifetime$namespace);
  146.  
  147.         return !Ethna::isError($r);
  148.     }
  149.  
  150.     /**
  151.      *  set cache
  152.      *
  153.      *  @access public
  154.      *  @param  string  $key        cache key
  155.      *  @param  mixed   $value      cache value
  156.      *  @param  int     $timestamp  timestamp of last modified
  157.      *  @param  string  $namespace  namespace
  158.      *  @param  int     $lifetime   expiration
  159.      */
  160.     public function set($key$value$timestamp null$namespace null$expiration null)
  161.     {
  162.         $cache_key $this->_getCacheKey($namespace$key);
  163.         if ($cache_key === null{
  164.             return Ethna::raiseError('invalid cache key (too long?)'E_CACHE_NO_VALUE);
  165.         }
  166.  
  167.         $time $timestamp $timestamp time();
  168.         $expiration $expiration $expiration 0;
  169.         if (!$this->m->set($cache_keyarray('time' => $time'data' => $value)$expiration)) {
  170.             return Ethna::raiseError(
  171.                 sprintf('failed to set cache, key="%s", message="%s"'$key$this->m->getResultMessage()),
  172.                 E_CACHE_GENERAL
  173.             );
  174.         }
  175.  
  176.         return true;
  177.     }
  178.  
  179.     /**
  180.      *  delete cache
  181.      *
  182.      *  @access public
  183.      *  @param  string  $key        キャッシュキー
  184.      *  @param  string  $namespace  キャッシュネームスペース
  185.      */
  186.     public function clear($key$namespace null)
  187.     {
  188.         $cache_key $this->_getCacheKey($namespace$key);
  189.         if ($cache_key === null{
  190.             return Ethna::raiseError('invalid cache key (too long?)'E_CACHE_NO_VALUE);
  191.         }
  192.  
  193.         if (!$this->m->delete($cache_key)) {
  194.             return Ethna::raiseError(
  195.                 sprintf('failed to clear cache, key="%s", message="%s"'$key$this->m->getResultMessage()),
  196.                 E_CACHE_NO_VALUE
  197.             );
  198.         }
  199.  
  200.         return true;
  201.     }
  202.  
  203.     /**
  204.      *  キャッシュデータをロックする
  205.      *
  206.      *  @access public
  207.      *  @param  string  $key        キャッシュキー
  208.      *  @param  int     $timeout    ロックタイムアウト
  209.      *  @param  string  $namespace  キャッシュネームスペース
  210.      *  @return bool    true:成功 false:失敗
  211.      */
  212.     public function lock($key$timeout 5$namespace null)
  213.     {
  214.         // not supported
  215.         return true;
  216.     }
  217.  
  218.     /**
  219.      *  キャッシュデータのロックを解除する
  220.      *
  221.      *  @access public
  222.      *  @param  string  $key        キャッシュキー
  223.      *  @param  string  $namespace  キャッシュネームスペース
  224.      *  @return bool    true:成功 false:失敗
  225.      */
  226.     public function unlock($key$namespace null)
  227.     {
  228.         // not supported
  229.         return true;
  230.     }
  231.  
  232.     /**
  233.      *  set option of pecl::memcached directly
  234.      *
  235.      *  @access public
  236.      *  @param  string  $opt    option key
  237.      *  @param  string  $value  opeion value
  238.      *  @return bool 
  239.      *  @see http://jp.php.net/manual/memcached.setoption.php
  240.      */
  241.     public function setMemcachedOption($opt$value)
  242.     {
  243.         return $this->m->setOption($opt$value);
  244.     }
  245.  
  246.     /**
  247.      *  get option of pecl::memcached directly
  248.      *
  249.      *  @access public
  250.      *  @param  string  $opt    option key
  251.      *  @return mixed 
  252.      *  @see http://jp.php.net/manual/memcached.getoption.php
  253.      */
  254.     public function getMemcachedOption($opt)
  255.     {
  256.         return $this->m->getOption($opt);
  257.     }
  258.  
  259.     /**
  260.      *  ネームスペースからキャッシュキーを生成する
  261.      *
  262.      *  @access private
  263.      */
  264.     private function _getCacheKey($namespace$key)
  265.     {
  266.         $namespace $this->getNamespace($namespace);
  267.  
  268.         $key str_replace(":""_"$key);
  269.         $cache_key $namespace "::" $key;
  270.         if (strlen($cache_key250{
  271.             return null;
  272.         }
  273.         return $cache_key;
  274.     }
  275. }

Documentation generated on Fri, 11 Nov 2011 03:58:41 +0900 by phpDocumentor 1.4.3