函数文档

wp_cache_get_last_changed()

💡 云策文档标注

概述

wp_cache_get_last_changed() 函数用于获取指定缓存组的最后更改日期,返回带微秒的 UNIX 时间戳。它通过查询缓存或设置新时间戳来确保数据一致性。

关键要点

  • 参数 $group 为字符串类型,必需,指定缓存内容的分组。
  • 返回值为字符串,表示缓存组最后更改的 UNIX 时间戳(含微秒)。
  • 函数内部先尝试从缓存获取 last_changed 值,若不存在则调用 wp_cache_set_last_changed() 设置并返回新时间戳。
  • 该函数自 WordPress 4.7.0 版本引入,常用于 WP_Query、WP_User_Query 等查询类中管理缓存失效。

代码示例

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

    if ( $last_changed ) {
        return $last_changed;
    }

    return wp_cache_set_last_changed( $group );
}

注意事项

  • 确保 $group 参数正确对应缓存分组,以避免数据混淆。
  • 返回值包含微秒,适用于高精度时间比较场景。
  • 相关函数包括 wp_cache_set_last_changed() 和 wp_cache_get(),用于设置和获取缓存数据。

📄 原文内容

Gets last changed date for the specified cache group.

Parameters

$groupstringrequired
Where the cache contents are grouped.

Return

string UNIX timestamp with microseconds representing when the group was last changed.

Source

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

	if ( $last_changed ) {
		return $last_changed;
	}

	return wp_cache_set_last_changed( $group );
}

Changelog

Version Description
4.7.0 Introduced.