wp-cli / cache-command
管理对象和短暂缓存。
Requires
- wp-cli/wp-cli: ^2.5
Requires (Dev)
- wp-cli/entity-command: ^1.3 || ^2
- wp-cli/wp-cli-tests: ^4
This package is auto-updated.
Last update: 2024-09-08 06:24:05 UTC
README
管理对象和短暂缓存。
使用
此包实现了以下命令
wp cache
添加、删除、获取和刷新WP对象缓存对象。
wp cache
默认情况下,WP对象缓存存在于PHP内存中,持续到请求的长度(并在结束时清空)。使用持久的对象缓存插件以在请求之间持久化对象缓存。
阅读Codex文章以获取更多详细信息。
示例
# Set cache.
$ wp cache set my_key my_value my_group 300
Success: Set object 'my_key' in group 'my_group'.
# Get cache.
$ wp cache get my_key my_group
my_value
wp cache add
将值添加到对象缓存。
wp cache add <key> <value> [<group>] [<expiration>]
如果键已存在值,则出错,这意味着值无法添加。
选项
<key>
Cache key.
<value>
Value to add to the key.
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
[<expiration>]
Define how long to keep the value, in seconds. `0` means as long as possible.
---
default: 0
---
示例
# Add cache.
$ wp cache add my_key my_group my_value 300
Success: Added object 'my_key' in group 'my_value'.
wp cache decr
在对象缓存中减少一个值。
wp cache decr <key> [<offset>] [<group>]
如果值不能减少,则出错。
选项
<key>
Cache key.
[<offset>]
The amount by which to decrement the item's value.
---
default: 1
---
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
示例
# Decrease cache value.
$ wp cache decr my_key 2 my_group
48
wp cache delete
从对象缓存中删除一个值。
wp cache delete <key> [<group>]
如果值不能删除,则出错。
选项
<key>
Cache key.
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
示例
# Delete cache.
$ wp cache delete my_key my_group
Success: Object deleted.
wp cache flush
刷新对象缓存。
wp cache flush
对于使用持久对象缓存的WordPress多站点实例,刷新对象缓存通常会刷新所有站点的缓存。在生产环境中刷新对象缓存时,请注意性能影响。
如果对象缓存不能刷新,则出错。
示例
# Flush cache.
$ wp cache flush
Success: The cache was flushed.
wp cache flush-group
如果对象缓存实现支持,则删除组中的所有缓存项。
wp cache flush-group <group>
选项
<group>
Cache group key.
示例
# Clear cache group.
$ wp cache flush-group my_group
Success: Cache group 'my_group' was flushed.
wp cache get
从对象缓存中获取一个值。
wp cache get <key> [<group>]
如果值不存在,则出错。
选项
<key>
Cache key.
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
示例
# Get cache.
$ wp cache get my_key my_group
my_value
wp cache incr
在对象缓存中增加一个值。
wp cache incr <key> [<offset>] [<group>]
如果值不能增加,则出错。
选项
<key>
Cache key.
[<offset>]
The amount by which to increment the item's value.
---
default: 1
---
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
示例
# Increase cache value.
$ wp cache incr my_key 2 my_group
50
wp cache replace
如果值已存在,则替换对象缓存中的值。
wp cache replace <key> <value> [<group>] [<expiration>]
如果值不能替换,则出错。
选项
<key>
Cache key.
<value>
Value to replace.
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
[<expiration>]
Define how long to keep the value, in seconds. `0` means as long as possible.
---
default: 0
---
示例
# Replace cache.
$ wp cache replace my_key new_value my_group
Success: Replaced object 'my_key' in group 'my_group'.
wp cache set
无论值是否存在,都将值设置到对象缓存中。
wp cache set <key> <value> [<group>] [<expiration>]
如果值不能设置,则出错。
选项
<key>
Cache key.
<value>
Value to set on the key.
[<group>]
Method for grouping data within the cache which allows the same key to be used across groups.
---
default: default
---
[<expiration>]
Define how long to keep the value, in seconds. `0` means as long as possible.
---
default: 0
---
示例
# Set cache.
$ wp cache set my_key my_value my_group 300
Success: Set object 'my_key' in group 'my_group'.
wp cache supports
确定对象缓存实现是否支持特定功能。
wp cache supports <feature>
选项
<feature>
Name of the feature to check for.
示例
# Check whether is add_multiple supported.
$ wp cache supports add_multiple
$ echo $?
0
# Bash script for checking whether for support like this:
if ! wp cache supports non_existing; then
echo 'non_existing is not supported'
fi
wp cache type
尝试确定正在使用的对象缓存。
wp cache type
请注意,此函数做出的猜测基于定义第三方对象缓存扩展的WP_Object_Cache类。这些类的更改可能导致此函数在确定正在使用的对象缓存方面出现问题。
示例
# Check cache type.
$ wp cache type
Default
wp transient
添加、获取和删除WordPress短暂缓存中的条目。
wp transient
默认情况下,短暂缓存使用WordPress数据库在请求之间持久化值。在单个站点安装中,值存储在wp_options
表中。在多站点安装中,值存储在wp_options
或wp_sitemeta
表中,具体取决于是否使用了--network
标志。
当安装了持久化对象缓存插件(例如Redis或Memcached)时,临时缓存会跳过数据库,直接包装WP对象缓存。
示例
# Set transient.
$ wp transient set sample_key "test data" 3600
Success: Transient added.
# Get transient.
$ wp transient get sample_key
test data
# Delete transient.
$ wp transient delete sample_key
Success: Transient deleted.
# Delete expired transients.
$ wp transient delete --expired
Success: 12 expired transients deleted from the database.
# Delete all transients.
$ wp transient delete --all
Success: 14 transients deleted from the database.
wp transient delete
删除临时值。
wp transient delete [<key>] [--network] [--all] [--expired]
有关临时缓存更完整的说明,包括网络|站点缓存,请参阅wp transient
的文档。
选项
[<key>]
Key for the transient.
[--network]
Delete the value of a network|site transient. On single site, this is
is a specially-named cache key. On multisite, this is a global cache
(instead of local to the site).
[--all]
Delete all transients.
[--expired]
Delete all expired transients.
示例
# Delete transient.
$ wp transient delete sample_key
Success: Transient deleted.
# Delete expired transients.
$ wp transient delete --expired
Success: 12 expired transients deleted from the database.
# Delete expired site transients.
$ wp transient delete --expired --network
Success: 1 expired transient deleted from the database.
# Delete all transients.
$ wp transient delete --all
Success: 14 transients deleted from the database.
# Delete all site transients.
$ wp transient delete --all --network
Success: 2 transients deleted from the database.
# Delete all transients in a multisite.
$ wp transient delete --all --network && wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --all
wp transient get
获取临时值。
wp transient get <key> [--format=<format>] [--network]
有关临时缓存更完整的说明,包括网络|站点缓存,请参阅wp transient
的文档。
选项
<key>
Key for the transient.
[--format=<format>]
Render output in a particular format.
---
default: table
options:
- table
- csv
- json
- yaml
---
[--network]
Get the value of a network|site transient. On single site, this is
is a specially-named cache key. On multisite, this is a global cache
(instead of local to the site).
示例
$ wp transient get sample_key
test data
$ wp transient get random_key
Warning: Transient with key "random_key" is not set.
wp transient set
设置临时值。
wp transient set <key> <value> [<expiration>] [--network]
<expiration>
是直到过期的时间,以秒为单位。
有关临时缓存更完整的说明,包括网络|站点缓存,请参阅wp transient
的文档。
选项
<key>
Key for the transient.
<value>
Value to be set for the transient.
[<expiration>]
Time until expiration, in seconds.
[--network]
Set the value of a network|site transient. On single site, this is
is a specially-named cache key. On multisite, this is a global cache
(instead of local to the site).
示例
$ wp transient set sample_key "test data" 3600
Success: Transient added.
wp transient type
确定临时实现类型。
wp transient type
表示临时API是否使用对象缓存或数据库。
有关临时缓存更完整的说明,包括网络|站点缓存,请参阅wp transient
的文档。
示例
$ wp transient type
Transients are saved to the database.
wp transient list
列出临时及其值。
wp transient list [--search=<pattern>] [--exclude=<pattern>] [--network] [--unserialize] [--human-readable] [--fields=<fields>] [--format=<format>]
选项
[--search=<pattern>]
Use wildcards ( * and ? ) to match transient name.
[--exclude=<pattern>]
Pattern to exclude. Use wildcards ( * and ? ) to match transient name.
[--network]
Get the values of network|site transients. On single site, this is
a specially-named cache key. On multisite, this is a global cache
(instead of local to the site).
[--unserialize]
Unserialize transient values in output.
[--human-readable]
Human-readable output for expirations.
[--fields=<fields>]
Limit the output to specific object fields.
[--format=<format>]
The serialization format for the value.
---
default: table
options:
- table
- json
- csv
- count
- yaml
---
可用字段
此字段将默认显示为每个匹配选项
- name
- value
- expiration
示例
# List all transients
$ wp transient list
+------+-------+---------------+
| name | value | expiration |
+------+-------+---------------+
| foo | bar | 39 mins |
| foo2 | bar2 | no expiration |
| foo3 | bar2 | expired |
| foo4 | bar4 | 4 hours |
+------+-------+---------------+
安装
此软件包包含在WP-CLI本身中,无需额外安装。
要安装此软件包的最新版本,超过WP-CLI中包含的版本,请运行
wp package install [email protected]:wp-cli/cache-command.git
贡献
我们感谢您主动为该项目做出贡献。
贡献不仅限于代码。我们鼓励您根据自己的能力以最佳方式做出贡献,例如撰写教程、在当地的聚会上进行演示、帮助其他用户解决支持问题或修订我们的文档。
欲了解更多信息,请查看WP-CLI的贡献指南。此软件包遵循那些政策和指南。
报告错误
你认为你找到了一个错误?我们很高兴你能帮助我们修复它。
在创建新问题之前,你应该搜索现有问题,看看是否已存在对该错误的解决方案,或者它是否已在更新的版本中修复。
在你进行了一些搜索并发现没有针对你的错误的开放或已修复的问题后,请创建一个新问题。尽可能提供详细信息,并在可能的情况下提供明确的重现步骤。有关更多指导,请查看我们的错误报告文档。
创建pull request
想要贡献一个新功能?请首先打开一个新问题,讨论该功能是否适合此项目。
一旦你决定投入时间让你的pull request顺利完成,请遵循我们创建pull request的指南,以确保这是一次愉快的体验。有关在本地处理此软件包的详细信息,请参阅"设置"。
支持
GitHub问题不是用于一般支持问题的,但你还可以尝试其他途径:https://wp-cli.org/#support
此README.md是从项目的代码库动态生成的,使用wp scaffold package-readme
(文档)。要提出更改,请针对代码库的相应部分提交pull request。