Guest User

Untitled

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