该软件包最新版本(v1.2)没有可用的许可证信息。

一个符合PSR的缓存库,用于在嵌套池中存储对象,具有脚本能力。

v1.2 2014-05-19 05:24 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:12:53 UTC


README

Build Status

这是什么?

Hoard是一个缓存库。

Hoard的目标是

  • 完全独立于任何其他框架。
  • 遵守PSR-6: Cache(更多信息这里)以使其与任何现代框架兼容。
  • 支持多种存储适配器(如memcached)。
  • 支持多种离散池和嵌套池。
  • 提供一种使用易于使用和与应用程序版本控制相结合的脚本来自动化操作的方法。
  • 一个命令行实用程序来运行这些脚本。

基本用法

池是类

您可以使用PSR-6标准明确地获取和设置缓存中的项。但是,为了创建物理池,有一些代码要求。

每个池都由一个扩展\Hoard\AbstractPool的具体类表示。嵌套池将是扩展其父池类的类,而不是\Hoard\AbstractPool

池名称直接双向转换为类名称。以下是一些示例(池名称 => 类名称)

  • my_pool => MyPool
  • parent.child_pool => Parent\ChildPool

池位于由\Hoard\CacheManager::getNamespace()提供的特定命名空间中,截至撰写本文档时,是\Hoard\Hoard\Pool。从上面的例子中扩展;

  • my_pool => \HoardPool\MyPool
  • parent.child_pool => \HoardPool\Parent\ChildPool

永远不要实例化这些类。您必须使用CacheManager检索池实例。

try {
    $pool = \Hoard\CacheManager::getPool('my.pool');
}
catch(\Hoard\NoSuchPoolException $e) {
    // deal with this appropriately
}

存储和检索

一旦您有了池实例(从上一个示例),您就可以

$item = $pool->getItem('key');
if($item->isHit()) {
    echo "Got: {$item->get()}";
}
else {
    echo "Item {$item->getKey()} is not in the cache.";
}    

无论该项之前是否存储在缓存中,您以相同的方式将数据保存到缓存

$item->set('myvalue')

对于set()有一个可选的第二个参数,允许您指定过期时间

这些永远不会过期(默认值)

$item->set('myvalue');       
$item->set('myvalue', null);

这将从现在起1小时后过期

$item->set('myvalue', 3600);

这将过期在特定的时间戳

$item->set('myvalue', new \DateTime('4th March 2015'));

清除缓存

要从池中删除单个项

$pool->getItem('key')->delete();

您可能想要删除整个池

$pool->clear();

使用缓存脚本

hoard脚本只是一个文本文件(扩展名不重要,尽管.txt是最容易使用的),它指定了可以从命令行实用程序运行的目标和命令。

语法类似于Makefile,目标有依赖关系,但命令前的缩进可以是任何空白。

像Makefile一样运行目标,通过./g脚本

./g hoard mytarget

语法

池名称很重要,因为它们直接链接到具体类,尝试对不存在的池执行任何操作将导致异常(脚本将停止执行并返回非0退出代码)。

删除整个池(及其所有子池)

drop my.pool_name

在池内删除单个键

drop my.pool:key1,key2,key3

注释是任何行,或任何以#开始的行的部分

# this is a comment
drop my.pool_name # this is also fine

完整示例

# my cool script
safe_drop:
    # this are items that may be safely dropped at any time and should be
    # dropped with any change to the software
    drop page:homepage

release: safe_drop
    # should be run with every production release

像这样运行release目标

$ ./g hoard release
Running target 'safe_drop'
  drop page:homepage... Done
Success.
Running target 'release'
  Nothing to do.
Success.

执行./g hoard命令将运行位于install-script/hoard.txt的脚本。

模拟一个池

有一个\Hoard\PoolTestCase可以使用,它可以生成预填充数据的模拟池。这比其他方法要简单。

class MyTest extends \Hoard\PoolTestCase
{

    public function testMockingPool()
    {
        // create mock
        $pool = $this->getMockedPool(array(
            'mykey' => 'myvalue'
        ));

        // found item
        $item = $pool->getItem('mykey');
        $this->assertEquals($item->get(), 'myvalue');
        $this->assertTrue($item->isHit());

        // not found item
        $item = $pool->getItem('mykey2');
        $this->assertFalse($item->isHit());
    }

}