ElggStaticVariableCache.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * \ElggStaticVariableCache
  4. * Dummy cache which stores values in a static array. Using this makes future
  5. * replacements to other caching back ends (eg memcache) much easier.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage Cache
  9. */
  10. class ElggStaticVariableCache extends \ElggSharedMemoryCache {
  11. /**
  12. * The cache.
  13. *
  14. * @var array
  15. */
  16. private static $__cache;
  17. /**
  18. * Create the variable cache.
  19. *
  20. * This function creates a variable cache in a static variable in
  21. * memory, optionally with a given namespace (to avoid overlap).
  22. *
  23. * @param string $namespace The namespace for this cache to write to.
  24. * @warning namespaces of the same name are shared!
  25. */
  26. public function __construct($namespace = 'default') {
  27. $this->setNamespace($namespace);
  28. $this->clear();
  29. }
  30. /**
  31. * Save a key
  32. *
  33. * @param string $key Name
  34. * @param string $data Value
  35. *
  36. * @return boolean
  37. */
  38. public function save($key, $data) {
  39. $namespace = $this->getNamespace();
  40. \ElggStaticVariableCache::$__cache[$namespace][$key] = $data;
  41. return true;
  42. }
  43. /**
  44. * Load a key
  45. *
  46. * @param string $key Name
  47. * @param int $offset Offset
  48. * @param int $limit Limit
  49. *
  50. * @return string
  51. */
  52. public function load($key, $offset = 0, $limit = null) {
  53. $namespace = $this->getNamespace();
  54. if (isset(\ElggStaticVariableCache::$__cache[$namespace][$key])) {
  55. return \ElggStaticVariableCache::$__cache[$namespace][$key];
  56. }
  57. return false;
  58. }
  59. /**
  60. * Invalidate a given key.
  61. *
  62. * @param string $key Name
  63. *
  64. * @return bool
  65. */
  66. public function delete($key) {
  67. $namespace = $this->getNamespace();
  68. unset(\ElggStaticVariableCache::$__cache[$namespace][$key]);
  69. return true;
  70. }
  71. /**
  72. * Clears the cache for a particular namespace
  73. *
  74. * @return void
  75. */
  76. public function clear() {
  77. $namespace = $this->getNamespace();
  78. if (!isset(\ElggStaticVariableCache::$__cache)) {
  79. \ElggStaticVariableCache::$__cache = array();
  80. }
  81. \ElggStaticVariableCache::$__cache[$namespace] = array();
  82. }
  83. }