PHPackages                             elliotchance/hoard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. elliotchance/hoard

ActiveLibrary[Caching](/categories/caching)

elliotchance/hoard
==================

A PSR-compliant caching library for holding objects in nested pools with scripting ability.

v1.2(11y ago)2694[4 issues](https://github.com/elliotchance/Hoard/issues)PHPPHP &gt;=5.3.0

Since Apr 22Pushed 11y ago1 watchersCompare

[ Source](https://github.com/elliotchance/Hoard)[ Packagist](https://packagist.org/packages/elliotchance/hoard)[ RSS](/packages/elliotchance-hoard/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (4)Versions (9)Used By (0)

Hoard
=====

[](#hoard)

[![Build Status](https://camo.githubusercontent.com/6dff39c973e62687864ba0bd93a3a4ea70402da2389d30cde6d79564de85c82b/68747470733a2f2f7472617669732d63692e6f72672f656c6c696f746368616e63652f486f6172642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/elliotchance/Hoard)

What Is It?
-----------

[](#what-is-it)

Hoard is a caching library.

Hoard's goal are:

- To be totally independant from any other framework.
- To be [PSR-6: Cache](https://github.com/php-fig/fig-standards/pull/149) (more information [here](https://github.com/Crell/fig-standards/blob/76527139cad588c072beaedc116438e46ba2f70b/proposed/cache.md)) compliant so that it should work with any modern framework.
- Support multiple adapters for storage (like memcached).
- Support multiple discreet pools, and nested pools.
- Provide a way to automate actions with a script that is easy to use and version controlled with the application.
- A command line utility to run these scripts.

Basic Usage
-----------

[](#basic-usage)

### Pools are Classes

[](#pools-are-classes)

You may follow the PSR-6 standard explicitely for getting and setting items in the cache. However there are some code requirements for you to be able to create the physical pool.

Each pool is represented by a concrete class that extends `\Hoard\AbstractPool`. Nested pools will be class that extend their parent pool class, **not** `\Hoard\AbstractPool`.

Pool names translate directly and both ways between the class name. With the following examples (pool name =&gt; class name):

- `my_pool` =&gt; `MyPool`
- `parent.child_pool` =&gt; `Parent\ChildPool`

Pools are located in a specific namespace provided by `\Hoard\CacheManager::getNamespace()`, which as of the writing of this document, is `\Hoard\Hoard\Pool`. Extending from the above examples;

- `my_pool` =&gt; `\HoardPool\MyPool`
- `parent.child_pool` =&gt; `\HoardPool\Parent\ChildPool`

**Never instantiate the classes themselfs. You must use the `CacheManager` to retrieve the pool instances.**

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

```

### Storing and Retrieving

[](#storing-and-retrieving)

Once you have the pool instance (from the previous example), you can:

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

```

Regardless of whether the item was stored in the cache previously, you save data to the cache the same way:

```
$item->set('myvalue')

```

There is an optional second argument for `set()` that allows you to specify a expiry time:

These will never expire (default):

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

```

This will expire in 1 hour from now:

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

```

This will expire at the specific timestamp:

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

```

### Clearing Cache

[](#clearing-cache)

To delete a single item from a pool:

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

```

You may want to drop an entire pool:

```
$pool->clear();

```

Using a Cache Script
--------------------

[](#using-a-cache-script)

A hoard script is just a text file (the extension is not important, although `.txt` is the easiest to use) which specifies targets and commands that can be run from a command line utility.

The syntax is similar to a Makefile, targets have dependencies, except that indentation can be any white space before a command.

Run targets like you would a Makefile, through the `./g` script:

```
./g hoard mytarget

```

### Syntax

[](#syntax)

Pool names are important because they link directly to concrete classes, trying to perform any action on a pool that doesn't exist will result in an exception (the script will stop execution and return a non 0 exit code).

Drop an entire pool (and all of its child pools):

```
drop my.pool_name

```

Drop individual keys inside a pool:

```
drop my.pool:key1,key2,key3

```

Comments are any line, or part of a line that comes after a `#`:

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

```

### Full Example

[](#full-example)

```
# 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

```

Running the `release` target like this:

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

```

The `./g hoard` command runs the script located at `install-script/hoard.txt`.

Mocking a Pool
--------------

[](#mocking-a-pool)

There is a `\Hoard\PoolTestCase` you can use to generate mocked pools that have prepopulated data. This is easier than trying to do it in other ways.

```
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());
    }

}

```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

8

Last Release

4379d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b974b28504bc5d48c1ad9220dedb4eef72b877ea1bd14d37539ff059280a51db?d=identicon)[chancey](/maintainers/chancey)

---

Top Contributors

[![elliotchance](https://avatars.githubusercontent.com/u/927418?v=4)](https://github.com/elliotchance "elliotchance (20 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/elliotchance-hoard/health.svg)

```
[![Health](https://phpackages.com/badges/elliotchance-hoard/health.svg)](https://phpackages.com/packages/elliotchance-hoard)
```

###  Alternatives

[pdffiller/qless-php

PHP Bindings for qless

29113.2k1](/packages/pdffiller-qless-php)[mjphaynes/php-resque

Redis backed library for creating background jobs and processing them later.

228199.3k2](/packages/mjphaynes-php-resque)[ethanhann/redisearch-php

239113.6k1](/packages/ethanhann-redisearch-php)[nlmdev/hoard

A PSR-compliant caching library for holding objects in nested pools with scripting ability.

107.7k](/packages/nlmdev-hoard)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
