PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Caching/MemcachedStorage.php

https://github.com/bazo/Mokuji
PHP | 155 lines | 68 code | 35 blank | 52 comment | 14 complexity | 696838c5d1bdc85a96eb7480df5ac1b1 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Caching
  10. */
  11. /**
  12. * Memcached storage.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Caching
  16. */
  17. class MemcachedStorage extends Object implements ICacheStorage
  18. {
  19. /**#@+ @ignore internal cache structure */
  20. const META_CALLBACKS = 'callbacks';
  21. const META_DATA = 'data';
  22. const META_DELTA = 'delta';
  23. /**#@-*/
  24. /** @var Memcache */
  25. private $memcache;
  26. /** @var string */
  27. private $prefix;
  28. /**
  29. * Checks if Memcached extension is available.
  30. * @return bool
  31. */
  32. public static function isAvailable()
  33. {
  34. return extension_loaded('memcache');
  35. }
  36. public function __construct($host = 'localhost', $port = 11211, $prefix = '')
  37. {
  38. if (!self::isAvailable()) {
  39. throw new Exception("PHP extension 'memcache' is not loaded.");
  40. }
  41. $this->prefix = $prefix;
  42. $this->memcache = new Memcache;
  43. $this->memcache->connect($host, $port);
  44. }
  45. /**
  46. * Read from cache.
  47. * @param string key
  48. * @return mixed|NULL
  49. */
  50. public function read($key)
  51. {
  52. $key = $this->prefix . $key;
  53. $meta = $this->memcache->get($key);
  54. if (!$meta) return NULL;
  55. // meta structure:
  56. // array(
  57. // data => stored data
  58. // delta => relative (sliding) expiration
  59. // callbacks => array of callbacks (function, args)
  60. // )
  61. // verify dependencies
  62. if (!empty($meta[self::META_CALLBACKS]) && !Cache::checkCallbacks($meta[self::META_CALLBACKS])) {
  63. $this->memcache->delete($key);
  64. return NULL;
  65. }
  66. if (!empty($meta[self::META_DELTA])) {
  67. $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time());
  68. }
  69. return $meta[self::META_DATA];
  70. }
  71. /**
  72. * Writes item into the cache.
  73. * @param string key
  74. * @param mixed data
  75. * @param array dependencies
  76. * @return void
  77. */
  78. public function write($key, $data, array $dp)
  79. {
  80. if (!empty($dp[Cache::TAGS]) || isset($dp[Cache::PRIORITY]) || !empty($dp[Cache::ITEMS])) {
  81. throw new NotSupportedException('Tags, priority and dependent items are not supported by MemcachedStorage.');
  82. }
  83. $meta = array(
  84. self::META_DATA => $data instanceof Callback || $data instanceof Closure ? $data->__invoke() : $data,
  85. );
  86. $expire = 0;
  87. if (!empty($dp[Cache::EXPIRE])) {
  88. $expire = (int) $dp[Cache::EXPIRE];
  89. if (!empty($dp[Cache::SLIDING])) {
  90. $meta[self::META_DELTA] = $expire; // sliding time
  91. }
  92. }
  93. if (!empty($dp[Cache::CALLBACKS])) {
  94. $meta[self::META_CALLBACKS] = $dp[Cache::CALLBACKS];
  95. }
  96. $this->memcache->set($this->prefix . $key, $meta, 0, $expire);
  97. }
  98. /**
  99. * Removes item from the cache.
  100. * @param string key
  101. * @return void
  102. */
  103. public function remove($key)
  104. {
  105. $this->memcache->delete($this->prefix . $key);
  106. }
  107. /**
  108. * Removes items from the cache by conditions & garbage collector.
  109. * @param array conditions
  110. * @return void
  111. */
  112. public function clean(array $conds)
  113. {
  114. if (!empty($conds[Cache::ALL])) {
  115. $this->memcache->flush();
  116. } elseif (isset($conds[Cache::TAGS]) || isset($conds[Cache::PRIORITY])) {
  117. throw new NotSupportedException('Tags and priority is not supported by MemcachedStorage.');
  118. }
  119. }
  120. }