linio / cache
提供多层次的缓存抽象
v5.1.1
2023-11-15 22:30 UTC
Requires
- php: ^8.1
- doctrine/inflector: ^2.0
- linio/database: ^5.1.0
- linio/util: ^4.0
- predis/predis: ^1.0 || ^2.0
- psr/log: ^1.0.1 || ^2.0 || ^3.0
Requires (Dev)
- contorion/aerospike-stub: 1.0.*
- friendsofphp/php-cs-fixer: ^3.6
- phpspec/prophecy-phpunit: ^1.1
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^8.5
Suggests
- ext-aerospike: Allows usage of Aerospike adapter
- ext-phpiredis: Allows faster serialization and deserialization of the Redis protocol
- ext-redis: Allows usage of phpredis adapter
- dev-master
- v5.1.1
- 5.1.0
- 5.0.1
- 5.0.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.1
- 3.0.0
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-chore/CC-1707-add-support-to-predis-2
- dev-feat/add-ttl-to-set
- dev-test/remove-deprecations-and-code-style
- dev-master-legacy
- dev-chore/support-linio-util-4.0
- dev-dependabot/composer/friendsofphp/php-cs-fixer-tw-2.16or-tw-3.0
- dev-dependabot/add-v2-config-file
- dev-update-doctrine-inflector-v2
- dev-update-doctrine-inflector
- dev-add-validation-for-bool-results-in-phpredis
- dev-maintenance
- dev-dependabot/composer/phpunit/phpunit-tw-8.5or-tw-9.0
This package is auto-updated.
Last update: 2024-09-16 19:46:18 UTC
README
Linio Cache 是 Linio 框架的另一个组件。它旨在通过支持多个适配器来抽象缓存。
安装
推荐通过 composer 安装 Linio Cache。
{ "require": { "linio/cache": "dev-master" } }
测试
要运行测试套件,您需要通过 composer 安装依赖项,然后运行 PHPUnit。
$ composer install
$ phpunit
未找到的缓存键
现在(v.1.0.9)可以在适配器堆栈的上一级适配器中缓存未找到的键。可以在适配器级别设置配置选项 cache_not_found_keys
。
请注意,此选项显然不适用于缓存层次结构的最后一层。
使用方法
<?php use \Linio\Component\Cache\CacheService; $container['cache'] = new CacheService([ 'namespace' => 'mx', 'layers' => [ 0 => [ 'adapter_name' => 'array', 'adapter_options' => [ 'cache_not_found_keys' => true, 'encoder' => 'json', ], ], 1 => [ 'adapter_name' => 'apc', 'adapter_options' => [ 'ttl' => 3600, ], ], 2 => [ 'adapter_name' => 'redis', 'adapter_options' => [ 'host' => 'localhost', 'port' => 6379, 'ttl' => 0, 'encoder' => 'serial', ], ], ], ]); $container->setLogger($container['logger']);
请注意,必须提供一个适配器名称和选项数组。每个适配器都有不同的配置选项。
开始设置数据
<?php $app['cache.service']->set('foo', 'bar');
方法
get
<?php /** * @param string $key * @return string value */ public function get($key); $adapter->get('foo');
getMulti
<?php /** * @param array $keys * @return string[] */ public function getMulti(array $keys); $adapter->getMulti(['foo', 'nop']);
set
<?php /** * @param string $key * @param string $value * @param ?int $ttl Time To Live; store value in the cache for ttl seconds. * This ttl overwrites the configuration ttl of the adapter * @return bool */ public function set(string $key, $value, ?int $ttl = null); $adapter->set('foo', 'bar'); $adapter->set('foo', 'bar', 60); // store bar in the cache for 60 seconds
setMulti
<?php /** * @param array $keys * @return bool */ public function setMulti(array $data); $adapter->setMulti(['foo' => 'bar', 'fooz' => 'baz']);
delete
<?php /** * @param string $key * @return bool */ public function delete($key); $adapter->delete('foo');
deleteMulti
<?php /** * @param array $keys * @return bool */ public function deleteMulti(array $keys); $adapter->deleteMulti(['foo', 'fooz']);
contains
<?php /** * @param string $key * @return bool */ public function contains($key); $adapter->contains('foo');
flush
<?php /** * @return bool */ public function flush(); $adapter->flush();
提供者
array
此缓存不会在请求之间保持持久性。
不建议在生产环境中使用。
apc
适配器选项
ttl
可选 默认: 0(无限期)cache_not_found_keys
可选 默认: false
wincache
适配器选项
ttl
可选 默认: 0(无限期)cache_not_found_keys
可选 默认: false
需要 WinCache 扩展。
memcached
适配器选项
servers
memcache 服务器的数组。格式:[[, , ], [, , ], ...]options
memcache 选项的数组。格式:[<option_name1> => , <option_name2> => , ...]connection_persistent
可选 默认: falsepool_size
可选 默认: 1(仅适用于持久连接)ttl
可选 默认: 0(无限期)cache_not_found_keys
可选 默认: false
需要 Memcached 扩展。
redis
适配器选项
host
可选 默认: 127.0.0.1port
可选 默认: 6379database
可选 默认: 0(整数)password
可选 默认: null(无密码)connection_persistent
可选 默认: falsettl
可选 默认: 0(无限期)cache_not_found_keys
可选 默认: false
有关可用参数的更多信息,请参阅 Predis 文档。
phpredis
适配器选项
host
可选 默认: 127.0.0.1port
可选 默认: 6379database
可选 默认: 0(整数)password
可选 默认: null(无密码)connection_persistent
可选 默认: falsepool_size
可选 默认: 1(仅适用于持久连接)timeout
可选 默认: 0(无限期)read_timeout
可选 默认: 0(无限期)retry_interval
可选 默认: 0(毫秒值)ttl
可选 默认: 0(无限期)cache_not_found_keys
可选 默认: falseserializer
可选 默认: nonenone
不序列化数据php
使用内置的序列化/反序列化igbinary
使用 igBinary 序列化/反序列化(需要igbinary
扩展)
有关可用参数的更多信息,请参阅 phpredis 文档。
需要 redis 扩展。
mysql
使用 PDO。
适配器选项
主机
端口
数据库名
用户名
密码
表名
ensure_table_created
可选 默认: falsecache_not_found_keys
可选 默认: false
使用 ensure_table_created
确保缓存表在数据库中存在。此选项对性能有显著影响。
不建议在生产环境中使用。
aerospike
适配器选项
主机
aerospike_namespace
可选 默认:testpersistent
可选 默认:trueoptions
可选 默认:[]ttl
可选 默认: 0(无限期)cache_not_found_keys
可选 默认: false
对于Aerospike适配器,aerospike_namespace 属性将用作Aerospike中的 namespace,而 CacheService 中的 namespace 配置将用作Aerospike中的 set。
需要 Aerospike扩展。