函数文档

wp_cache_set_last_changed()

💡 云策文档标注

概述

wp_cache_set_last_changed() 函数用于将指定缓存组的最后更改时间设置为当前时间,并返回UNIX时间戳。它通过更新缓存中的 'last_changed' 键值来实现,并触发一个动作钩子。

关键要点

  • 函数参数:$group(字符串,必需),指定缓存组名。
  • 返回值:字符串,表示缓存组最后更改的UNIX时间戳。
  • 内部流程:先获取之前的 'last_changed' 时间,然后使用 microtime() 设置新时间,并调用 wp_cache_set() 更新缓存。
  • 钩子:do_action('wp_cache_set_last_changed', $group, $time, $previous_time),在更新后触发,允许开发者执行相关操作。
  • 相关函数:与 wp_cache_set()、wp_cache_get() 和 wp_cache_get_last_changed() 等缓存函数协同工作。
  • 使用场景:常用于用户、站点、分类、评论和文章等缓存组的最后更改时间管理。
  • 版本历史:自 WordPress 6.3.0 版本引入。

代码示例

function wp_cache_set_last_changed( $group ) {
    $previous_time = wp_cache_get( 'last_changed', $group );

    $time = microtime();

    wp_cache_set( 'last_changed', $time, $group );

    do_action( 'wp_cache_set_last_changed', $group, $time, $previous_time );

    return $time;
}

注意事项

  • 此函数可能在每个页面加载中多次调用,注册的动作必须高效,以避免性能问题。
  • 钩子参数包括缓存组名、新时间戳和之前的时间戳(如果未设置则为 false)。

📄 原文内容

Sets last changed date for the specified cache group to now.

Parameters

$groupstringrequired
Where the cache contents are grouped.

Return

string UNIX timestamp when the group was last changed.

Source

function wp_cache_set_last_changed( $group ) {
	$previous_time = wp_cache_get( 'last_changed', $group );

	$time = microtime();

	wp_cache_set( 'last_changed', $time, $group );

	/**
	 * Fires after a cache group `last_changed` time is updated.
	 * This may occur multiple times per page load and registered
	 * actions must be performant.
	 *
	 * @since 6.3.0
	 *
	 * @param string       $group         The cache group name.
	 * @param string       $time          The new last changed time (msec sec).
	 * @param string|false $previous_time The previous last changed time. False if not previously set.
	 */
	do_action( 'wp_cache_set_last_changed', $group, $time, $previous_time );

	return $time;
}

Hooks

do_action( ‘wp_cache_set_last_changed’, string $group, string $time, string|false $previous_time )

Fires after a cache group last_changed time is updated.

Changelog

Version Description
6.3.0 Introduced.