dimadin/wp-temporary

WordPress 数据库缓存库。类似于无对象缓存的 transient。

1.0.0 2018-09-14 14:53 UTC

This package is auto-updated.

Last update: 2024-09-16 07:04:20 UTC


README

Build Status Latest Stable Version

WP_Temporary 是一个辅助类,包含一组静态方法,用于与数据库中存储数据直到过期的方式相同的 transient 函数。基本上,它与 transient 存储在数据库中(禁用对象缓存)的方式相同,因此它不会在过期之前被删除。

transient 如果可用,会使用对象缓存,如果不可用,则回退到数据库。正因为如此,transient 可能在任何时间点在它们过期之前消失。 WP_Temporary 解决了这个问题,因为使用它存储的数据不会在过期之前消失。

此外,WP_Temporary 还有一个单独的方法,可以更改已设置的临时数据的值,但不会更改该数据的过期时间。这与仅允许设置总是更改过期时间以便从设置时刻开始计时的 transient 不同。

WP_Temporary 具有内置的垃圾回收器和 WP-CLI 命令。

使用

WP_Temporary 作为 Composer 包提供,您可以在项目中使用它。

composer require dimadin/wp-temporary

或者,您可以下载文件 class-wp-temporary.php 并将其包含到您的项目中。

API

完整的代码参考可在此处找到:http://api.milandinic.com/wp-temporary/

WP_Temporary::set()

等同于 set_transient()

WP_Temporary::get()

等同于 get_transient()

WP_Temporary::delete()

等同于 delete_transient()

WP_Temporary::update()

此方法与 WP_Temporary::set() 相同,只有一个区别:如果临时数据已存在,它将不会更改过期时间,只需更改其值(不使用第三个参数 $expiration);如果它不存在,它将回退到 WP_Temporary::set()

WP_Temporary::set_site()

等同于 set_site_transient()

WP_Temporary::get_site()

等同于 get_site_transient()

WP_Temporary::delete_site()

等同于 delete_site_transient()

WP_Temporary::update_site()

此方法与 WP_Temporary::set_site() 相同,只有一个区别:如果临时数据已存在,它将不会更改过期时间,只需更改其值(不使用第三个参数 $expiration);如果它不存在,它将回退到 WP_Temporary::set_site()

WP_Temporary::clean()

这是 WP_Temporary 的垃圾回收器。它清除数据库中所有过期的临时数据(其过期时间超过一分钟)。

请注意,它默认不运行,您需要通过以下方式调用它:

WP_Temporary::clean();

或者将其挂钩到某个动作。例如,以下代码使用 WP Cron 每天调用一次:

add_action( 'wp_scheduled_delete', array( 'WP_Temporary', 'clean' ) );

WP_Temporary::init_wp_cli()

此方法初始化 WP-CLI 命令 wp temporary。它默认不运行,您需要通过以下方式调用它:

WP_Temporary::init_wp_cli();

或者将其连接到某些操作。例如,这个操作仅在WP-CLI初始化时调用

add_action( 'cli_init', array( 'WP_Temporary', 'init_wp_cli' ) );

注意,如果您没有使用Composer来包含 WP_Temporary(),则必须手动包含文件 /cli/Temporary_Command.php

WP-CLI

WP_Temporary 实现以下命令

wp temporary

添加、获取、更新和删除临时数据。

临时数据使用WordPress数据库在请求之间持久化值。在单站安装中,值存储在 wp_options 表中。在多站安装中,值存储在 wp_optionswp_sitemeta 表中,具体取决于是否使用 --network 标志。

示例

# Set temporary.
$ wp temporary set sample_key "test data" 3600
Success: Temporary added.

# Update temporary.
$ wp temporary update sample_key "test data" 3600
Success: Temporary updated.

# Get temporary.
$ wp temporary get sample_key
test data

# Get all temporaries.
$ wp temporary get --all

# Delete temporary.
$ wp temporary delete sample_key
Success: Temporary deleted.

# Delete all temporaries.
$ wp temporary delete --all
Success: 14 temporaries deleted from the database.

wp temporary get

获取临时值。

wp temporary get [<key>] [--format=<format>] [--network] [--all]

选项

[<key>]
    Key for the temporary.

[--format=<format>]
    Render output in a particular format.
    ---
    options:
      - json
      - yaml
    ---

[--network]
     Get the value of a network|site temporary.

[--all]
    Get all temporaries.

示例

# Get temporary.
$ wp temporary get sample_key
test data

# Get temporary.
$ wp temporary get random_key
Warning: Temporary with key "random_key" is not set.

# Get all temporaries.
$ wp temporary get --all

wp temporary set

设置临时值。

wp temporary set <key> <value> [<expiration>] [--network]

选项

<key>
    Key for the temporary.

<value>
    Value to be set for the temporary.

[<expiration>]
    Time until expiration, in seconds.

[--network]
     Set the value of a network|site temporary.

示例

$ wp temporary set sample_key "test data" 3600
Success: Temporary added.

wp temporary update

更新临时值。

更改现有临时值的值而不影响过期时间,或者如果临时值不存在,则设置新的临时值并带有提供的过期时间。

wp temporary update <key> <value> [<expiration>] [--network]

选项

<key>
    Key for the temporary.

<value>
    Value to be set for the temporary.

[<expiration>]
    Time until expiration, in seconds.

[--network]
     Set the value of a network|site temporary.

示例

$ wp temporary update sample_key "test data" 3600
Success: Temporary updated.

wp temporary delete

获取临时值。

wp temporary delete [<key>] [--network] [--all]

选项

[<key>]
    Key for the temporary.

[--network]
    Delete the value of a network|site temporary.

[--all]
    Delete all temporaries.

示例

# Delete temporary.
$ wp temporary delete sample_key
Success: Temporary deleted.

# Delete all temporaries.
$ wp temporary delete --all
Success: 14 temporaries deleted from the database.

wp temporary clean

删除所有过期的临时数据。

wp temporary clean

示例

$ wp temporary clean
Success: Expired temporaries deleted from the database.