fastcacheitem.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @file
  4. * fastcacheitem Class.
  5. */
  6. class fastcacheitem {
  7. public $persist = FALSE;
  8. public $changed = FALSE;
  9. public $locked = FALSE;
  10. public $bin;
  11. private $data;
  12. /**
  13. * Construct with a DrupalCacheInterface object
  14. * that comes from a real cache storage.
  15. */
  16. public function __construct($binary, $cache = NULL) {
  17. if (isset($cache)) {
  18. $this->data = $cache->data;
  19. }
  20. else {
  21. $this->data = array();
  22. }
  23. $this->bin = $binary;
  24. }
  25. /**
  26. * Aux starsWith string.
  27. */
  28. private function startsWith($haystack, $needle) {
  29. return $needle === "" || strpos($haystack, $needle) === 0;
  30. }
  31. /**
  32. * Get the global contents of this cache.
  33. * Used to be sent to a real cache
  34. * storage.
  35. */
  36. public function rawdata() {
  37. return $this->data;
  38. }
  39. /**
  40. * Set a value in cache.
  41. *
  42. * @param mixed $key
  43. * @param mixed $value
  44. */
  45. public function data_set($key, $value) {
  46. $container = new stdClass();
  47. $container->data = $value;
  48. $this->data[$key] = $container;
  49. }
  50. /**
  51. * Retrieve a value from cache.
  52. *
  53. * @param mixed $key
  54. * @return bool|stdClass
  55. */
  56. public function data_get($key) {
  57. if (isset($this->data[$key])) {
  58. return $this->data[$key];
  59. }
  60. return FALSE;
  61. }
  62. /**
  63. * Clear a cache item.
  64. *
  65. * @param string $key
  66. * If set, the cache ID or an array of cache IDs. Otherwise, all cache entries that
  67. ** can expire are deleted. The $wildcard argument will be ignored if set to NULL.
  68. * @param bool $wildcard
  69. * If TRUE, the $cid argument must contain a string value and cache
  70. * IDs starting with $cid are deleted in addition to the exact cache
  71. * ID specified by $cid. If $wildcard is TRUE and $cid is '*', the entire cache is emptied.
  72. */
  73. public function clear($key, $wildcard = FALSE) {
  74. if (!isset($key)) {
  75. if (empty($this->data)) {
  76. return;
  77. }
  78. else {
  79. $this->data = array();
  80. }
  81. }
  82. elseif (isset($key) && $wildcard === FALSE) {
  83. unset($this->data[$key]);
  84. }
  85. else {
  86. if ($key == '*') {
  87. // Completely reset this binary.
  88. unset($this->data);
  89. $this->data = array();
  90. }
  91. else {
  92. // Only reset items that start with $key.
  93. foreach ($this->data as $k => $v) {
  94. if ($this->startsWith($k, $key)) {
  95. unset($this->data[$k]);
  96. }
  97. }
  98. }
  99. }
  100. $this->persist = TRUE;
  101. }
  102. }