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