7 * Provides a cache API for drush core and commands, forked from Drupal 7.
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.
15 use Drush\Log\LogLevel;
18 * Indicates that the item should never be removed unless explicitly selected.
20 * The item may be removed using cache_clear_all() with a cache ID.
22 define('DRUSH_CACHE_PERMANENT', 0);
25 * Indicates that the item should be removed at the next general cache wipe.
27 define('DRUSH_CACHE_TEMPORARY', -1);
30 * Get the cache object for a cache bin.
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.
36 * @see \Drush\Cache\CacheInterface
39 * The cache bin for which the cache object should be returned.
41 * @return \Drush\Cache\CacheInterface
42 * The cache object associated with the specified bin.
44 function _drush_cache_get_object($bin) {
45 static $cache_objects;
47 if (!isset($cache_objects[$bin])) {
48 $class = drush_get_option('cache-class-' . $bin, NULL);
50 $class = drush_get_option('cache-default-class', '\Drush\Cache\JSONCache');
52 $cache_objects[$bin] = new $class($bin);
54 return $cache_objects[$bin];
58 * Return data from the persistent cache.
60 * Data may be stored as either plain text or as serialized data.
61 * _drush_cache_get() will automatically return unserialized
65 * The cache ID of the data to retrieve.
67 * The cache bin to store the data in.
70 * The cache or FALSE on failure.
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);
81 * Return data from the persistent cache when given an array of cache IDs.
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.
87 * The cache bin where the data is stored.
90 * An array of the items successfully returned from cache indexed by cid.
92 function drush_cache_get_multiple(array &$cids, $bin = 'default') {
93 return _drush_cache_get_object($bin)->getMultiple($cids);
97 * Store data in the persistent cache.
100 * The cache ID of the data to store.
103 * The data to store in the cache.
105 * The cache bin to store the data in.
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.
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);
124 * Expire data from the cache.
126 * If called without arguments, expirable entries will be cleared from all known
130 * If set, the cache ID to delete. Otherwise, all cache entries that can
131 * expire are deleted.
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.
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();
147 return _drush_cache_get_object($bin)->clear($cid, $wildcard);
151 * Check if a cache bin is empty.
153 * A cache bin is considered empty if it does not contain any valid data for any
157 * The cache bin to check.
160 * TRUE if the cache bin specified is empty.
162 function _drush_cache_is_empty($bin) {
163 return _drush_cache_get_object($bin)->isEmpty();
167 * Return drush cache bins and any bins added by hook_drush_flush_caches().
169 function drush_cache_get_bins() {
170 $drush = ['default'];
172 // return array_merge(drush_command_invoke_all('drush_flush_caches'), $drush);
176 * Create a cache id from a given prefix, contexts, and additional parameters.
179 * A human readable cid prefix that will not be hashed.
181 * Array of drush contexts that will be used to build a unique hash.
183 * Array of any addition parameters to be hashed.
188 function drush_get_cid($prefix, $contexts = [], $params = []) {
191 foreach ($contexts as $context) {
192 $c = drush_get_context($context);
194 $cid[] = is_scalar($c) ? $c : serialize($c);
198 foreach ($params as $param) {
202 return Drush::getVersion() . '-' . $prefix . '-' . md5(implode("", $cid));