pinkcrab / wp-psr16-cache
基于 WordPress 实现的 PSR16 Cache 接口,使用 transient 或 WP_File_System
2.0.4
2022-03-31 00:02 UTC
Requires
- php: >=7.1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: *
- gin0115/wpunit-helpers: 1.*
- object-calisthenics/phpcs-calisthenics-rules: *
- php-stubs/wordpress-stubs: ^5.9.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: ^5.9
- symfony/var-dumper: 4.*
- szepeviktor/phpstan-wordpress: ^1.0
- wp-coding-standards/wpcs: *
- wp-phpunit/wp-phpunit: ^5.9
- yoast/phpunit-polyfills: ^0.2.0 || ^1.0.0
This package is auto-updated.
Last update: 2024-09-07 04:46:59 UTC
README
提供 WP Transient 和 WP FileSystem (直接) 实现,用于 PSR16 的 CacheInterface。
要求
需要 Composer 和 WordPress。
- 测试过 PHP7.1, 7.2, 7.3, 7.4, 8.0, 8.1
- 测试过 WP5.5, 5.6, 5.7, 5.8, 5.9
安装
$ composer require pinkcrab/wp-psr16-cache
入门指南
安装好软件包并包含自动加载器后。
文件缓存
use PinkCrab\WP_PSR16_Cache\File_Cache; use PinkCrab\WP_PSR16_Cache\Transient_Cache; // FILE CACHE // Creates directory at path passed, if it doesn't exist. $cache = new File_Cache('path/to/dir', '.do'); // TRANSIENT CACHE // Created with optional groups, adding a prefix to transient keys and set file extension. $cache = new Transient_Cache('group_prefix' ); // Set single item to cache. $cache->set( 'cache_key', $data, 24 * HOURS_IN_SECONDS ); // Gets the value, if not set or expired returns null $cache->get( 'cache_key', 'fallback' ); // Returns if valid cache item exists. $cache->has( 'cache_key' ); // Deletes a cache if it exists. $cahe->delete( 'cache_key' ); // Set multiple values, with a single expiry $cache->setMultiple( ['key1' => 'Value1', 'key2' => 42], 1 * HOURS_IN_SECONDS ); // Get multiple values in a key => value array, with a shared default. $cache->getMultiple( ['key1', 'key2'], 'FALLBACK' ); // Clears multiple keys. $cache->deleteMultiple( ['key1', 'key2'] ); // Clear all cache items $cache->clear();
File_Cache
在创建对象时将创建定义的基本目录。
构造函数接收两个属性:路径和文件扩展名。默认文件扩展名为 .do。
$wp_uploads = wp_upload_dir(); $cache = new File_Cache($wp_uploads['basedir'] . '/my-cache', '.cache'); $cache->set('my_key', ['some', 'data']); // Creates /public/wp-content/uploads/my-cache/my_key.cache
如果您计划将其作为插件使用并希望安装后进行清理,您只需在激活时创建类的实例,然后在卸载时运行 clear 即可。
/** * Creates the cache directory */ function called_on_activation(){ new File_Cache($wp_uploads['basedir'] . '/my-cache', '.cache'); } /** * Clears all values form the cache directory. * Please note doesn't delete the folder. */ function called_on_uninstall(){ (new File_Cache($wp_uploads['basedir'] . '/my-cache', '.cache'))->clear(); }
Transient 缓存
使用前缀/分组 transient 值。在允许简短和清晰的键的同时防止冲突。
构造函数接受一个参数,表示您将使用的前缀组。如果您想要键上没有前缀,可以省略。
$cache = new Transient_Cache('my_cache'); $cache->set('my_key', ['some', 'data']); // Will create a transient which can be recalled using either; $value = get_transient('my_cache_my_key'); (new Transient_Cache('my_cache'))->get('my_key'); // You can create an instance with no key $cache = New Transient_Cache(); $cache->set('my_other_key', ['some', 'data']); // Get $value = get_transient('my_other_key');
请注意:调用 clear() 将使用 $wpdb 从数据库中获取所有 transient 并清除以您的前缀开头的任何 transient。如果您未定义前缀,这可能会清除您所有的 transient 并产生一些异常副作用。
此外:一些托管主机将 transient 存储在常规选项表之外。这可能导致获取具有您前缀的所有 transient 时出现问题。
变更日志
- 2.0.4 - 更新了开发依赖项,并添加了 scrutinizer 到 CI
- 2.0.3 - 修复了缺少 wp 文件系统包含的问题
- 2.0.2 - 修复了 Readme 格式问题,并添加了额外的测试以实现 100% 覆盖率。
- 2.0.1 - 修复了 File_Cache 中的尾随逗号问题,并设置了所有 github CI 工作流程。
- 2.0.0 - 转移到 composer,并改用 WP_FileSystem 而不是原始 PHP 函数。