forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.php
104 lines (91 loc) · 2.41 KB
/
cache.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
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
<?php
/**
* Caching logic for vip-go-ci. Stores data in-memory
* during run-time.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Get a specific item from in-memory cache based on
* $cache_id_arr if $data is null, or if $data is not null,
* add a specific item to cache.
*
* The data is stored in an associative array, with
* key being an array (or anything else) -- $cache_id_arr --,
* and used to identify the data up on retrieval.
*
* If the data being cached is an object, we make a copy of it,
* and then store it. When the cached data is being retrieved,
* we return a copy of the cached data.
*
* @param array|string $cache_id_arr Cache ID to use when caching data, or ask for data. Special string used to clear cache.
* @param mixed $data Data to cache, null if asking for data.
*
* @return mixed Newly or previously cached data on success, false when no data
* is available, true on successful caching.
*/
function vipgoci_cache(
array|string $cache_id_arr,
mixed $data = null
) :mixed {
global $vipgoci_cache_buffer;
/*
* Special invocation: Allow for
* the cache to be cleared.
*/
if (
( is_string(
$cache_id_arr
) )
&&
(
VIPGOCI_CACHE_CLEAR ===
$cache_id_arr
)
) {
$vipgoci_cache_buffer = array();
return true;
}
$cache_id = json_encode(
$cache_id_arr
);
if ( null === $data ) {
// Asking for data from cache, find and return if it exists.
if ( isset( $vipgoci_cache_buffer[ $cache_id ] ) ) {
$ret = $vipgoci_cache_buffer[ $cache_id ];
// If an object, copy and return the copy.
if ( is_object( $ret ) ) {
$ret = clone $ret;
}
return $ret;
} else {
return false;
}
} else {
/*
* Asking to save data in cache; save it and return the data.
*/
// If an object, copy, save it, and return the copy.
if ( is_object( $data ) ) {
$data = clone $data;
}
$vipgoci_cache_buffer[ $cache_id ] = $data;
return $data;
}
}
/**
* Support function for other functions
* that use the internal cache and need to indicate
* that information from the cache was used.
*
* @param mixed $cache_used If non-false value, will return string indicating
* that cache was used, else empty string.
*
* @return string Indication of cache usage.
*/
function vipgoci_cached_indication_str(
mixed $cache_used
) :string {
return ( false !== $cache_used ) ? ' (cached)' : '';
}