PHPackages                             juneym/zf2-cache-mongo - 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. juneym/zf2-cache-mongo

ActiveLibrary[Caching](/categories/caching)

juneym/zf2-cache-mongo
======================

ZF2 cache storage compatible library using MongoDB's TTL collection

0.5.0(9y ago)01.3kMITPHPPHP &gt;=5.3.23

Since Sep 2Pushed 9y ago1 watchersCompare

[ Source](https://github.com/juneym/zf2-cache-mongo)[ Packagist](https://packagist.org/packages/juneym/zf2-cache-mongo)[ Docs](https://github.com/juneym/zf2-cache-mongo)[ RSS](/packages/juneym-zf2-cache-mongo/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (10)Used By (0)

zf2-cache-mongo
===============

[](#zf2-cache-mongo)

ZF2 cache storage compatible library using MongoDB's TTL collection

Overview
========

[](#overview)

It seems that there aren't that many people out there who is interested in creating a ZF2 compatible cache storage backend library for MongoDB, hence this project.

The library utilizes the Time To Live (TTL) collection feature introduced in MongoDB v2.2

Using the Library
=================

[](#using-the-library)

1. Update your `composer.json` (if you have)

    (to be added)
2. Instantiate the library

    $options = array( 'dsn' =&gt; 'mongodb://127.0.0.1', 'mongoOptions' =&gt; array(/\* any of the valid \\Mongo or \\MongoClient options \*/), 'dbname' =&gt; 'cachedb', 'collection' =&gt; 'cache', 'ttl' =&gt; 10, 'namespace' =&gt; 'stl' );

    $mongoCache = new \\Juneym\\Cache\\Storage\\Adapter\\Mongo($options); $cacheKey = md5('This is a sample key');

    $created = $mongoCache-&gt;setItem($cacheKey, array('x' =&gt; 12345, 'y' =&gt; 'ABCDEF' . rand(0,10000))); if (!$created) { die("Cached using key: " . $cacheKey . "\\n"); }

    $data = $mongoCache-&gt;getItem($cacheKey); print\_r($data); unset($mongoCache);

Cache Item Attributes
=====================

[](#cache-item-attributes)

There are applications that require additional metadata as part of the cache record in MongoDB such as "current page addres" or "remote host ip". To do this, use the `setCacheItemAttributes(array)` to attach an array attributes to a cache item. When the item is retrieve with an attribute, the data is accessible via the `attr` key. Following is a snippet from the tests file.

```
    public function testCanAttachCacheItemAttributes()
    {
        $mongoCache = new Storage\Adapter\Mongo($this->options);

        $cacheKey = md5('this is a test key with attributes' . __METHOD__);
        $itemAttribute = array(
            'current_page' => 'http://hello-world.com/about-us.html',
            'browser' => 'Firefox'
        );
        $mongoCache->setCacheItemAttributes($itemAttribute);
        $result = $mongoCache->setItem($cacheKey, array('x' => 11111, 'y' => 'ABCDEF' . rand(0,10000)));
        $this->assertTrue($result);
        $data = $mongoCache->getItem($cacheKey);

        $this->assertArrayHasKey('attr', $data);
        $this->assertEquals($itemAttribute['current_page'], $data['attr']['current_page']);
        $this->assertEquals($itemAttribute['browser'], $data['attr']['browser']);

        $itemAttribute1 = $mongoCache->getCacheItemAttributes();
        $this->assertTrue(is_array($itemAttribute1));
        $this->assertTrue(empty($itemAttribute1));

        unset($mongoCache);
    }
```

About TTL Index &amp; Cache Expiry
==================================

[](#about-ttl-index--cache-expiry)

There are two ways a cached data will expire.

1. When the difference between the current time and the cache item's `created` time is more than the cache item's `ttl` value (in seconds)
2. When the record's `created` value is way past the MongoDB's cache collection TTL index (`expireAfterSeconds`). Note that MongoDB's garbage collection runs every 60 seconds so don't be surprised if the cached item is still available. MongoDB's garbage collector will eventually remove all qualified records in the background.

Required Index
==============

[](#required-index)

Assuming that the cache database is called "cachedb" and the collection name is "cache", fhe following indexes are required:

```
use cachedb
db.cache.ensureIndex({ns:1}, {background:true});
db.cache.ensureIndex({ns:1, key:1}, {background:true});
db.cache.ensureIndex({ns:1, tags:1}, {background:true});

```

If you are using version below v0.2.0, please use the following TTL index definition:

```
db.cache.ensureIndex({created:1}, {background:true, expireAfterSeconds: 3600, name: 'colRecordTTl'});

```

Starting in v0.2.0, the `expireAt` field is populated based on the `ttl` value at the time the cache entry has been created (or saved). Use the following index to enforce automatic expiration of record based on `expireAt` field.

```
db.cache.ensureIndex({created:1, expireAt:1}, {background: true});
db.cache.ensureIndex({expireAt:1}, {expireAfterSeconds: 0, name: 'cache_expire_at'});

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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 ~82 days

Recently: every ~138 days

Total

9

Last Release

3606d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

cachezf2mongo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/juneym-zf2-cache-mongo/health.svg)

```
[![Health](https://phpackages.com/badges/juneym-zf2-cache-mongo/health.svg)](https://phpackages.com/packages/juneym-zf2-cache-mongo)
```

###  Alternatives

[psr/simple-cache

Common interfaces for simple caching

8.1k727.3M2.1k](/packages/psr-simple-cache)[psr/cache

Common interface for caching libraries

5.2k686.9M1.3k](/packages/psr-cache)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[stroker/cache

Provides a full page cache solution for Laminas

6444.7k](/packages/stroker-cache)[beste/in-memory-cache

A PSR-6 In-Memory cache that can be used as a fallback implementation and/or in tests.

2512.2M6](/packages/beste-in-memory-cache)[rtcamp/nginx-helper

Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also provides cloudflare edge cache purging with Cache-Tags.

23817.0k1](/packages/rtcamp-nginx-helper)

PHPackages © 2026

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