-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CommandCache.php
64 lines (57 loc) · 1.53 KB
/
CommandCache.php
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
<?php
namespace Drush\Cache;
use Consolidation\AnnotatedCommand\Cache\SimpleCacheInterface;
/**
* Command cache implementation.
*
* This wrapper implements a cache usable with the annotated-command
* library's command cache. It uses a Drush JSONCache for its back-end.
*/
class CommandCache implements SimpleCacheInterface
{
protected $cacheBackend;
public function __construct(CacheInterface $cacheBackend)
{
$this->cacheBackend = $cacheBackend;
}
/**
* Test for an entry from the cache
* @param string $key
* @return boolean
*/
public function has($key)
{
$cacheItem = $this->cacheBackend->get($key);
return $this->valid($cacheItem);
}
/**
* Get an entry from the cache
* @param string $key
* @return array
*/
public function get($key)
{
$cacheItem = $this->cacheBackend->get($key);
if (!$this->valid($cacheItem)) {
return [];
}
// TODO: FileCache::get() should just return the
// data element, not the entire cacheItem. Then we
// could make it implement SimpleCacheInterface & do
// away with this adapter class.
return $cacheItem->data;
}
/**
* Store an entry in the cache
* @param string $key
* @param array $data
*/
public function set($key, $data)
{
$this->cacheBackend->set($key, $data);
}
protected function valid($cacheItem)
{
return is_object($cacheItem) && isset($cacheItem->data);
}
}