Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Cache;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Provides methods to use a cache backend while respecting a 'use caches' flag.
|
Chris@0
|
7 */
|
Chris@0
|
8 trait UseCacheBackendTrait {
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Cache backend instance.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @var \Drupal\Core\Cache\CacheBackendInterface
|
Chris@0
|
14 */
|
Chris@0
|
15 protected $cacheBackend;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Flag whether caches should be used or skipped.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var bool
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $useCaches = TRUE;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Fetches from the cache backend, respecting the use caches flag.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @param string $cid
|
Chris@0
|
28 * The cache ID of the data to retrieve.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return object|false
|
Chris@0
|
31 * The cache item or FALSE on failure.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @see \Drupal\Core\Cache\CacheBackendInterface::get()
|
Chris@0
|
34 */
|
Chris@0
|
35 protected function cacheGet($cid) {
|
Chris@0
|
36 if ($this->useCaches && $this->cacheBackend) {
|
Chris@0
|
37 return $this->cacheBackend->get($cid);
|
Chris@0
|
38 }
|
Chris@0
|
39 return FALSE;
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Stores data in the persistent cache, respecting the use caches flag.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @param string $cid
|
Chris@0
|
46 * The cache ID of the data to store.
|
Chris@0
|
47 * @param mixed $data
|
Chris@0
|
48 * The data to store in the cache.
|
Chris@0
|
49 * Some storage engines only allow objects up to a maximum of 1MB in size to
|
Chris@0
|
50 * be stored by default. When caching large arrays or similar, take care to
|
Chris@0
|
51 * ensure $data does not exceed this size.
|
Chris@0
|
52 * @param int $expire
|
Chris@0
|
53 * One of the following values:
|
Chris@0
|
54 * - CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should
|
Chris@0
|
55 * not be removed unless it is deleted explicitly.
|
Chris@0
|
56 * - A Unix timestamp: Indicates that the item will be considered invalid
|
Chris@0
|
57 * after this time, i.e. it will not be returned by get() unless
|
Chris@0
|
58 * $allow_invalid has been set to TRUE. When the item has expired, it may
|
Chris@0
|
59 * be permanently deleted by the garbage collector at any time.
|
Chris@0
|
60 * @param array $tags
|
Chris@0
|
61 * An array of tags to be stored with the cache item. These should normally
|
Chris@0
|
62 * identify objects used to build the cache item, which should trigger
|
Chris@0
|
63 * cache invalidation when updated. For example if a cached item represents
|
Chris@0
|
64 * a node, both the node ID and the author's user ID might be passed in as
|
Chris@0
|
65 * tags. For example array('node' => array(123), 'user' => array(92)).
|
Chris@0
|
66 *
|
Chris@0
|
67 * @see \Drupal\Core\Cache\CacheBackendInterface::set()
|
Chris@0
|
68 */
|
Chris@0
|
69 protected function cacheSet($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
|
Chris@0
|
70 if ($this->cacheBackend && $this->useCaches) {
|
Chris@0
|
71 $this->cacheBackend->set($cid, $data, $expire, $tags);
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 }
|