11<?php
22namespace WordPress_ToolKit ;
3+ use WordPress_ToolKit \Helpers \ArrayHelper ;
34
45/**
56 * A helper class for getting/setting values from WordPress object cache, if
67 * available.
78 *
9+ * @see https://github.com/dmhendricks/wordpress-toolkit/wiki/ObjectCache
810 * @since 0.1.0
911 */
1012class ObjectCache extends ToolKit {
@@ -15,65 +17,116 @@ class ObjectCache extends ToolKit {
1517 *
1618 * @param string $key Key value of cache to retrieve
1719 * @param function $callback Result to return/set if does not exist in cache
20+ * @param array $args An array of arguments
1821 * @return string Cached value of key
1922 * @since 0.1.0
2023 */
2124
22- public function get_object ( $ key = null , $ callback , $ object_cache_group = null , $ network_global = false , $ cache_disabled = false ) {
25+ public function get_object ( $ key , $ callback , $ args = [] ) {
2326
24- if ( !$ object_cache_group ) $ object_cache_group = self ::$ config ->get ( 'object_cache/group ' ) ? self ::$ config ->get ( 'object_cache/group ' ) : sanitize_title ( self ::$ config ->get ( 'data/Name ' ) );
25- if ( is_multisite () ) $ object_cache_group .= '_ ' . get_current_site ()->id ;
26- $ object_cache_expire = ( is_int ( self ::$ config ->get ( 'object_cache/expire_hours ' ) ) ? self ::$ config ->get ( 'object_cache/expire_hours ' ) : 24 ) * 3600 ; // Default to 24 hours
27+ $ args = ArrayHelper::set_default_atts ( [
28+ 'expire ' => self ::$ config ->get ( 'object_cache/expire ' ) ?: DAY_IN_SECONDS , // Expiration time for cache value or group
29+ 'group ' => self ::$ config ->get ( 'object_cache/group ' ) ?: sanitize_title ( self ::$ config ->get ( 'data/Name ' ) ),
30+ 'single ' => false , // Store as single key rather than group array
31+ 'network_global ' => false // Set to true to store cache value for entire network, rather than per sub-site
32+ ], $ args );
2733
2834 $ result = null ;
35+ $ result_group = null ;
2936
30- // Set key variable
31- $ object_cache_key = $ key . ( is_multisite () && !$ network_global && get_current_blog_id () ? '_ ' . get_current_blog_id () : '' );
32- $ cache_hit = false ;
37+ // Validate arguments
38+ $ args ['expire ' ] = intval ( $ args ['expire ' ] );
39+ $ args ['single ' ] = filter_var ( $ args ['single ' ], FILTER_VALIDATE_BOOLEAN );
40+ $ args ['network_global ' ] = filter_var ( $ args ['network_global ' ], FILTER_VALIDATE_BOOLEAN );
41+
42+ // Add site ID suffic to cache group if multisite
43+ if ( is_multisite () ) $ args ['group ' ] .= '_ ' . get_current_site ()->id ;
44+
45+ // Set key variable, appending blog ID if network_global is false
46+ $ object_cache_key = $ key . ( is_multisite () && !$ args ['network_global ' ] && get_current_blog_id () ? '_ ' . get_current_blog_id () : '' );
47+
48+ // Try to get key value from cache
49+ if ( $ args ['single ' ] ) {
50+
51+ // Store value in individual key
52+ $ result = unserialize ( wp_cache_get ( $ object_cache_key , $ args ['group ' ], false , $ cache_hit ) );
53+
54+ } else {
55+
56+ // Store value in array of values with group as key
57+ $ result_group = wp_cache_get ( $ args ['group ' ], $ args ['group ' ], false , $ cache_hit );
58+ $ result_group = $ cache_hit ? (array ) unserialize ( $ result_group ) : [];
59+
60+ if ( $ cache_hit && isset ( $ result_group [$ object_cache_key ] ) ) {
61+ $ result = $ result_group [$ object_cache_key ];
62+ } else {
63+ $ cache_hit = false ;
64+ }
3365
34- // Try to get the value of the cache
35- if ( !$ cache_disabled ) {
36- $ result = wp_cache_get ( $ object_cache_key , $ object_cache_group , false , $ cache_hit );
37- if ( $ result && is_serialized ( $ result ) ) $ result = unserialize ( $ result );
3866 }
3967
40- // If cache miss or caching is disabled , set & return the value from $callback()
68+ // If cache miss, set & return the value from $callback()
4169 if ( !$ cache_hit ) {
70+
4271 $ result = $ callback ();
43- if ( !$ cache_disabled ) wp_cache_set ( $ object_cache_key , ( is_array ( $ result ) || is_object ( $ result ) || is_bool ( $ result ) ? serialize ( $ result ) : $ result ), $ object_cache_group , $ object_cache_expire );
72+
73+ // Store cache key value pair
74+ if ( $ args ['single ' ] ) {
75+
76+ // If single, store cache value.
77+ wp_cache_set ( $ object_cache_key , serialize ( $ result ), $ args ['group ' ], $ args ['expire ' ] );
78+
79+ } else {
80+
81+ // Store cache value in group array to allow "flushing" of individual group
82+ $ result_group [$ object_cache_key ] = $ result ;
83+ wp_cache_set ( $ args ['group ' ], serialize ( $ result_group ), $ args ['group ' ], $ args ['expire ' ] );
84+
85+ }
86+
4487 }
4588
4689 return $ result ;
4790
4891 }
4992
5093 /**
51- * Flushes the object cache, if enabled. Parameters are not used but are
52- * when passed by 'publish_post' action hook.
94+ * Flushes the entire object cache
5395 *
54- * Example usage: Cache::flush();
55- *
56- * @param int $ID The ID of the post being published
57- * @param WP_Post The post object that is being published
58- * @return mixed Returns success as JSON string if called by AJAX,
59- * else true/false
96+ * @return bool True on success, false on error
6097 * @since 0.1.0
6198 */
62- public function flush ( $ ID = null , $ post = null ) {
63-
64- $ result = array ( 'success ' => true );
99+ public function flush () {
65100
66101 try {
67102 wp_cache_flush ();
68103 } catch ( Exception $ e ) {
69- $ result = array ( ' success ' => false , ' message ' => $ e -> getMessage ()) ;
104+ return false ;
70105 }
71106
72- if ( defined ( 'DOING_AJAX ' ) && DOING_AJAX ) {
73- echo json_encode ( $ result );
74- die ();
107+ return true ;
108+
109+ }
110+
111+ /**
112+ * Flushes (deletes) the key group from cache. This is a poor man's way of flushing
113+ * a single group rather than entire object cache through wp_cache_flush()
114+ *
115+ * @param string $group The name of the key group to flush (delete)
116+ * @return bool True on success, false on error
117+ * @since 0.4.0
118+ */
119+ public function flush_group ( $ cache_group = null ) {
120+
121+ $ cache_group = $ cache_group ?: self ::$ config ->get ( 'object_cache/group ' );
122+
123+ try {
124+ wp_cache_delete ( $ cache_group , $ cache_group );
125+ } catch ( Exception $ e ) {
126+ return false ;
75127 }
76- return $ result ['success ' ];
128+
129+ return true ;
77130
78131 }
79132
0 commit comments