CacheEngine.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  55. * Write value for a key into cache
  56. *
  57. * @param string $key Identifier for the data
  58. * @param mixed $value Data to be cached
  59. * @param mixed $duration How long to cache for.
  60. * @return boolean True if the data was successfully cached, false on failure
  61. */
  62. abstract public function write($key, $value, $duration);
  63. /**
  64. * Read a key from the cache
  65. *
  66. * @param string $key Identifier for the data
  67. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  68. */
  69. abstract public function read($key);
  70. /**
  71. * Increment a number under the key and return incremented value
  72. *
  73. * @param string $key Identifier for the data
  74. * @param integer $offset How much to add
  75. * @return New incremented value, false otherwise
  76. */
  77. abstract public function increment($key, $offset = 1);
  78. /**
  79. * Decrement a number under the key and return decremented value
  80. *
  81. * @param string $key Identifier for the data
  82. * @param integer $offset How much to subtract
  83. * @return New incremented value, false otherwise
  84. */
  85. abstract public function decrement($key, $offset = 1);
  86. /**
  87. * Delete a key from the cache
  88. *
  89. * @param string $key Identifier for the data
  90. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  91. */
  92. abstract public function delete($key);
  93. /**
  94. * Delete all keys from the cache
  95. *
  96. * @param boolean $check if true will check expiration, otherwise delete all
  97. * @return boolean True if the cache was successfully cleared, false otherwise
  98. */
  99. abstract public function clear($check);
  100. /**
  101. * Cache Engine settings
  102. *
  103. * @return array settings
  104. */
  105. public function settings() {
  106. return $this->settings;
  107. }
  108. /**
  109. * Generates a safe key for use with cache engine storage engines.
  110. *
  111. * @param string $key the key passed over
  112. * @return mixed string $key or false
  113. */
  114. public function key($key) {
  115. if (empty($key)) {
  116. return false;
  117. }
  118. $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
  119. return $key;
  120. }
  121. }