| @@ -102,5 +102,37 @@ | ||
| 102 | 102 | |
| 103 | 103 | public function delete_transient( $transient ) { |
| 104 | 104 | return delete_transient( $transient ); |
| 105 | 105 | } |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Versioned-key incrementor. Reading the current version lets callers build | |
| 109 | + * cache keys like "{namespace}_v{N}_..."; bumping the version makes every | |
| 110 | + * key in that namespace unreachable in one update. | |
| 111 | + * | |
| 112 | + * The canonical value lives in wp_options so invalidation persists across | |
| 113 | + * requests even on sites with no external object cache (where wp_cache_* | |
| 114 | + * is per-request in-memory only). wp_cache_* is still used as a | |
| 115 | + * within-request memoization layer. | |
| 116 | + */ | |
| 117 | + public function get_cache_version( $namespace, $group = 'betterdocs' ) { | |
| 118 | + $cache_key = $namespace . '_version'; | |
| 119 | + $option_key = 'bd_cache_v_' . $namespace; | |
| 120 | + $v = wp_cache_get( $cache_key, $group ); | |
| 121 | + if ( false === $v ) { | |
| 122 | + $v = (int) get_option( $option_key, 1 ); | |
| 123 | + if ( $v < 1 ) { | |
| 124 | + $v = 1; | |
| 125 | + } | |
| 126 | + wp_cache_set( $cache_key, $v, $group, 0 ); | |
| 127 | + } | |
| 128 | + return (int) $v; | |
| 129 | + } | |
| 130 | + | |
| 131 | + public function bump_cache_version( $namespace, $group = 'betterdocs' ) { | |
| 132 | + $cache_key = $namespace . '_version'; | |
| 133 | + $option_key = 'bd_cache_v_' . $namespace; | |
| 134 | + $new = (int) get_option( $option_key, 1 ) + 1; | |
| 135 | + update_option( $option_key, $new, false ); | |
| 136 | + wp_cache_set( $cache_key, $new, $group, 0 ); | |
| 137 | + } | |
| 106 | 138 | } |