germania-kg/namespaced-cache
Composer 安装命令:
composer require germania-kg/namespaced-cache
包简介
Factory classes for namespaced PSR-6 CacheItemPools
README 文档
README
Namespaced CacheItemPool Factory
Installation
$ composer require germania-kg/namespaced-cache
One of these libraries is required to be installed manually:
$ composer require symfony/cache $ composer require tedivm/stash
Interfaces
Factory Interface
Classes implementing the PsrCacheItemPoolFactoryInterface are callable. Their invokation method accepts a namespace string.
<?php Germania\NamespacedCache\PsrCacheItemPoolFactoryInterface; interface PsrCacheItemPoolFactoryInterface { public function __invoke( string $namespace) : \Psr\Cache\CacheItemPoolInterface; }
DefaultLifeTimeAware
Define a default lifetime for cache items. It can be used on those PSR-6 libraries that support default life times on cache item pools.
<?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); }
Example:
<?php use Germania\NamespacedCache\SymfonyFileCacheItemPoolFactory; use Germania\NamespacedCache\DefaultLifeTimeAware; $factory = new SymfonyFileCacheItemPoolFactory($directory); if ($factory instanceOf DefaultLifeTimeAware::class) { $factory->setDefaultLifetime( 3600 ); }
Auto-discovering
Abstract class PsrCacheItemPoolFactory provides a static autodiscover method which will create SQLite or Filesystem cache factory, depending on if SQLite being available.
This is an experimental feature.
<?php use Germania\NamespacedCache\PsrCacheItemPoolFactory; $factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path); $factory = PsrCacheItemPoolFactory::autodiscover($dsn_or_path, $default_lifetime);
Filesystem caches
Auto-discover Symfony Cache or Stash
Use this when migrating from one cache engine to another. It internally uses SymfonyFileCacheItemPoolFactory or StashFileCacheItemPoolFactory, whichever library is installed.
Callable class FileCacheItemPoolFactory implements 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 Cache Component
Callable class SymfonyFileCacheItemPoolFactory extends SymfonyCacheItemPoolFactory and implements PsrCacheItemPoolFactoryInterface and 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 Caching Library
Callable class StashFileCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface. Note that Stash caches do not provide setting default cache item lifetime.
<?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 Caches
Auto-discover Symfony Cache or Stash
Use this when migrating from one cache engine to another. It internally uses SymfonySqliteCacheItemPoolFactory or StashSqliteCacheItemPoolFactory, whichever library is installed.
Callable class SqliteCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.
Please note:
- Symfony/Cache requires a DSN string
- Stash/Cache requires a directory.
<?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 Cache Component
Callable class SymfonySqliteCacheItemPoolFactory extends SymfonyCacheItemPoolFactory and implements PsrCacheItemPoolFactoryInterface and 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 Caching Library
Callable class StashSqliteCacheItemPoolFactory implements PsrCacheItemPoolFactoryInterface.
Note that Stash caches do not provide setting default cache item lifetime.
<?php use Germania\NamespacedCache\StashSqliteCacheItemPoolFactory; # These are defaults $directory = getcwd(); $factory = new StashSqliteCacheItemPoolFactory(); $factory = new StashSqliteCacheItemPoolFactory($directory); // Psr\Cache\CacheItemPoolInterface $cache = $factory("my_namespace");
Testing
$ composer phpunit
germania-kg/namespaced-cache 适用场景与选型建议
germania-kg/namespaced-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.31k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「sqlite」 「psr-6」 「symfony-cache」 「namespaced」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 germania-kg/namespaced-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 germania-kg/namespaced-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 germania-kg/namespaced-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
repository php library
Simple PSR-16 cache implementations for WordPress transient the OOP way
The tenancy/tenancy database driver for sqlite
Stash Cache pool wrapper
Stash Cache middleware for Relay
统计信息
- 总下载量: 1.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-18