cache-compat.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Object Cache API functions missing from 3rd party object caches.
  4. *
  5. * @link https://developer.wordpress.org/reference/classes/wp_object_cache/
  6. *
  7. * @package WordPress
  8. * @subpackage Cache
  9. */
  10. if ( ! function_exists( 'wp_cache_add_multiple' ) ) :
  11. /**
  12. * Adds multiple values to the cache in one call, if the cache keys don't already exist.
  13. *
  14. * Compat function to mimic wp_cache_add_multiple().
  15. *
  16. * @ignore
  17. * @since 6.0.0
  18. *
  19. * @see wp_cache_add_multiple()
  20. *
  21. * @param array $data Array of keys and values to be added.
  22. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  23. * @param int $expire Optional. When to expire the cache contents, in seconds.
  24. * Default 0 (no expiration).
  25. * @return bool[] Array of return values, grouped by key. Each value is either
  26. * true on success, or false if cache key and group already exist.
  27. */
  28. function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
  29. $values = array();
  30. foreach ( $data as $key => $value ) {
  31. $values[ $key ] = wp_cache_add( $key, $value, $group, $expire );
  32. }
  33. return $values;
  34. }
  35. endif;
  36. if ( ! function_exists( 'wp_cache_set_multiple' ) ) :
  37. /**
  38. * Sets multiple values to the cache in one call.
  39. *
  40. * Differs from wp_cache_add_multiple() in that it will always write data.
  41. *
  42. * Compat function to mimic wp_cache_set_multiple().
  43. *
  44. * @ignore
  45. * @since 6.0.0
  46. *
  47. * @see wp_cache_set_multiple()
  48. *
  49. * @param array $data Array of keys and values to be set.
  50. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  51. * @param int $expire Optional. When to expire the cache contents, in seconds.
  52. * Default 0 (no expiration).
  53. * @return bool[] Array of return values, grouped by key. Each value is either
  54. * true on success, or false on failure.
  55. */
  56. function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
  57. $values = array();
  58. foreach ( $data as $key => $value ) {
  59. $values[ $key ] = wp_cache_set( $key, $value, $group, $expire );
  60. }
  61. return $values;
  62. }
  63. endif;
  64. if ( ! function_exists( 'wp_cache_get_multiple' ) ) :
  65. /**
  66. * Retrieves multiple values from the cache in one call.
  67. *
  68. * Compat function to mimic wp_cache_get_multiple().
  69. *
  70. * @ignore
  71. * @since 5.5.0
  72. *
  73. * @see wp_cache_get_multiple()
  74. *
  75. * @param array $keys Array of keys under which the cache contents are stored.
  76. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  77. * @param bool $force Optional. Whether to force an update of the local cache
  78. * from the persistent cache. Default false.
  79. * @return array Array of return values, grouped by key. Each value is either
  80. * the cache contents on success, or false on failure.
  81. */
  82. function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
  83. $values = array();
  84. foreach ( $keys as $key ) {
  85. $values[ $key ] = wp_cache_get( $key, $group, $force );
  86. }
  87. return $values;
  88. }
  89. endif;
  90. if ( ! function_exists( 'wp_cache_delete_multiple' ) ) :
  91. /**
  92. * Deletes multiple values from the cache in one call.
  93. *
  94. * Compat function to mimic wp_cache_delete_multiple().
  95. *
  96. * @ignore
  97. * @since 6.0.0
  98. *
  99. * @see wp_cache_delete_multiple()
  100. *
  101. * @param array $keys Array of keys under which the cache to deleted.
  102. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  103. * @return bool[] Array of return values, grouped by key. Each value is either
  104. * true on success, or false if the contents were not deleted.
  105. */
  106. function wp_cache_delete_multiple( array $keys, $group = '' ) {
  107. $values = array();
  108. foreach ( $keys as $key ) {
  109. $values[ $key ] = wp_cache_delete( $key, $group );
  110. }
  111. return $values;
  112. }
  113. endif;
  114. if ( ! function_exists( 'wp_cache_flush_runtime' ) ) :
  115. /**
  116. * Removes all cache items from the in-memory runtime cache.
  117. *
  118. * Compat function to mimic wp_cache_flush_runtime().
  119. *
  120. * @ignore
  121. * @since 6.0.0
  122. *
  123. * @see wp_cache_flush_runtime()
  124. *
  125. * @return bool True on success, false on failure.
  126. */
  127. function wp_cache_flush_runtime() {
  128. if ( ! wp_cache_supports( 'flush_runtime' ) ) {
  129. _doing_it_wrong(
  130. __FUNCTION__,
  131. __( 'Your object cache implementation does not support flushing the in-memory runtime cache.' ),
  132. '6.1.0'
  133. );
  134. return false;
  135. }
  136. return wp_cache_flush();
  137. }
  138. endif;
  139. if ( ! function_exists( 'wp_cache_flush_group' ) ) :
  140. /**
  141. * Removes all cache items in a group, if the object cache implementation supports it.
  142. *
  143. * Before calling this function, always check for group flushing support using the
  144. * `wp_cache_supports( 'flush_group' )` function.
  145. *
  146. * @since 6.1.0
  147. *
  148. * @see WP_Object_Cache::flush_group()
  149. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  150. *
  151. * @param string $group Name of group to remove from cache.
  152. * @return bool True if group was flushed, false otherwise.
  153. */
  154. function wp_cache_flush_group( $group ) {
  155. global $wp_object_cache;
  156. if ( ! wp_cache_supports( 'flush_group' ) ) {
  157. _doing_it_wrong(
  158. __FUNCTION__,
  159. __( 'Your object cache implementation does not support flushing individual groups.' ),
  160. '6.1.0'
  161. );
  162. return false;
  163. }
  164. return $wp_object_cache->flush_group( $group );
  165. }
  166. endif;
  167. if ( ! function_exists( 'wp_cache_supports' ) ) :
  168. /**
  169. * Determines whether the object cache implementation supports a particular feature.
  170. *
  171. * @since 6.1.0
  172. *
  173. * @param string $feature Name of the feature to check for. Possible values include:
  174. * 'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple',
  175. * 'flush_runtime', 'flush_group'.
  176. * @return bool True if the feature is supported, false otherwise.
  177. */
  178. function wp_cache_supports( $feature ) {
  179. return false;
  180. }
  181. endif;