More tidying.
[yaffs-website] / vendor / drush / drush / includes / cache.inc
1 <?php
2
3 /**
4  * @file
5  * Drush cache API
6  *
7  * Provides a cache API for drush core and commands, forked from Drupal 7.
8  *
9  * The default storage backend uses the plain text files to store serialized php
10  * objects, which can be extended or replaced by setting the cache-default-class
11  * option in drushrc.php.
12  */
13
14 use Drush\Log\LogLevel;
15
16 /**
17  * Indicates that the item should never be removed unless explicitly selected.
18  *
19  * The item may be removed using cache_clear_all() with a cache ID.
20  */
21 define('DRUSH_CACHE_PERMANENT', 0);
22
23 /**
24  * Indicates that the item should be removed at the next general cache wipe.
25  */
26 define('DRUSH_CACHE_TEMPORARY', -1);
27
28 /**
29  * Get the cache object for a cache bin.
30  *
31  * By default, this returns an instance of the \Drush\Cache\FileCache class.
32  * Classes implementing \Drush\Cache\CacheInterface can register themselves
33  * both as a default implementation and for specific bins.
34  *
35  * @see \Drush\Cache\CacheInterface
36  *
37  * @param string $bin
38  *   The cache bin for which the cache object should be returned.
39  *
40  * @return \Drush\Cache\CacheInterface
41  *   The cache object associated with the specified bin.
42  */
43 function _drush_cache_get_object($bin) {
44   static $cache_objects;
45
46   if (!isset($cache_objects[$bin])) {
47     $class = drush_get_option('cache-class-' . $bin, NULL);
48     if (!isset($class)) {
49       $class = drush_get_option('cache-default-class', '\Drush\Cache\JSONCache');
50     }
51     $cache_objects[$bin] = new $class($bin);
52   }
53   return $cache_objects[$bin];
54 }
55
56 /**
57  * Return data from the persistent cache.
58  *
59  * Data may be stored as either plain text or as serialized data.
60  * _drush_cache_get() will automatically return unserialized
61  * objects and arrays.
62  *
63  * @param string $cid
64  *   The cache ID of the data to retrieve.
65  * @param string $bin
66  *   The cache bin to store the data in.
67  *
68  * @return
69  *   The cache or FALSE on failure.
70  *
71  */
72 function drush_cache_get($cid, $bin = 'default') {
73   $ret = _drush_cache_get_object($bin)->get($cid);
74   $mess = $ret ? "HIT" : "MISS";
75   drush_log(dt("Cache !mess cid: !cid", array('!mess' => $mess, '!cid' => $cid)), LogLevel::DEBUG);
76   return $ret;
77 }
78
79 /**
80  * Return data from the persistent cache when given an array of cache IDs.
81  *
82  * @param array $cids
83  *   An array of cache IDs for the data to retrieve. This is passed by
84  *   reference, and will have the IDs successfully returned from cache removed.
85  * @param string $bin
86  *   The cache bin where the data is stored.
87  *
88  * @return
89  *   An array of the items successfully returned from cache indexed by cid.
90  */
91 function drush_cache_get_multiple(array &$cids, $bin = 'default') {
92   return _drush_cache_get_object($bin)->getMultiple($cids);
93 }
94
95 /**
96  * Store data in the persistent cache.
97  *
98  * @param string $cid
99  *   The cache ID of the data to store.
100  *
101  * @param $data
102  *   The data to store in the cache.
103  * @param string $bin
104  *   The cache bin to store the data in.
105  * @param $expire
106  *   One of the following values:
107  *   - DRUSH_CACHE_PERMANENT: Indicates that the item should never be removed
108  *     unless explicitly told to using drush_cache_clear_all() with a cache ID.
109  *   - DRUSH_CACHE_TEMPORARY: Indicates that the item should be removed at
110  *     the next general cache wipe.
111  *   - A Unix timestamp: Indicates that the item should be kept at least until
112  *     the given time, after which it behaves like DRUSH_CACHE_TEMPORARY.
113  *
114  * @return bool
115  */
116 function drush_cache_set($cid, $data, $bin = 'default', $expire = DRUSH_CACHE_PERMANENT) {
117   $ret = _drush_cache_get_object($bin)->set($cid, $data, $expire);
118   if ($ret) drush_log(dt("Cache SET cid: !cid", array('!cid' => $cid)), LogLevel::DEBUG);
119   return $ret;
120 }
121
122 /**
123  * Expire data from the cache.
124  *
125  * If called without arguments, expirable entries will be cleared from all known
126  * cache bins.
127  *
128  * @param string $cid
129  *   If set, the cache ID to delete. Otherwise, all cache entries that can
130  *   expire are deleted.
131  * @param string $bin
132  *   If set, the bin $bin to delete from. Mandatory
133  *   argument if $cid is set.
134  * @param bool $wildcard
135  *   If $wildcard is TRUE, cache IDs starting with $cid are deleted in
136  *   addition to the exact cache ID specified by $cid.  If $wildcard is
137  *   TRUE and $cid is '*' then the entire bin $bin is emptied.
138  */
139 function drush_cache_clear_all($cid = NULL, $bin = 'default', $wildcard = FALSE) {
140   if (!isset($cid) && !isset($bin)) {
141     foreach (drush_cache_get_bins() as $bin) {
142       _drush_cache_get_object($bin)->clear();
143     }
144     return;
145   }
146   return _drush_cache_get_object($bin)->clear($cid, $wildcard);
147 }
148
149 /**
150  * Check if a cache bin is empty.
151  *
152  * A cache bin is considered empty if it does not contain any valid data for any
153  * cache ID.
154  *
155  * @param $bin
156  *   The cache bin to check.
157  *
158  * @return
159  *   TRUE if the cache bin specified is empty.
160  */
161 function _drush_cache_is_empty($bin) {
162   return _drush_cache_get_object($bin)->isEmpty();
163 }
164
165 /**
166  * Return drush cache bins and any bins added by hook_drush_flush_caches().
167  */
168 function drush_cache_get_bins() {
169   $drush = array('default');
170   return array_merge(drush_command_invoke_all('drush_flush_caches'), $drush);
171 }
172
173 /**
174  * Create a cache id from a given prefix, contexts, and additional parameters.
175  *
176  * @param prefix
177  *   A human readable cid prefix that will not be hashed.
178  * @param contexts
179  *   Array of drush contexts that will be used to build a unique hash.
180  * @param params
181  *   Array of any addition parameters to be hashed.
182  *
183  * @return
184  *   A cache id string.
185  */
186 function drush_get_cid($prefix, $contexts = array(), $params = array()) {
187   $cid = array();
188
189   foreach ($contexts as $context) {
190     $c = drush_get_context($context);
191     if (!empty($c)) {
192       $cid[] = is_scalar($c) ? $c : serialize($c);
193     }
194   }
195
196   foreach ($params as $param) {
197     $cid[] = $param;
198   }
199
200   return DRUSH_VERSION . '-' . $prefix . '-' . md5(implode("", $cid));
201 }