nilportugues / cache
使用责任链模式的 PHP 应用程序缓存层
Requires
- php: >=5.4
- nilportugues/php_backslasher: ^0.2.1
- predis/predis: ~1.0
Requires (Dev)
- fabpot/php-cs-fixer: dev-master
- fiunchinho/phpunit-randomizer: 1.0.*@dev
- phpunit/phpunit: ~4.2
Suggests
- ext-curl: Native curl support for php.
- ext-memcached: Native memcached support for php.
- ext-redis: Native redis support for php.
This package is not auto-updated.
Last update: 2024-09-14 17:00:53 UTC
README
PHP 应用程序缓存层,能够独立使用或与责任链模式结合使用。
1. 安装
推荐通过 Composer 安装此软件包。运行以下命令进行安装
php composer.phar require nilportugues/cache
2. 功能
- 一个缓存类,多个适配器。
- 所有缓存适配器都可以作为独立的缓存类使用。
- 选择作为缓存链使用。
- 实现标准化所有机制和行为。
- 在
config
和migrations
目录中提供所有适配器的配置文件,格式为纯 PHP 和 Symfony2。 - 高质量、100% 测试过的代码。
3. 可用驱动器
此软件包为键值缓存提供多个实现。
最敏感的选择是使用基于缓存的适配器,如 Redis 和 Memcached,它们性能最好且最专业。
然而,有时这些可能不可用,应考虑其他选项,但我们已经为您准备好了。
基于缓存
- Memcached: MemcachedAdapter (php5-memcached)
- Redis: RedisAdapter (php5-redis), PredisAdapter (Predis)
基于全文搜索
- SphinxQL: SphinxAdapter
- ElasticSearch: ElasticSearchAdapter
基于系统
- 内存: InMemoryAdapter
- 文件系统: FileSystemAdapter
基于数据库
- MySQL: MySqlAdapter
- PostgreSql: PostgreSqlAdapter
- Sqlite: SqliteAdapter
4. 缓存和 CacheAdapter 接口
以下是缓存及其所有可用适配器的所有公共方法
- get($key): 通过 $key 获取值。
- set($key, $value, $ttl = 0): 设置由 $key 标识的值,并可选地设置 $ttl。
- defaultTtl($ttl): 允许为 set() 设置默认的 ttl 值,如果未提供。
- delete($key): 删除由 $key 标识的值。
- isAvailable(): 检查缓存服务的可用性。
- isHit(): 检查值是否在缓存中。
- clear(): 清除缓存中所有过期的值。
- drop(): 清除缓存中的所有值。
4.1 - CacheAdapter
允许使用 Cache 接口定义的所有公共方法。
每个 CacheAdapter 将有一个定制的构造方法,以满足其需求,但 最后一个参数始终是一个可链的值,即另一个 CacheAdapter。
5. 示例
The more cache levels the slower the cache system will be, so leverage the
cache to your needs.
Maybe you don't need a fallback mechanism at all! This is just an example.
第一级缓存 Redis (PredisAdapter) 是我们的主要缓存,位于专用服务器上。
第二级缓存 Memcached (MemcachedAdapter) 作为后备机制,位于与我们的 PHP 脚本相同的机器上。
应用程序缓存 InMemoryAdapter,用于避免在重复操作中多次访问外部缓存,并由所有缓存层共享。此功能默认启用,因此您无需担心。
5.1. 配置
使用服务容器,如返回服务的数组或更流行的解决方案,例如 Symfony 的服务容器,构建缓存。
在这个示例中,我们将构建两个缓存,分别是 user_cache 和 image_cache。两者都使用 Predis 作为一级缓存,如果 Predis 在运行时无法建立连接,则回退到 Memcached。
<?php include_once realpath(dirname(__FILE__)).'/vendor/autoload.php'; use NilPortugues\Cache\Adapter\MemcachedAdapter; use NilPortugues\Cache\Adapter\PredisAdapter; use NilPortugues\Cache\Cache; $parameters = include_once realpath(dirname(__FILE__)).'/cache_parameters.php'; $cache = new PredisAdapter( $parameters['redis'], //here we're chaining the $memcachedAdapter new MemcachedAdapter( $parameters['memcached']['persistent_id'], $parameters['memcached']['connections'] ); ); return [ 'user_cache' => new Cache($cache, 'user', 60*5), //5 minutes cache 'image_cache' => new Cache($cache, 'image', 60*60), //1 hour cache ];
5.2. 使用方法
现在,使用服务容器,我们将获取 user_cache 来获取数据,如果不存在则添加。这些数据将存储在缓存中。
对于获取,首先检查数据是否在内存中可用,如果不在,则从数据存储中获取,添加到内存缓存中,并返回给用户。
$db = $this->serviceContainer->get('database'); $cache = $this->serviceContainer->get('cache'); $userId = 1; $cacheKey = sprintf("user:id:%s", $userId); $user = $cache->get($cacheKey); if(null !== $user) { return $user; } $user = $db->findById($userId); $cache->set($cacheKey, $user); return $user;
就是这样。注意获取和设置方法使用了相同的键。
5.3 其他配置
5.3.1 使用 ElasticSearch 作为缓存
您需要配置 ElasticSearch,方法是在 elasticsearch.yml 文件中追加以下行
indices.ttl.interval: 1s
现在重新启动 ElasticSearch 守护进程。
如果您想知道缓存索引定义在哪里,如果索引不存在,则适配器在实例化时处理索引的创建。
5.3.2 使用 Sphinx 作为缓存
配置在 /migrations/sphinx.conf
文件中。
5.3.3 使用 MySQL 作为缓存
配置在 /migrations/mysql_schema.sql
文件中。
5.3.4 使用 Postgres 作为缓存
配置在 /migrations/postgresql_schema.sql
文件中。
5.3.5 使用 Sqlite 作为缓存
配置在 /migrations/sqlite_schema.sqlite
文件中。
6. 质量保证
要运行 PHPUnit 测试,请前往测试目录并执行 phpunit。
此库试图遵守 PSR-1、PSR-2 和 PSR-4。如果您注意到有违反规定的部分,请通过 pull request 发送补丁。
7. 作者
Nil Portugués Calderó
8. 许可证
代码库遵循 MIT 许可证。