Cake/Cache/CacheEngine.php

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 /**
17 * Storage engine for CakePHP caching
18 *
19 * @package Cake.Cache
20 */
21 abstract class CacheEngine {
22  
23 /**
24 * Settings of current engine instance
25 *
26 * @var array
27 */
28 public $settings = array();
29  
30 /**
31 * Initialize the cache engine
32 *
33 * Called automatically by the cache frontend
34 *
35 * @param array $settings Associative array of parameters for the engine
36 * @return boolean True if the engine has been successfully initialized, false if not
37 */
38 public function init($settings = array()) {
39 $this->settings = array_merge(
40 array('prefix' => 'cake_', 'duration' => 3600, 'probability' => 100),
41 $this->settings,
42 $settings
43 );
44 if (!is_numeric($this->settings['duration'])) {
45 $this->settings['duration'] = strtotime($this->settings['duration']) - time();
46 }
47 return true;
48 }
49  
50 /**
51 * Garbage collection
52 *
53 * Permanently remove all expired and deleted data
54 * @return void
55 */
56 public function gc() {
57 }
58  
59 /**
60 * Write value for a key into cache
61 *
62 * @param string $key Identifier for the data
63 * @param mixed $value Data to be cached
64 * @param mixed $duration How long to cache for.
65 * @return boolean True if the data was successfully cached, false on failure
66 */
67 abstract public function write($key, $value, $duration);
68  
69 /**
70 * Read a key from the cache
71 *
72 * @param string $key Identifier for the data
73 * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
74 */
75 abstract public function read($key);
76  
77 /**
78 * Increment a number under the key and return incremented value
79 *
80 * @param string $key Identifier for the data
81 * @param integer $offset How much to add
82 * @return New incremented value, false otherwise
83 */
84 abstract public function increment($key, $offset = 1);
85  
86 /**
87 * Decrement a number under the key and return decremented value
88 *
89 * @param string $key Identifier for the data
90 * @param integer $offset How much to subtract
91 * @return New incremented value, false otherwise
92 */
93 abstract public function decrement($key, $offset = 1);
94  
95 /**
96 * Delete a key from the cache
97 *
98 * @param string $key Identifier for the data
99 * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
100 */
101 abstract public function delete($key);
102  
103 /**
104 * Delete all keys from the cache
105 *
106 * @param boolean $check if true will check expiration, otherwise delete all
107 * @return boolean True if the cache was successfully cleared, false otherwise
108 */
109 abstract public function clear($check);
110  
111 /**
112 * Cache Engine settings
113 *
114 * @return array settings
115 */
116 public function settings() {
117 return $this->settings;
118 }
119  
120 /**
121 * Generates a safe key for use with cache engine storage engines.
122 *
123 * @param string $key the key passed over
124 * @return mixed string $key or false
125 */
126 public function key($key) {
127 if (empty($key)) {
128 return false;
129 }
130 $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
131 return $key;
132 }
133  
134 }
135  
136