germania-kg / namespaced-cache
命名空间PSR-6 CacheItemPools的工厂类
Requires
- php: ^7.3|^8.0
- nyholm/dsn: ^2.0
- psr/cache: ^1.0|^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.12
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12.67
- phpunit/phpunit: ^9.0
- symfony/cache: ^5.2
- tedivm/stash: ^0.16
Suggests
- symfony/cache: Pretty good cache library
- tedivm/stash: Another fine cache library
README
命名空间 CacheItemPool 工厂
安装
$ composer require germania-kg/namespaced-cache
以下库中需要手动安装一个
$ composer require symfony/cache $ composer require tedivm/stash
接口
工厂接口
实现了 PsrCacheItemPoolFactoryInterface 接口的类是可调用的。它们的调用方法接受一个 命名空间字符串。
<?php Germania\NamespacedCache\PsrCacheItemPoolFactoryInterface; interface PsrCacheItemPoolFactoryInterface { public function __invoke( string $namespace) : \Psr\Cache\CacheItemPoolInterface; }
默认生命周期感知
为缓存项定义默认生命周期。它可以用于支持在缓存项池上设置默认生命周期的PSR-6库。
<?php use Germania\NamespacedCache\DefaultLifeTimeAware; interface DefaultLifeTimeAware { /** * Returns default cache item lifetime. * * @return int|null */ public function getDefaultLifetime() : ?int; /** * Sets default cache item lifetime. * * @param int|null $lifetime Default cache item lifetime */ public function setDefaultLifetime( ?int $lifetime); }
示例
<?php use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory; use Germania\NamespacedCache\DefaultLifeTimeAware; $factory = new SymfonyFileCacheItemPoolFactory($directory); if ($factory instanceOf DefaultLifeTimeAware::class) { $factory->setDefaultLifetime( 3600 ); }
自动发现
抽象类 PsrCacheItemPoolFactory 提供了一个静态的 autodiscover 方法,它将创建 SQLite 或文件系统 缓存 工厂,具体取决于 SQLite 是否可用。
这是一个实验性功能。
<?php use Germania\NamespacedCache\PsrCacheItemPoolFactory; $factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path); $factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path, $default_lifetime);
文件系统缓存
自动发现 Symfony Cache 或 Stash
在迁移到一个缓存引擎到另一个时使用此功能。它内部使用 SymfonyFileCacheItemPoolFactory 或 StashFileCacheItemPoolFactory,取决于已安装的库。
可调用的类 FileCacheItemPoolFactory 实现了 PsrCacheItemPoolFactoryInterface。
<?php use Germania\NamespacedCache\FileCacheItemPoolFactory; # These are defaults $directory = getcwd(); $default_lifetime = 0; $factory = new FileCacheItemPoolFactory(); $factory = new FileCacheItemPoolFactory($directory, $default_lifetime); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace"); echo get_class($cache); // "Stash\Pool" or // "Symfony\Component\Cache\Adapter\FilesystemAdapter"
Symfony 缓存组件
可调用的类 SymfonyFileCacheItemPoolFactory 继承了 SymfonyCacheItemPoolFactory 并实现了 PsrCacheItemPoolFactoryInterface 和 DefaultLifeTimeAware
<?php use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory; # These are defaults $directory = getcwd(); $default_lifetime = 0; $factory = new SymfonyFileCacheItemPoolFactory(); $factory = new SymfonyFileCacheItemPoolFactory($directory, $default_lifetime); $factory = (new SymfonyFileCacheItemPoolFactory($directory)) ->setDefaultLifetime( 3600 ); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace");
Stash PHP 缓存库
可调用的类 StashFileCacheItemPoolFactory 实现了 PsrCacheItemPoolFactoryInterface。注意,Stash 缓存不提供设置默认缓存项生命周期的功能。
<?php use Germania\NamespacedCache\StashFileCacheItemPoolFactory; # These are defaults $directory = getcwd(); $factory = new StashFileCacheItemPoolFactory(); $factory = new StashFileCacheItemPoolFactory($directory); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace");
SQLite 缓存
自动发现 Symfony Cache 或 Stash
在迁移到一个缓存引擎到另一个时使用此功能。它内部使用 SymfonySqliteCacheItemPoolFactory 或 StashSqliteCacheItemPoolFactory,取决于已安装的库。
可调用的类 SqliteCacheItemPoolFactory 实现了 PsrCacheItemPoolFactoryInterface。
请注意
- Symfony/Cache 需要一个 DSN 字符串
- Stash/Cache 需要一个目录。
<?php use Germania\NamespacedCache\SqliteCacheItemPoolFactory; # These are defaults $directory_or_dsn = getcwd(); $default_lifetime = 0; $factory = new SqliteCacheItemPoolFactory($directory_or_dsn); $factory = new SqliteCacheItemPoolFactory($directory_or_dsn, $default_lifetime); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace"); echo get_class($cache); // "Stash\Pool" or // "Symfony\Component\Cache\Adapter\PdoAdapter"
Symfony 缓存组件
可调用的类 SymfonySqliteCacheItemPoolFactory 继承了 SymfonyCacheItemPoolFactory 并实现了 PsrCacheItemPoolFactoryInterface 和 DefaultLifeTimeAware。
<?php use Germania\NamespacedCache\SymfonySqliteCacheItemPoolFactory; # These are defaults $dsn_or_directory = "sqlite::memory:"; $dsn_or_directory = "sqlite:/path/to/mydb.sq3"; $dsn_or_directory = "/tmp"; $default_lifetime = 0; $factory = new SymfonySqliteCacheItemPoolFactory(); $factory = new SymfonySqliteCacheItemPoolFactory($dsn_or_directory, $default_lifetime); $factory = (new SymfonySqliteCacheItemPoolFactory($dsn_or_directory)) ->setDefaultLifetime( 3600 ); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace");
Stash PHP 缓存库
可调用的类 StashSqliteCacheItemPoolFactory 实现了 PsrCacheItemPoolFactoryInterface。
注意,Stash 缓存不提供设置默认缓存项生命周期的功能。
<?php use Germania\NamespacedCache\StashSqliteCacheItemPoolFactory; # These are defaults $directory = getcwd(); $factory = new StashSqliteCacheItemPoolFactory(); $factory = new StashSqliteCacheItemPoolFactory($directory); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace");
测试
$ composer phpunit