CacheEngine.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @package Cake.Cache
  12. * @since CakePHP(tm) v 1.2.0.4933
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. /**
  16. * Storage engine for CakePHP caching
  17. *
  18. * @package Cake.Cache
  19. */
  20. abstract class CacheEngine {
  21. /**
  22. * Settings of current engine instance
  23. *
  24. * @var array
  25. */
  26. public $settings = array();
  27. /**
  28. * Initialize the cache engine
  29. *
  30. * Called automatically by the cache frontend
  31. *
  32. * @param array $settings Associative array of parameters for the engine
  33. * @return boolean True if the engine has been successfully initialized, false if not
  34. */
  35. public function init($settings = array()) {
  36. $this->settings = array_merge(
  37. array('prefix' => 'cake_', 'duration' => 3600, 'probability' => 100),
  38. $this->settings,
  39. $settings
  40. );
  41. if (!is_numeric($this->settings['duration'])) {
  42. $this->settings['duration'] = strtotime($this->settings['duration']) - time();
  43. }
  44. return true;
  45. }
  46. /**
  47. * Garbage collection
  48. *
  49. * Permanently remove all expired and deleted data
  50. * @return void
  51. */
  52. public function gc() { }
  53. /**
  54. * Write value for a key into cache
  55. *
  56. * @param string $key Identifier for the data
  57. * @param mixed $value Data to be cached
  58. * @param mixed $duration How long to cache for.
  59. * @return boolean True if the data was successfully cached, false on failure
  60. */
  61. abstract public function write($key, $value, $duration);
  62. /**
  63. * Read a key from the cache
  64. *
  65. * @param string $key Identifier for the data
  66. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  67. */
  68. abstract public function read($key);
  69. /**
  70. * Increment a number under the key and return incremented value
  71. *
  72. * @param string $key Identifier for the data
  73. * @param integer $offset How much to add
  74. * @return New incremented value, false otherwise
  75. */
  76. abstract public function increment($key, $offset = 1);
  77. /**
  78. * Decrement a number under the key and return decremented value
  79. *
  80. * @param string $key Identifier for the data
  81. * @param integer $offset How much to subtract
  82. * @return New incremented value, false otherwise
  83. */
  84. abstract public function decrement($key, $offset = 1);
  85. /**
  86. * Delete a key from the cache
  87. *
  88. * @param string $key Identifier for the data
  89. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  90. */
  91. abstract public function delete($key);
  92. /**
  93. * Delete all keys from the cache
  94. *
  95. * @param boolean $check if true will check expiration, otherwise delete all
  96. * @return boolean True if the cache was successfully cleared, false otherwise
  97. */
  98. abstract public function clear($check);
  99. /**
  100. * Cache Engine settings
  101. *
  102. * @return array settings
  103. */
  104. public function settings() {
  105. return $this->settings;
  106. }
  107. /**
  108. * Generates a safe key for use with cache engine storage engines.
  109. *
  110. * @param string $key the key passed over
  111. * @return mixed string $key or false
  112. */
  113. public function key($key) {
  114. if (empty($key)) {
  115. return false;
  116. }
  117. $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
  118. return $key;
  119. }
  120. }