Skip to:
Content

BuddyPress.org

Changeset 11053


Ignore:
Timestamp:
09/01/2016 03:27:05 AM (8 years ago)
Author:
boonebgorges
Message:

Introduce cache incrementor functions.

A cache incrementor is a unique string that allows for easy bulk
invalidation of specific cache items. In the BuddyPress context, this
is useful for things like object queries. Object queries have more
permutations of query parameters than can be individually invalidated.
As a workaround, we can ensure that all cache keys for query results
are hashed with the same incrementor; bumping this incrementor will
then effectively invalidate all of these caches.

See #7237.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-cache.php

    r10950 r11053  
    263263    return $cache;
    264264}
     265
     266/**
     267 * Gets a value that has been cached using an incremented key.
     268 *
     269 * A utility function for use by query methods like BP_Activity_Activity::get().
     270 *
     271 * @since 2.7.0
     272 * @see bp_core_set_incremented_cache()
     273 *
     274 * @param string $key   Unique key for the query. Usually a SQL string.
     275 * @param string $group Cache group. Eg 'bp_activity'.
     276 * @return array|bool False if no cached values are found, otherwise an array of IDs.
     277 */
     278function bp_core_get_incremented_cache( $key, $group ) {
     279    $cache_key = bp_core_get_incremented_cache_key( $key, $group );
     280    return wp_cache_get( $cache_key, $group );
     281}
     282
     283/**
     284 * Caches a value using an incremented key.
     285 *
     286 * An "incremented key" is a cache key that is hashed with a unique incrementor,
     287 * allowing for bulk invalidation.
     288 *
     289 * Use this method when caching data that should be invalidated whenever any
     290 * object of a given type is created, updated, or deleted. This usually means
     291 * data related to object queries, which can only reliably cached until the
     292 * underlying set of objects has been modified. See, eg, BP_Activity_Activity::get().
     293 *
     294 * @since 2.7.0
     295 *
     296 * @param string $key   Unique key for the query. Usually a SQL string.
     297 * @param string $group Cache group. Eg 'bp_activity'.
     298 * @param array  $ids   Array of IDs.
     299 * @return bool
     300 */
     301function bp_core_set_incremented_cache( $key, $group, $ids ) {
     302    $cache_key = bp_core_get_incremented_cache_key( $key, $group );
     303    return wp_cache_set( $cache_key, $ids, $group );
     304}
     305
     306/**
     307 * Gets the key to be used when caching a value using an incremented cache key.
     308 *
     309 * The $key is hashed with a component-specific incrementor, which is used to
     310 * invalidate multiple caches at once.
     311
     312 * @since 2.7.0
     313 *
     314 * @param string $key   Unique key for the query. Usually a SQL string.
     315 * @param string $group Cache group. Eg 'bp_activity'.
     316 * @return string
     317 */
     318function bp_core_get_incremented_cache_key( $key, $group ) {
     319    $incrementor = bp_core_get_incrementor( $group );
     320    $cache_key = md5( $key . $incrementor );
     321    return $cache_key;
     322}
     323
     324/**
     325 * Gets a group-specific cache incrementor.
     326 *
     327 * The incrementor is paired with query identifiers (like SQL strings) to
     328 * create cache keys that can be invalidated en masse.
     329 *
     330 * If an incrementor does not yet exist for the given `$group`, one will
     331 * be created.
     332 *
     333 * @since 2.7.0
     334 *
     335 * @param string $group Cache group. Eg 'bp_activity'.
     336 * @return string
     337 */
     338function bp_core_get_incrementor( $group ) {
     339    $incrementor = wp_cache_get( 'incrementor', $group );
     340    if ( ! $incrementor ) {
     341        $incrementor = microtime();
     342        wp_cache_set( 'incrementor', $incrementor, $group );
     343    }
     344
     345    return $incrementor;
     346}
     347
     348/**
     349 * Reset a group-specific cache incrementor.
     350 *
     351 * Call this function when all incrementor-based caches associated with a given
     352 * cache group should be invalidated.
     353 *
     354 * @since 2.7.0
     355 *
     356 * @param string $group Cache group. Eg 'bp_activity'.
     357 * @return bool True on success, false on failure.
     358 */
     359function bp_core_reset_incrementor( $group ) {
     360    wp_cache_delete( 'incrementor', $group );
     361}
Note: See TracChangeset for help on using the changeset viewer.