infira / cachly
快速链接到 https://github.com/symfony/cache 缓存解决方案
v1.3.0
2023-08-05 19:02 UTC
Requires
- php: >=8.1
- infira/errorhandler: ^2.1
- infira/wolo: ^1
- symfony/cache: ^v6.2
README
快速链接到 Symfony 缓存解决方案
该项目遵循 MIT 许可证。驱动程序
您可以在此处GitHub上评论、提出更改或贡献,或者向gen@infira.ee提问
目录
- [安装/配置]
- 示例
安装/配置
基本配置
$ composer require infira/cachly
require_once 'vendor/autoload.php'; use Infira\Cachly\Cachly; Cachly::configure([ 'defaultAdapter' => Cachly::SESS, 'memAdapter' => Cachly::REDIS, 'defaultInstanceName' => 'cachly-production', ]);
选项
defaultAdapter
- 当访问 Cachly::$instanceMethod(...) 时将使用的适配器memAdapter
- 当访问 Cachly::mem()->$instanceMethod 时将使用的适配器defaultInstanceName
- 默认命名空间Cachly::setPropertyInstances
- 用于访问 Cachly::$sess 实例
配置 PDO/database 适配器适配器
Cachly::configureDbAdapter([ 'dsn' => 'mysql:host=mysql.docker;dbname=cachly', 'table' => 'cachly_cache2', 'user' => 'root', 'password' => 'parool', ]);
配置 session 适配器适配器
Cachly::configureSessionAdapter(static function($namespace) { Session::init(); return new \Infira\Cachly\Adapter\SessionAdapter($namespace); });
配置文件系统适配器
Cachly::configureFileSystemAdapter([ 'directory' => __DIR__ . '/fileCache' ]);
配置内存适配器
配置 memcached 适配器
Cachly::configureMemcachedAdapter([ 'host' => 'memcached://my.server.com:11211', 'options' => [ //... see https://symfony.com.cn/doc/current/components/cache/adapters/memcached_adapter.html#configure-the-options ] ]);
配置 redis 适配器
Cachly::configureRedisAdapter([ 'host' => 'redis://:6379', 'options' => [ //... see https://symfony.com.cn/doc/current/components/cache/adapters/redis_adapter.html#configure-the-options ] ]);
设置默认内存适配器
Cachly::configure([ //... 'memAdapter' => Cachly::REDIS ]); Cachly::setPropertyInstances([ Cachly::MEM => static fn() => Cachly::mem(), ]); //now you can access Cachly::$sess->put(...) //or Cachly::sess('new instance')->put(...)
配置您自己的适配器(基本)
@see https://symfony.com.cn/doc/current/components/cache.html 获取更多信息
Cachly::configureAdapter('myAdapter', function (string $namespace) { return MyAwesomeAdapter($namespace); }); //accessing instance Cachly::instance('cachly','myAdapter')->set('myKey','myValue');
配置您自己的适配器(带快捷方式)
class MyCachly extends Cachly { public static \Infira\Cachly\CacheInstance $myAdapter; public static function configure(array $options = []): void { parent::configure($options); Cachly::configureAdapter( 'myAdapter', function(string $namespace) { return MyAwesomeAdapter($namespace); }); static::$myAdapter = static::myAdapterInstance(); } public static function myAdapterInstance(string $namespace = null): \Infira\Cachly\CacheInstance { return static::instance($namespace, 'myAdapter') } } //now you can use MyCachly::configure(); MyCachly::$myAdapter->set('myKey', 'value'); //or creating new instance MyCachly::myAdapterInstance('new instance')->set('myKey', 'value');
快捷方式
Cachly::setPropertyInstances([ Cachly::SESS => static fn() => Cachly::sess(), Cachly::DB => static fn() => Cachly::db(), Cachly::FILE => static fn() => Cachly::file(), ]); $Cachly::$sess->put(...);
示例
选择您喜欢的
use Infira\Cachly\CacheItem; if (!Cachly::has('myKey')) { Cachly::put('myKey', 'my Value', '+1 day'); //save value immediately and will expire after one day } Cachly::get('key','defaultValue'); //defaultValue Cachly::get('key'); //null Cachly::put('key','stored value'); //CacheItem Cachly::get('key','defaultValue'); //stored value Cachly::get('key'); //stored value //run callback when value does not exist Cachly::get('compute-key','defaultValue'); //defaultValue Cachly::get('compute-key'); //null Cachly::get('compute-key', function (CacheItem $item) { $item->expiresAfter(3600); // do computations $computedValue = 'foobar'; return $computedValue; }); Cachly::get('compute-key','defaultValue'); //foobar Cachly::get('compute-key'); //foobar $item = Cachly::set('array', ['init value']); $item[] = 'value2'; $item['key'] = 'value3'; Cachly::get('array'); //foobar
使用方法参数作为键
使用从任何值到计算值的自动哈希
function filterItems(DateTimeInterface $date, array $filters): mixed { //add as many variables as you want as long last variable is callable return Cachly::once('getDataFromDataBase', $date, $filters, function (CacheItem $item) use ($date, $filters) { $item->expiresAfter(3600); return db()->where('date', $date)->where($filters); }); }
延迟
$item = Cachly::set('key','value1')->expire('tomorrow'); $item->set('value2') Cachly::get('key'); //value2 Cachly::setMany(['key1'=> 'value1','key2' => 'value2']); Cachly::commit(); //save all deferred items Cachly::get('key'); //value2
保存、提交、自动保存
$item = Cachly::set('key2','value1')->expire('tomorrow')->save(); //saves value to database $item->save(); //will not save because nothing has changed since last save $item->commit(); //will save value without checking changes $item->set('newValue')->save(); //will save $item->set('newValue')->save(); //do nothing $item->expire(5)->save(); //will save $autoSave = Cachly::set('key2','value1')->autoSave(true); //tries save after every change $autoSave->set('new value'); //will call save() $autoSave->expire('tomorrow'); //will also call save
子实例
$sub = Cachly::sub('myCollectionName'); $sub->set('myKey1', 'value1')->expires('tomorrow'); $sub->set('myKey2', 'value2'); $sub->all(); //[] $sub->commit(); //save values $sub->all(); //['myKey1' => 'value1','myKey2' => 'value2'] $subOfSub = $sub::sub('subCollection'); $subOfSub->put('myKey1', 'value1'); $subOfSub->put('myKey2', 'value2'); $subOfSub->get('myKey2'); //outputs value2 $subOfSub->all(); //['myKey1' => 'value1','myKey2' => 'value2'] $sub::sub('subCollection')->get('myKey2'); //outputs value2 $subOfSub->all(); //['myKey1' => 'value1','myKey2' => 'value2']
为默认适配器使用新实例
Cachly::instance('newInstance')->put('key1', 'key1 value'); Cachly::instance('newInstance')->all(); //outputs /* Array ( [key1] => key1 value ) */
适配器实例快捷方式
- yourOwnShortcut - 查看如何制作自己的 快捷方式
Cachly::sess('mySessionInstance')->put('key1', 'key1 value'); Cachly::sess('mySessionInstance')->all(); Cachly::$sess->put('key1', 'key1 value'); Cachly::$sess->all(); Cachly::mem('mySessionInstance')->put('key1', 'key1 value'); Cachly::mem('mySessionInstance')->all(); Cachly::$mem->put('key1', 'key1 value'); Cachly::$mem->all(); ....