nilportugues/repository-cache

使用StashPHP实现带缓存层的基仓库

3.0.2 2016-08-08 23:32 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:43:04 UTC


README

PHP7 Tested Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

nilportugues/repository一起使用的仓库,实现PSR-6缓存层,使用StashPHP

安装

使用Composer安装此包

$ composer require nilportugues/repository-cache

缓存驱动程序

Repository Cache支持Stash的所有驱动程序

  • 系统
    • 文件系统
    • Sqlite(需要php-pdo-sqlite扩展)
    • APC(需要php-apcu扩展)
  • 服务器
    • Memcached(需要php-memcache或php-memcached扩展)
    • Redis(需要php-redis扩展)
  • 专用
    • 临时(内存缓存)
    • 组合

有关更多信息,请参阅Stash的文档

用法

use NilPortugues\Foundation\Infrastructure\Model\Repository\Cache\RepositoryCache;
use Stash\Driver\Ephemeral;
use Stash\Driver\Memcache;
use Stash\Pool;

//---------------------------------------------------------------------------
// Definition of cache drivers
//---------------------------------------------------------------------------
$memcached = new Memcache();
$memcached->setOptions(array('servers' => array('127.0.0.1', '11211')));

$cachePool = new Pool();
$cachePool->setDriver(new Ephemeral()); //hit in memory first as always will be faster
$cachePool->setDriver($memcached); //hit memcached second.

$ttl = 3600; //time in second for cache to expire.
$cacheNamespace = Color::class;

//---------------------------------------------------------------------------
// Adding cache to an existing repository
//---------------------------------------------------------------------------
$sqlRepository = new MySQLColorRepository();
$repository = new RepositoryCache($cachePool, $repository, $cacheNamespace, $ttl);

//---------------------------------------------------------------------------
// Now use as normal... 
//---------------------------------------------------------------------------
$color = new Color('#@@@@@@', 'New color');
$repository->add($color);
$repository->find(ColorId('#@@@@@@')); //should hit cache and return an instance of Color.

质量

要在命令行运行PHPUnit测试,请转到tests目录并运行phpunit。

此库尝试遵守PSR-1PSR-2PSR-4

如果您注意到合规性疏忽,请通过Pull Request发送补丁。

贡献

欢迎为该包做出贡献!

支持

通过以下方式之一与我联系

作者

许可证

代码库使用MIT许可证