PHPackages                             molajo/cache - 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. molajo/cache

ActiveMolajo-package[Caching](/categories/caching)

molajo/cache
============

Cache API for PHP applications with APC, Database, File, Memcached, In-memory, Redis, Wincache, and xCache handlers.

v0.4(12y ago)64761[1 PRs](https://github.com/Molajo/Cache/pulls)MITPHPPHP &gt;=5.4

Since Jan 3Pushed 10y ago6 watchersCompare

[ Source](https://github.com/Molajo/Cache)[ Packagist](https://packagist.org/packages/molajo/cache)[ Docs](http://github.com/Molajo/Cache)[ RSS](/packages/molajo-cache/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (1)Versions (4)Used By (0)

======= Molajo Cache API
========================

[](#molajo-cache-api)

[![Build Status](https://camo.githubusercontent.com/c3b51bbeb7abb207af833502b856ccbdce43b0eb7a597055126188f9061980f8/68747470733a2f2f7472617669732d63692e6f72672f4d6f6c616a6f2f43616368652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/Molajo/Cache)

Simple, clean cache API for PHP applications to [get](https://github.com/Molajo/Cache#get), \[set\] (), \[remove\] (), \[clear\] (), cache.

Cache Handlers available include:

- [Apc](https://github.com/Molajo/Cache/Cache#apc),
- [Database](https://github.com/Molajo/Cache/Cache#database),
- [Dummy](https://github.com/Molajo/Cache/Cache#dummy),
- [File](https://github.com/Molajo/Cache/Cache#file),
- [Memcached](https://github.com/Molajo/Cache/Cache#memcached),
- [Memory](https://github.com/Molajo/Cache/Cache#memory),
- [Redis](https://github.com/Molajo/Cache/Cache#redis),
- [Wincache](https://github.com/Molajo/Cache/Cache#wincache), and
- [xCache](https://github.com/Molajo/Cache/Cache#xcache).

At a glance ...
---------------

[](#at-a-glance-)

1. Construct a Cache Handler Class.
2. Instantiate the Cache Adapter, injecting it with the Cache Handler instance.
3. Set cache.
4. Get cache.
5. Remove cache.
6. Clear cache.

```
    // 1. Instantiate a Cache Handler.
    $options = array();
    $options['cache_folder']  = SITE_BASE_PATH . '/' . $application->get('system_cache_folder', 'cache');
    $options['cache_time']    = $application->get('system_cache_time', 900);
    $options['cache_handler'] = $application->get('cache_handler', 'File');

    use Molajo\Cache\Adapter\File as FileCache;
    $adapter_handler = new FileCache($options);

    // 2. Instantiate the Adapter, injecting it with the Handler.
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

    // 3. Set cache.
    $adapter->set('key value', 'cache this value for seconds =>', 86400);

    // 4. Get Cache.
    $cacheItem = $adapter->get('this is the key for a cache item');

    if ($cacheItem->isHit() === false) {
        // deal with no cache
    } else {
        echo $cacheItem->value; // Use the Cached Value
    }

    // 5. Remove cache.
    $adapter->remove('key value');

    // 6. Clear cache.
    $adapter->clear();
```

Cache API
---------

[](#cache-api)

Common API for Cache operations: [get](https://github.com/Molajo/Cache/Cache#get), \[set\] (), \[remove\] (), \[clear\] () methods.

### Get

[](#get)

Retrieves a CacheItem object associated with the key. If the value is not found, an exception is thrown.

```
    try {
        $cacheItem = $adapter->get($key);

    } catch (Exception $e) {
        // deal with the exception
    }

    if ($cacheItem->isHit() === true) {
        $cached_value = $cacheItem->getValue();
    } else {
        // cache is not available - do what you have to do.
    }
```

**Parameters**

- **$key** contains the key value for the requested cache

### Set

[](#set)

Stores a value as cache for the specified Key value and number of seconds specified.

```
    try {
        $adapter->set($key, $value, $ttl);

    } catch (Exception $e) {
        // deal with the exception
    }
```

**Parameters**

- **$key** contains the value to use for the cache key
- **$value** contains the value to be stored as cache
- **$ttl** "Time to live" which is the number of seconds the cache is considered relevant

### Remove

[](#remove)

Removes a cache entry associated with the specified Key value.

```
    try {
        $adapter->remove($key);

    } catch (Exception $e) {
        // deal with the exception
    }
```

**Parameters**

- **$key** contains the value to use for the cache key

### Clear

[](#clear)

Remove all cache for this Cache Handler instance.

```
    try {
        $adapter->clear();

    } catch (Exception $e) {
        // deal with the exception
    }
```

**Parameters**

- **n/a** no parameters are required

Cache Adapter Handlers
----------------------

[](#cache-adapter-handlers)

Cache Handlers available include:

- [Apc](https://github.com/Molajo/Cache/Cache#apc),
- [Database](https://github.com/Molajo/Cache/Cache#database),
- [Dummy](https://github.com/Molajo/Cache/Cache#dummy),
- [File](https://github.com/Molajo/Cache/Cache#file),
- [Memcached](https://github.com/Molajo/Cache/Cache#memcached),
- [Memory](https://github.com/Molajo/Cache/Cache#memory),
- [Redis](https://github.com/Molajo/Cache/Cache#redis),
- [Wincache](https://github.com/Molajo/Cache/Cache#wincache), and
- [xCache](https://github.com/Molajo/Cache/Cache#xcache).

### Apc

[](#apc)

APC (Alternative PHP Cache) comes standard with PHP. An APC Cache Handler is available with *Molajo Cache* and can be used, as follows.

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Apc;
    $adapter_handler = new Apc($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### Database

[](#database)

Before using the *Database Cache Handler*, a table must be available with four columns: id (identity column), key (varchar(255)), value (text) and expiration (integer). When instantiating the Cache Handler, pass in the database connection, the name of the database table for cache, the value for the RDBMS quote and name quote, as shown in this example.

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the Database Handler
    $options['database_connection'] = $connection;
    $options['database_table']      = 'xyz_cache_table';
    $options['database_quote']      = "'";
    $options['database_namequote']  = '`';

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Database;
    $adapter_handler = new Database($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### Dummy

[](#dummy)

The *Dummy Cache Handler* can be used for testing purpose. It does not really cache data. Use, as follows:

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Dummy as DummyCache;
    $adapter_handler = new DummyCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### File

[](#file)

The *File Cache Handler* can be used to turn the local filesystem into a caching device. Use, as follows:

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the File Handler
    $options['cache_handler']       = '/Absolute/Path/To/Cache/Folder';

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\File as FileCache;
    $adapter_handler = new FileCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### Memcached

[](#memcached)

The *Memcached Cache Handler* requires the `memcached` PHP extension be loaded and that the `Memcached`class exists. For more information, see [Memcached in the PHP Manual](http://us1.php.net/manual/en/book.memcached.php). Use, as follows:

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the Memcached Handler
    $options['memcached_pool']         = $connection;
    $options['memcached_compression']  = 'xyz_cache_table';
    $options['memcached_servers']      = "'";

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Memcached
    $adapter_handler = new Memcached($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### Memory

[](#memory)

The *Memory Cache Handler* can be used storing the variables in memory. This can be used with Sessions to create persistence, if desired. Use, as follows:

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Memory
    $adapter_handler = new Memory($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### Redis

[](#redis)

The *Redis Cache Handler* can be used storing the variables in memory. This can be used with Sessions to create persistence, if desired. Use, as follows:

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Redis
    $adapter_handler = new Redis($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### Wincache

[](#wincache)

The *Wincache Cache Handler* requires the PHP extension `wincache` is loaded and that `wincache_ucache_get` is callable. For more information, see [Windows Cache for PHP.](http://us1.php.net/manual/en/book.wincache.php). Besides using the Windows Operating System, there are no other configuration options required to use `Wincache`.

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Wincache
    $adapter_handler = new Wincache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

### xCache

[](#xcache)

The *xCache Handler* requires the PHP extension `xcache` is loaded and that `xcache_get` is callable.

```
    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\XCache
    $adapter_handler = new XCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);
```

Install using Composer from Packagist
-------------------------------------

[](#install-using-composer-from-packagist)

### Step 1: Install composer in your project

[](#step-1-install-composer-in-your-project)

```
    curl -s https://getcomposer.org/installer | php
```

### Step 2: Create a **composer.json** file in your project root

[](#step-2-create-a-composerjson-file-in-your-project-root)

```
{
    "require": {
        "Molajo/Cache": "1.*"
    }
}
```

### Step 3: Install via composer

[](#step-3-install-via-composer)

```
    php composer.phar install
```

Requirements and Compliance
---------------------------

[](#requirements-and-compliance)

- PHP framework independent, no dependencies
- Requires PHP 5.4, or above
- [Semantic Versioning](http://semver.org/)
- Compliant with:
    - [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) and [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md) Namespacing
    - [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) Coding Standards
    - [PSR-Cache Interfaces](https://github.com/php-fig/fig-standards/pull/96) (Still in Draft)
- \[phpDocumentor2\] ()
- \[phpUnit Testing\] ()
- Author [AmyStephen](http://twitter.com/AmyStephen)
- \[Travis Continuous Improvement\] ()
- Listed on \[Packagist\] () and installed using \[Composer\] ()
- Use github to submit [pull requests](https://github.com/Molajo/Cache/pulls) and [features](https://github.com/Molajo/Cache/issues)
- Licensed under the MIT License - see the `LICENSE` file for details

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity50

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

Total

3

Last Release

4420d ago

PHP version history (2 changes)v0.2PHP &gt;=5.3.10

v0.3PHP &gt;=5.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/95525?v=4)[AmyStephen](/maintainers/AmyStephen)[@AmyStephen](https://github.com/AmyStephen)

---

Top Contributors

[![AmyStephen](https://avatars.githubusercontent.com/u/95525?v=4)](https://github.com/AmyStephen "AmyStephen (91 commits)")

---

Tags

rediscachexcachememcachedapcwincacheMolajoCommonApi

### Embed Badge

![Health badge](/badges/molajo-cache/health.svg)

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

###  Alternatives

[phpfastcache/phpfastcache

PHP Abstract Cache Class - Reduce your database call using cache system. Phpfastcache handles a lot of drivers such as Apc(u), Cassandra, CouchBase, Couchdb, Dynamodb, Firestore, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ravendb, Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.

2.4k5.0M130](/packages/phpfastcache-phpfastcache)[tedivm/stash

The place to keep your cache.

9824.8M124](/packages/tedivm-stash)[tedivm/stash-bundle

Incorporates the Stash caching library into Symfony.

841.4M16](/packages/tedivm-stash-bundle)[ihor/cachalot

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

2528.1k](/packages/ihor-cachalot)[sabre/cache

Simple cache abstraction layer implementing PSR-16

541.2M3](/packages/sabre-cache)[jamm/memory

Key-value storage in memory. As a storage can be used: APC, Redis, Memcache, Shared memory. All storage objects have one interface, so you can switch them without changing the working code. Contains PHP Redis client.

13326.3k1](/packages/jamm-memory)

PHPackages © 2026

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