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.
14 use Drush\Log\LogLevel;
17 * Indicates that the item should never be removed unless explicitly selected.
19 * The item may be removed using cache_clear_all() with a cache ID.
21 define('DRUSH_CACHE_PERMANENT', 0);
24 * Indicates that the item should be removed at the next general cache wipe.
26 define('DRUSH_CACHE_TEMPORARY', -1);
29 * Get the cache object for a cache bin.
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.
35 * @see \Drush\Cache\CacheInterface
38 * The cache bin for which the cache object should be returned.
40 * @return \Drush\Cache\CacheInterface
41 * The cache object associated with the specified bin.
43 function _drush_cache_get_object($bin) {
44 static $cache_objects;
46 if (!isset($cache_objects[$bin])) {
47 $class = drush_get_option('cache-class-' . $bin, NULL);
49 $class = drush_get_option('cache-default-class', '\Drush\Cache\JSONCache');
51 $cache_objects[$bin] = new $class($bin);
53 return $cache_objects[$bin];
57 * Return data from the persistent cache.
59 * Data may be stored as either plain text or as serialized data.
60 * _drush_cache_get() will automatically return unserialized
64 * The cache ID of the data to retrieve.
66 * The cache bin to store the data in.
69 * The cache or FALSE on failure.
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);
80 * Return data from the persistent cache when given an array of cache IDs.
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.
86 * The cache bin where the data is stored.
89 * An array of the items successfully returned from cache indexed by cid.
91 function drush_cache_get_multiple(array &$cids, $bin = 'default') {
92 return _drush_cache_get_object($bin)->getMultiple($cids);
96 * Store data in the persistent cache.
99 * The cache ID of the data to store.
102 * The data to store in the cache.
104 * The cache bin to store the data in.
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.
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);
123 * Expire data from the cache.
125 * If called without arguments, expirable entries will be cleared from all known
129 * If set, the cache ID to delete. Otherwise, all cache entries that can
130 * expire are deleted.
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.
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();
146 return _drush_cache_get_object($bin)->clear($cid, $wildcard);
150 * Check if a cache bin is empty.
152 * A cache bin is considered empty if it does not contain any valid data for any
156 * The cache bin to check.
159 * TRUE if the cache bin specified is empty.
161 function _drush_cache_is_empty($bin) {
162 return _drush_cache_get_object($bin)->isEmpty();
166 * Return drush cache bins and any bins added by hook_drush_flush_caches().
168 function drush_cache_get_bins() {
169 $drush = array('default');
170 return array_merge(drush_command_invoke_all('drush_flush_caches'), $drush);
174 * Create a cache id from a given prefix, contexts, and additional parameters.
177 * A human readable cid prefix that will not be hashed.
179 * Array of drush contexts that will be used to build a unique hash.
181 * Array of any addition parameters to be hashed.
186 function drush_get_cid($prefix, $contexts = array(), $params = array()) {
189 foreach ($contexts as $context) {
190 $c = drush_get_context($context);
192 $cid[] = is_scalar($c) ? $c : serialize($c);
196 foreach ($params as $param) {
200 return DRUSH_VERSION . '-' . $prefix . '-' . md5(implode("", $cid));