PHPackages                             greg-md/php-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. greg-md/php-cache

ActiveLibrary[Caching](/categories/caching)

greg-md/php-cache
=================

A powerful Cache for PHP.

02391[1 PRs](https://github.com/greg-md/php-cache/pulls)PHPCI failing

Since Jul 24Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/greg-md/php-cache)[ Packagist](https://packagist.org/packages/greg-md/php-cache)[ RSS](/packages/greg-md-php-cache/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (2)Used By (0)

Greg PHP Cache
==============

[](#greg-php-cache)

[![StyleCI](https://camo.githubusercontent.com/0b93b31a9af6466a1379c76ddb847cbce21ca6a1698c8ff6aa6d94447f84b84a/68747470733a2f2f7374796c6563692e696f2f7265706f732f37303030343536332f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/70004563)[![Build Status](https://camo.githubusercontent.com/6f21938302123a5d37a580da83633a7e04e78723908fe0d46c7c24444f8962c1/68747470733a2f2f7472617669732d63692e6f72672f677265672d6d642f7068702d63616368652e737667)](https://travis-ci.org/greg-md/php-cache)[![Total Downloads](https://camo.githubusercontent.com/af0bc1ded875949ae241df257b90b0ab074e49c6fcc6041d55fed45919458784/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d63616368652f642f746f74616c2e737667)](https://packagist.org/packages/greg-md/php-cache)[![Latest Stable Version](https://camo.githubusercontent.com/27035711a2776040d4f523abec4666a359e6a0ebe2e058956da164be87851927/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d63616368652f762f737461626c652e737667)](https://packagist.org/packages/greg-md/php-cache)[![Latest Unstable Version](https://camo.githubusercontent.com/8941e83b60684f31531e9f381cb035f287b42b9b09345c23540c2416740102e6/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d63616368652f762f756e737461626c652e737667)](https://packagist.org/packages/greg-md/php-cache)[![License](https://camo.githubusercontent.com/9cbb14d6ca5155674f97d953f399c74ce0a30055f4ff2c62ea6a090180d769cb/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d63616368652f6c6963656e73652e737667)](https://packagist.org/packages/greg-md/php-cache)

A powerful Cache for PHP.

Table of Contents:
==================

[](#table-of-contents)

- [Requirements](#requirements)
- [Supported Drivers](#supported-drivers)
- [How It Works](#how-it-works)
- [Cache Strategy](#cache-strategy)
- [License](#license)
- [Huuuge Quote](#huuuge-quote)

Requirements
============

[](#requirements)

- PHP Version `^7.1`

Supported Drivers
=================

[](#supported-drivers)

- **Array** - Use the PHP array as a storage;
- **Tmp** - Use temporary files as a storage. They are automatically removed when script ends;
- **File** - Use file based storage;
- **Memcached** - Use Memcached driver as a storage;
- **Redis** - Use Redis driver as a storage;
- **Sqlite** - Use Sqlite database as a storage.

How It Works
============

[](#how-it-works)

There are two ways of working with cache strategies. Directly or via a cache manager.

> A cache manager could have many cache strategies and a default one. The cache manager implements the same cache strategy and could act as default one if it's defined.

In the next example we will use a cache manager.

**First of all**, you have to initialize a cache manager and register some strategies:

```
$manager = new \Greg\Cache\CacheManager();

// Register a file cache
$manager->registerStrategy('store1', new \Greg\Cache\FileCache(__DIR__ . '/storage'));

// Register a sqlite cache
$manager->register('store2', function() {
    $pdo = new \PDO('sqlite:' . __DIR__ . '/storage/store2.sqlite');

    return new \Greg\Cache\SqliteCache($pdo);
});

// Register a redis cache
$manager->register('store3', function() {
    $redis = new \Redis();

    $redis->connect('127.0.0.1');

    return new \Greg\Cache\RedisCache($redis);
});
```

**Optionally**, you can define a default store to be used by the cache manager.

```
$manager->setDefaultStoreName('store2');
```

**Then**, you can **set** or **get** some data:

```
// Add some data in "store1"
$manager->store('store1')->set('foo', 'FOO');

// Add some data in default store, which is "store2"
$manager->set('bar', 'BAR');

// Get "bar" value from default store.
$value = $manager->get('bar'); // result: BAR
```

Cache Strategy
==============

[](#cache-strategy)

If you want, you can create your own strategies. They should implement the `\Greg\Cache\CacheStrategy` interface.

Below you can find a list of **supported methods**.

- [has](#has) - Determines whether an item is present in the cache;
- [hasMultiple](#hasmultiple) - Determines whether multiple items are present in the cache;
- [get](#get) - Fetch a value from the cache;
- [getMultiple](#getmultiple) - Obtains multiple cache items by their unique keys;
- [set](#set) - Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time;
- [setMultiple](#setmultiple) - Persists a set of `key => value` pairs in the cache, with an optional TTL;
- [forever](#forever) - Persists forever data in the cache, uniquely referenced by a key;
- [foreverMultiple](#forevermultiple) - Persists forever a set of `key => value` pairs in the cache;
- [delete](#delete) - Delete an item from the cache by its unique key;
- [deleteMultiple](#deletemultiple) - Delete multiple items from the cache by their unique keys;
- [clear](#clear) - Clear the storage;
- [remember](#remember) - Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist;
- [increment](#increment) - Increment a value;
- [decrement](#decrement) - Decrement a value;
- [incrementFloat](#incrementfloat) - Increment a float value;
- [decrementFloat](#decrementfloat) - Decrement a float value;
- [touch](#touch) - Set a new expiration on an item;
- [pull](#pull) - Retrieve and delete an item from the cache;
- [add](#add) - Persists data in the cache if it's not present.

has
---

[](#has)

Determines whether an item is present in the cache.

```
has(string $key): bool
```

`$key` - The cache item key.

*Example:*

```
$strategy->has('foo');
```

hasMultiple
-----------

[](#hasmultiple)

Determines whether an item is present in the cache.

```
hasMultiple(array $keys): bool
```

`$keys` - The cache items keys.

*Example:*

```
$strategy->hasMultiple(['foo', 'bar']);
```

get
---

[](#get)

Fetch a value from the cache.

```
get(string $key, mixed $default = null): mixed
```

`$key` - The unique key of this item in the cache;
`$default` - Default value to return if the key does not exist.

Return the value of the item from the cache, or `$default` in case of cache miss.

*Example:*

```
$strategy->get('foo');
```

getMultiple
-----------

[](#getmultiple)

Obtains multiple cache items by their unique keys.

```
getMultiple(array $keys, $default = null): mixed
```

`$keys` - A list of keys that can obtained in a single operation;
`$default` - Default value to return for keys that do not exist.

Return a list of `key => value` pairs. Cache keys that do not exist or are stale will have `$default` as value.

*Example:*

```
$strategy->get('foo');
```

set
---

[](#set)

Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.

```
set(string $key, $value, ?int $ttl = null): $this
```

`$key` - The key of the item to store;
`$value` - The value of the item to store, must be serializable;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->set('foo', 'FOO');
```

setMultiple
-----------

[](#setmultiple)

Persists a set of `key => value` pairs in the cache, with an optional TTL.

```
setMultiple(array $values, ?int $ttl = null): $this
```

`$values` - A list of `key => value` pairs for a multiple-set operation;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR']);
```

forever
-------

[](#forever)

Persists forever data in the cache, uniquely referenced by a key.

```
forever(string $key, $value): $this
```

`$key` - The key of the item to store;
`$value` - The value of the item to store, must be serializable.

*Example:*

```
$strategy->forever('foo', 'FOO');

// or

$strategy->set('foo', 'FOO', 0);
```

foreverMultiple
---------------

[](#forevermultiple)

Persists forever a set of `key => value` pairs in the cache.

```
foreverMultiple(array $values): $this
```

`$values` - A list of `key => value` pairs for a multiple-set operation.

*Example:*

```
$strategy->foreverMultiple(['foo' => 'FOO', 'bar' => 'BAR']);

// or

$strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR'], 0);
```

delete
------

[](#delete)

Delete an item from the cache by its unique key.

```
delete(string $key): $this
```

`$key` - The unique cache key of the item to delete.

*Example:*

```
$strategy->delete('foo');
```

deleteMultiple
--------------

[](#deletemultiple)

Delete multiple items from the cache by their unique keys.

```
deleteMultiple(array $keys): $this
```

`$keys` - The unique cache keys of the items to delete.

*Example:*

```
$strategy->deleteMultiple(['foo', 'bar']);
```

clear
-----

[](#clear)

Wipes clean the entire cache's keys.

```
clear(): $this
```

*Example:*

```
$strategy->clear();
```

remember
--------

[](#remember)

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist.

```
remember(string $key, callable($this): mixed $callable, ?int $ttl = null): mixed
```

`$key` - The unique key of this item in the cache;
`$callable` - The value callable of the item to store when the key is not present in the cache. The value must be serializable;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->remember('foo', function() {
    return 'FOO';
});
```

increment
---------

[](#increment)

Increment a value.

```
increment(string $key, int $amount = 1, ?int $ttl = null): $this
```

`$key` - The unique key of this item in the cache;
`$abount` - The amount to increment;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->increment('foo');

$strategy->increment('foo', 10);
```

decrement
---------

[](#decrement)

Decrement a value.

```
decrement(string $key, int $amount = 1, ?int $ttl = null): $this
```

`$key` - The unique key of this item in the cache;
`$abount` - The amount to decrement;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->decrement('foo');

$strategy->decrement('foo', 10);
```

incrementFloat
--------------

[](#incrementfloat)

Increment a float value.

```
incrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this
```

`$key` - The unique key of this item in the cache;
`$abount` - The amount to increment;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->incrementFloat('foo');

$strategy->incrementFloat('foo', 1.5);
```

decrementFloat
--------------

[](#decrementfloat)

Decrement a float value.

```
decrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this
```

`$key` - The unique key of this item in the cache;
`$abount` - The amount to decrement;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->decrementFloat('foo');

$strategy->decrementFloat('foo', 1.5);
```

touch
-----

[](#touch)

Set a new expiration on an item.

```
touch(string $key, ?int $ttl = null): $this
```

`$key` - The unique key of this item in the cache;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

*Example:*

```
$strategy->touch('foo', 100);
```

pull
----

[](#pull)

Retrieve and delete an item from the cache.

```
pull(string $key, $default = null): mixed
```

`$key` - The unique key of this item in the cache;
`$default` - Default value to return for keys that do not exist.

Return the value of the item from the cache, or `$default` in case of cache miss.

*Example:*

```
$strategy->pull('foo'); // return foo value

$strategy->pull('foo'); // return null
```

add
---

[](#add)

Persists data in the cache if it's not present.

```
add(string $key, $value, ?int $ttl = null): $this
```

`$key` - The key of the item to store;
`$value` - The value of the item to store, must be serializable;
`$ttl` - Optional. The TTL value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

Return `true` if the item is actually added to the cache. Otherwise, return `false`.

*Example:*

```
$strategy->add('foo', 'FOO'); // return true

$strategy->add('foo', 'FOO2'); // return false
```

License
=======

[](#license)

MIT © [Grigorii Duca](http://greg.md)

Huuuge Quote
============

[](#huuuge-quote)

[![I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. © #horrorsquad](https://camo.githubusercontent.com/73fc580403a8367b075fff0a0d7439dc0dc095dd3124b22757522675c1308df8/687474703a2f2f677265672e6d642f6875757567652d71756f74652d66622e6a7067)](https://camo.githubusercontent.com/73fc580403a8367b075fff0a0d7439dc0dc095dd3124b22757522675c1308df8/687474703a2f2f677265672e6d642f6875757567652d71756f74652d66622e6a7067)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance50

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/85a6700372326d0546fcff3eb2c3f27daa711273bdd5d6fdda875a7d2f02af85?d=identicon)[greg-md](/maintainers/greg-md)

---

Top Contributors

[![greg-md](https://avatars.githubusercontent.com/u/10551984?v=4)](https://github.com/greg-md "greg-md (5 commits)")

---

Tags

cachegreg-mdgreg-phpphpphp-cacheweb-artisans

### Embed Badge

![Health badge](/badges/greg-md-php-cache/health.svg)

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

PHPackages © 2026

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