simplecomplex / cache
基于文件的PSR-16简单缓存实现;缓存管理;命令行界面。
1.3.3
2019-08-16 10:46 UTC
Requires
- php: >=7.0
- psr/simple-cache: ^1.0 || dev-develop
- simplecomplex/utils: ^1.8 || ^2.0 || dev-develop
Suggests
- simplecomplex/inspect: CLI features do NOT work without Inspect. And great for logging; better variable dumps and traces.
README
范围
缓存复杂变量和难以生成的变量。
如配置、本地化和服务响应。
不是页面缓存,没有缓存雪崩保护。
缓存抽象
CacheBroker
将使用缓存的代码与实际的PSR-16缓存实现解耦。
定义了三个缓存类别名
- 变量存活时间(默认ttl和set() arg ttl)
- 固定存活时间(默认ttl,set() arg ttl忽略)
- 持久(默认ttl 'forever',set() arg ttl忽略)
以及上述类似的三个,允许长键;长度为128,而不是PSR-16兼容的64。
如何使用
请求 CacheBroker
获取别名类型的缓存实例 - 不要 实例化特定的缓存类。
如果以后想从基于文件的缓存切换到基于数据库的缓存,则扩展 CacheBroker
。
依赖注入容器ID:cache-broker
建议:通过DI容器ID 'cache-broker' 访问(并因此实例化)缓存代理。
见 SimpleComplex Utils Dependency
。
基于文件的缓存
FileCache
是一个全面且谨慎的PSR-16简单缓存实现;基于文件。
编写时考虑防御性 - 验证键(和其他参数)。
地址
- 默认存活时间;忽略set() arg ttl选项;无存活时间
- 垃圾回收
- 清除所有项或仅清除过期项
- 导出所有项,到JSON
- 在生产期间构建/替换缓存存储(使用'候选'存储)
- 命令行界面用于清除项,例如通过cron
- 并发问题(仅限于存储方面)
缓存管理、替换和备份
定义了两个扩展PSR-16 CacheInterface,由 FileCache
实现。
ManageableCacheInterface
- 缓存存储是新的还是空的?
- 设置默认存活时间;设置'ignore' set() arg ttl
- 清除和导出
- 列出所有缓存存储
BackupCacheInterface
- 备份/恢复
- 通过构建'候选'并切换到完成时的存储来替换存储
示例
// Bootstrap. Dependency::genericSet('cache-broker', function () { return new \SimpleComplex\Cache\CacheBroker(); }); // ... // Use. /** @var \Psr\Container\ContainerInterface $container */ $container = Dependency::container(); /** @var \SimpleComplex\Cache\CacheBroker $cache_broker */ $cache_broker = $container->get('cache-broker'); /** * Create or re-initialize a cache store. * * @var \SimpleComplex\Cache\FileCache $cache_store */ $cache_store = $cache_broker->getStore( 'some-cache-store', CacheBroker::CACHE_VARIABLE_TTL ); unset($cache_broker); /** @var mixed $whatever */ $whatever = $cache_store->get('some-key', 'the default value');
命令行命令
# List all cache commands and their help. php cli.php cache -h # One command's help. php cli.php cache-xxx -h # List existing cache stores. php cli.php cache-list-stores # Display/get value of a cache item. php cli.php cache-get store key # Delete a cache item. php cli.php cache-delete store key # Delete all expired items of one or all cache stores. php cli.php cache-clear-expired # Delete all items of one or all cache stores. php cli.php cache-clear # Backup a cache store. php cli.php cache-backup store # Restore a cache store from backup. php cli.php cache-restore store # Destroy one or all cache stores. php cli.php cache-destroy
安装
在文档根目录旁边创建一个'private'文件目录
并使其可写,供网络服务器用户(www-data或apache)使用。
例如
/var/www/my-host/
http
/var/www/my-host/
private
在第一次缓存存储实例化时,FileCache
将创建目录
private/
lib/simplecomplex/file-cache
如果该目录结构不合适,请执行以下操作之一
- 向
CacheBroker
(或FileCache
构造函数直接)提供 'path' 参数 - 扩展
FileCache
并覆盖其类常量PATH_DEFAULT
需求
- PHP >=7.0
- 64位PHP
- PSR-16 Simple Cache
- SimpleComplex Utils