-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
235 lines (220 loc) · 6.88 KB
/
functions.php
File metadata and controls
235 lines (220 loc) · 6.88 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* MilliCache API Functions
*
* @link https://www.millipress.com
* @since 1.0.0
*
* @package MilliCache
* @author Philipp Wellmer <hello@millipress.com>
*/
! defined( 'ABSPATH' ) && exit;
if ( ! function_exists( 'millicache' ) ) {
/**
* Get the MilliCache Engine instance for advanced usage.
*
* This is an advanced method that provides direct access to the Engine
* singleton. For common tasks, prefer the simpler helper functions:
*
* - millicache_clear_cache() - Clear cache by flags, post-IDs, or URLs
* - millicache_add_flag() - Add a cache flag to the current request
* - millicache_set_ttl() - Set cache TTL for the current request
*
* Use millicache() when you need access to Engine methods not exposed
* via helper functions or for complex chained operations.
*
* Example usage:
*
* // Access storage backend directly
* millicache()->storage()->get_status();
*
* // Read configuration
* millicache()->config()->get('storage.backend');
*
* // Chain multiple invalidation operations
* millicache()->clear()->posts([1, 2])->flags(['custom'])->execute_queue();
*
* @since 1.0.0
*
* @return \MilliCache\Engine The MilliCache Engine instance.
*/
function millicache(): \MilliCache\Engine {
return \MilliCache\Engine::instance();
}
}
/**
* Clear cache by given Flags, Post-IDs or URLs.
*
* @since 1.0.0
*
* @param string|array<string|int> $targets The targets (Flags, Post-IDs or URLs) to clear the cache for.
* @param bool $expire Expire cache if set to true, or delete by default.
* @return void
*/
function millicache_clear_cache( $targets, bool $expire = false ): void {
millicache()->clear()->targets( $targets, $expire );
}
/**
* Clear cache by given URLs.
*
* @since 1.0.0
*
* @param string|array<string> $urls A string or array of URLs to execute.
* @param bool $expire Expire cache if set to true, or delete by default.
* @return void
*/
function millicache_clear_cache_by_urls( $urls, bool $expire = false ): void {
millicache()->clear()->urls( $urls, $expire );
}
/**
* Expire caches by post id.
*
* @since 1.0.0
*
* @param int|array<int> $post_ids The post-IDs to expire.
* @param bool $expire Expire cache if set to true, or delete by default.
* @return void
*/
function millicache_clear_cache_by_post_ids( $post_ids, bool $expire = false ): void {
millicache()->clear()->posts( $post_ids, $expire );
}
/**
* Clears cache by given flags.
*
* @since 1.0.0
*
* @param string|array<string> $flags A string or array of flags to expire.
* @param bool $expire Expire cache if set to true, or delete by default.
* @param bool $add_prefix Add the flag prefix to the flags.
* @return void
*/
function millicache_clear_cache_by_flags( $flags, bool $expire = false, bool $add_prefix = true ): void {
millicache()->clear()->flags( $flags, $expire, $add_prefix );
}
/**
* Clear the full cache of a given website.
*
* @since 1.0.0
*
* @param int|array<int> $site_ids The site IDs to clear.
* @param int|null $network_id The network ID.
* @param bool $expire Expire cache if set to true, or delete by default.
* @return void
*/
function millicache_clear_cache_by_site_ids( $site_ids = null, ?int $network_id = null, bool $expire = false ): void {
millicache()->clear()->sites( $site_ids, $network_id, $expire );
}
/**
* Clear the full cache of each site in a given network.
*
* @since 1.0.0
*
* @param int|null $network_id The network ID.
* @param bool $expire Expire cache.
* @return void
*/
function millicache_clear_cache_by_network_id( ?int $network_id = null, bool $expire = false ): void {
millicache()->clear()->networks( $network_id, $expire );
}
/**
* Reset the complete cache.
*
* @since 1.0.0
*
* @param bool $expire Expire cache.
* @return void
*/
function millicache_reset_cache( bool $expire = false ): void {
millicache()->clear()->all( $expire );
}
/**
* Add a flag to the current request.
*
* Flags are labels attached to cache entries that allow for efficient
* cache clearing. For example, all pages tagged with "post:123" can
* be cleared simultaneously.
*
* @since 1.0.0
*
* @param string $flag The flag name (e.g., 'post:123', 'custom-flag').
* @return void
*/
function millicache_add_flag( string $flag ): void {
millicache()->flags()->add( $flag );
}
/**
* Remove a flag from the current request.
*
* @since 1.0.0
*
* @param string $flag The flag name to remove.
* @return void
*/
function millicache_remove_flag( string $flag ): void {
millicache()->flags()->remove( $flag );
}
/**
* Get the prefix for flags (site:network: or empty).
*
* In multisite environments, flags are automatically prefixed with
* site and network IDs. This function returns that prefix.
*
* @since 1.0.0
*
* @param int|string|null $site_id Site ID (null for current).
* @param int|string|null $network_id Network ID (null for current).
* @return string The prefix string (empty string for non-multisite).
*/
function millicache_get_flag_prefix( $site_id = null, $network_id = null ): string {
return millicache()->flags()->get_prefix( $site_id, $network_id );
}
/**
* Prefix an array of flags with site/network prefix.
*
* In multisite environments, this adds the site and network ID prefix
* to each flag. Useful when you need to manually construct prefixed flags.
*
* @since 1.0.0
*
* @param string|array<string> $flags Flags to prefix (string or array).
* @param int|string|null $site_id Site ID (null for current).
* @param int|string|null $network_id Network ID (null for current).
* @return array<string> Array of prefixed flags.
*/
function millicache_prefix_flags( $flags, $site_id = null, $network_id = null ): array {
return millicache()->flags()->prefix( $flags, $site_id, $network_id );
}
/**
* Override the cache TTL (time-to-live) for the current request.
*
* This allows dynamic control of how long content is cached. Useful for
* setting different cache durations based on content type, user role or
* other runtime conditions.
*
* Example: Cache homepage for 1 hour, product pages for 5 minutes
*
* @since 1.0.0
*
* @param int $ttl Time-to-live in seconds (must be positive).
* @return void
*/
function millicache_set_ttl( int $ttl ): void {
millicache()->options()->set_ttl( $ttl );
}
/**
* Override the cache grace period for the current request.
*
* The grace period allows serving stale cache while regenerating in the
* background. This prevents cache stampedes and ensures consistent
* performance during cache regeneration.
*
* Example: Allow serving up to 1-hour-old cache while regenerating fresh content
*
* @since 1.0.0
*
* @param int $grace Grace period in seconds (must be non-negative, 0 to disable).
* @return void
*/
function millicache_set_grace( int $grace ): void {
millicache()->options()->set_grace( $grace );
}