tobento / app-cache
应用缓存支持。
1.0.1
2024-02-20 17:48 UTC
Requires
- php: >=8.0
- tobento/app: ^1.0
- tobento/app-file-storage: ^1.0
- tobento/app-migration: ^1.0
- tobento/service-cache: ^1.0
- tobento/service-clock: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- tobento/service-file-storage: ^1.0
- tobento/service-filesystem: ^1.0
- vimeo/psalm: ^4.0
README
应用缓存支持。
目录
入门
运行以下命令添加正在运行的应用缓存项目的最新版本。
composer require tobento/app-cache
要求
- PHP 8.0 或更高版本
文档
应用
如果您使用骨架,请查看 应用骨架。
您还可以查看 应用 以了解更多关于应用的一般信息。
缓存启动
缓存启动执行以下操作
- 安装和加载缓存配置文件
- 根据缓存配置实现 PSR-6 和 PSR-16 接口
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // Adding boots: $app->boot(\Tobento\App\Cache\Boot\Cache::class); // Run the app: $app->run();
您可以查看 缓存服务 以了解更多信息。
缓存配置
缓存配置位于默认应用骨架配置位置 app/config/cache.php
文件中,您可以在其中指定应用程序的池和缓存。
缓存使用
您可以通过几种方式访问池和缓存
使用应用
use Tobento\App\AppFactory; use Tobento\Service\Cache\CacheItemPoolsInterface; use Tobento\Service\Cache\Simple\CachesInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // Adding boots: $app->boot(\Tobento\App\Cache\Boot\Cache::class); $app->booting(); // PSR-6 cache: // using the default pool: $pool = $app->get(CacheItemPoolInterface::class); // using the pools: $pools = $app->get(CacheItemPoolsInterface::class); // PSR-16 simple cache: // using the default pool: $cache = $app->get(CacheInterface::class); // using the caches: $caches = $app->get(CachesInterface::class); // Run the app: $app->run();
查看 缓存项池接口 了解更多信息。
查看 缓存接口 了解更多信息。
使用自动注入
您还可以在任何由应用解析的类中请求接口
use Tobento\Service\Cache\CacheItemPoolsInterface; use Tobento\Service\Cache\Simple\CachesInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; class SomeService { public function __construct( protected CacheItemPoolsInterface $pools, protected CacheItemPoolInterface $pool, protected CachesInterface $caches, protected CacheInterface $cache, ) {} }
添加和注册缓存
您可以通过以下方式添加和注册更多池和缓存,而不是使用缓存配置文件
use Tobento\App\AppFactory; use Tobento\Service\Cache\CacheItemPoolsInterface; use Tobento\Service\Cache\Simple\CachesInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // PSR-6: $app->on(CacheItemPoolsInterface::class, function(CacheItemPoolsInterface $pools) { // using the add method: $pools->add(name: 'name', pool: $pool); // using the register method: $pools->register( name: 'name', pool: function(string $name): CacheItemPoolInterface { // create the pool: return $pool; }, ); }); // PSR-16: $app->on(CachesInterface::class, function(CachesInterface $caches) { // using the add method: $caches->add(name: 'name', cache: $cache); // using the register method: $caches->register( name: 'name', cache: function(string $name): CacheInterface { // create the cache: return $cache; }, ); }); // Adding boots: $app->boot(\Tobento\App\Cache\Boot\Cache::class); // Run the app: $app->run();
删除过期项
您可以使用任务管理器(在开发中)自动删除过期项。