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