CacheEngine.php 3.6 KB

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