-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache.php
160 lines (138 loc) · 4.5 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
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
<?php
/**
* This file contains the Geniem Cache class.
*/
namespace Geniem;
/**
* This class extends the Redis Object Cache for WordPress dropin
* with group caching functionalities.
*
* @package Geniem
*/
class GroupCache {
/**
* @var $redis_instance
*
* Holds the connection object.
*/
protected static $redis_instance;
/**
* A setter function for the connection object.
*
* @param object $redis_instance
*/
public static function set_redis_instance( $redis_instance ) {
self::$redis_instance = $redis_instance;
}
/**
* If a group key is set, pushes the cache key to the specified Redis list.
* This function is hooked to the cache setting.
* The value is not needed here, set a human readable timestamp instead.
*
* @param string $key The Redis cache key.
* @param any $value The cache data.
* @param string $group The Redis hash key.
*/
public static function add_to_group( $key, $value, $group ) {
if ( self::no_group_cache( $group, $key ) ) {
return;
}
$timestamp = date_i18n('F d, Y H:i');
// Set the data into a hash set grouped by the group key.
self::$redis_instance->hset( $group, $key, $timestamp );
}
/**
* If a group key is set, deletes the key from the specified Redis hash.
* This function is hooked to the cache deletion.
*
* @param string $key The Redis cache key.
* @param string $group The Redis cache group key.
*/
public static function delete_from_group( $key, $group ) {
// Ignore this group?
if ( self::no_group_cache( $group, $key ) ) {
return;
}
// HDEL deletes a single value from the group.
self::$redis_instance->hdel( $group, $key );
}
/**
* Delete all data related to a group key by first
* deleting all keys and then the Redis hash of the group.
*
* @param string $group The Redis hash key.
*/
public static function delete_group( $group ) {
global $wp_object_cache;
// Get all keys first.
$keys = self::$redis_instance->hkeys( $group );
if ( is_array( $keys ) ) {
// Delete all data related to keys.
foreach ( $keys as $key ) {
// Get the full key.
$full_key = $wp_object_cache->build_key( $key, $group );
// Delete the single key.
self::$redis_instance->del( $full_key );
}
}
// Delete the group and its content.
self::$redis_instance->del( $group );
}
/**
* Do we want to create a group cache for this group?
*
* @param string $group The cache group key.
* @param string $key The cache key.
* @return bool
*/
public static function no_group_cache( $group, $key ) {
// Group blacklist
$blacklist = apply_filters( 'geniem/cache/no_group_cache/blacklist', [
'posts',
'terms',
'options',
'transient',
'default',
'blog-details',
'blog-id-cache',
'blog-lookup',
'global-posts',
'networks',
'rss',
'sites',
'site-details',
'site-lookup',
'site-options',
'site-transient',
'users',
'useremail',
'userlogins',
'usermeta',
'user_meta',
'userslugs',
'terms',
'plugins',
'counts',
'comment',
'post_meta',
'post_tag_relationships',
'post_translations_relationships',
'language_relationships',
'term_language_relationships',
'term_translations_relationships',
'timeinfo',
'dustpress/rendered', // Dustpress
'acf', // Advanced Custom Fields
'dymoloader1.0', // Dynamic .mo loader
'stateless_post_meta', // wp stateless
'pll_count_posts', // polylang
'the_seo_framework', // seo framework
]);
$no_group_cache = (
empty( $group ) || // No group
in_array( $group, $blacklist, true ) || // Group is in blacklist
apply_filters( 'geniem/cache/no_group_cache', false, $group, $key ) // Failed specific check
);
return $no_group_cache;
}
}