WC_Cache_Helper::get_transient_version( string $group, boolean $refresh = false )

Get transient version.


Description Description

When using transients with unpredictable names, e.g. those containing an md5 hash in the name, we need a way to invalidate them all at once.

When using default WP transients we’re able to do this with a DB query to delete transients manually.

With external cache however, this isn’t possible. Instead, this function is used to append a unique string (based on time()) to each transient. When transients are invalidated, the transient version will increment and data will be regenerated.

Raised in issue https://github.com/woocommerce/woocommerce/issues/5777. Adapted from ideas in http://tollmanz.com/invalidation-schemes/.


Parameters Parameters

$group

(Required) Name for the group of transients we need to invalidate.

$refresh

(Optional) true to force a new version.

Default value: false


Top ↑

Return Return

(string) transient version based on time(), 10 digits.


Top ↑

Source Source

File: includes/class-wc-cache-helper.php

	public static function get_transient_version( $group, $refresh = false ) {
		$transient_name  = $group . '-transient-version';
		$transient_value = get_transient( $transient_name );

		if ( false === $transient_value || true === $refresh ) {
			$transient_value = (string) time();

			set_transient( $transient_name, $transient_value );
		}

		return $transient_value;
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.