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

ActiveLibrary[Caching](/categories/caching)

please/cache
============

2.0.1(2y ago)0241MITPHPPHP ^8.1

Since Nov 30Pushed 2y ago1 watchersCompare

[ Source](https://github.com/chipslays/cache)[ Packagist](https://packagist.org/packages/please/cache)[ RSS](/packages/please-cache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)DependenciesVersions (6)Used By (1)

🗄️ Cache
========

[](#️-cache)

A simple and primitive library for caching values for PHP &gt;8.1.

 [![](/.github/static/hero.png)](/.github/static/hero.png)

Installation
------------

[](#installation)

```
composer require please/cache
```

Examples
--------

[](#examples)

You can find usage examples [here](/examples).

Drivers
-------

[](#drivers)

Available drivers:

- [Filesystem](#filesystem)
    - This driver uses the file system to store the cache.
- [Memory](#memory)
    - This driver uses the memory to store the cache.
- [Session](#session)
    - This driver uses the `$_SESSION` to store the cache.

### Filesystem

[](#filesystem)

This driver uses the file system to store the cache.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Filesystem;

$cache = new Cache(new Filesystem);

$cache->set('foo', 'bar');
$cache->get('foo'); // bar
```

You can provide a specific parameters.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Filesystem;

$driver = new Filesystem(folder: '/path/to/folder', prefix: 'data');

$cache = new Cache($driver);

$cache->set('foo', fn () => ['bar']);
$cache->get('foo'); // ['bar']
```

### Memory

[](#memory)

This driver uses the memory to store the cache.

Warning

After the script completes the memory will be cleared.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Memory;

$cache = new Cache(new Memory);
$anotherCacheInstance = new Cache(new Memory);
```

By default, cache created with Memory driver under hood.

```
use Please\Cache\Cache;

$cache = new Cache;
```

### Session

[](#session)

This driver uses the `$_SESSION` to store the cache.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Session;

$cache = new Cache(new Session);
```

You can pass the key in which the cache will be stored.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Session;

$apiCache = new Cache(new Session('_api'));
$imageCache = new Cache(new Session('_images'));
```

Cache
-----

[](#cache)

You can create as many Cache instances as you need.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Session;
use Please\Cache\Drivers\Filesystem;

$videoCache = new Cache(new Session('your unique key'));
$imageCache = new Cache(new Filesystem('/path/to/images'));
```

By default, for serialization uses native PHP functions `serialize()` and `unserialize()`.

You can create and pass your own serializer if you need to, for example to serialize closures, classes, etc.

```
use Please\Cache\Cache;
use Please\Cache\Drivers\Filesystem;
use Please\Cache\Serializers\Contracts\Serializer;

class JsonSerializer implements Serializer
{
    public function serialize(mixed $value): string
    {
        return json_encode($value);
    }

    public function unserialize(mixed $value): mixed
    {
        return json_decode($value, true);
    }
}

$cache = new Cache(new Filesystem, new JsonSerializer);
```

Methods
-------

[](#methods)

### set()

[](#set)

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

ParameterRequiredDefault`string $key``true``mixed $value``true``int|string $ttl``false``1 year````
$cache->set(key: 'foo', value: 'bar', ttl: 3600);
```

You can pass the TTL value as a string like for the `strtotime() ` function.

```
$cache->set('foo', ['bar', 'baz'], '1 day');

// the example above is equivalent to this code
$ttl = strtotime('1 day') - time();
$cache->set('foo', 'bar', $ttl);
```

### get()

[](#get)

Fetches a value from the cache.

ParameterRequiredDefault`string $key``true``mixed $default ``false``null````
$cache->get(key: 'foo', default: 'baz');
```

Pass a default value as a `Closure`, it will be executed lazily if the key is not found.

```
$cache->get('foo', fn () => 'baz');
```

### has()

[](#has)

Determines whether an item is present in the cache.

ParameterRequiredDefault`string $key``true````
$cache->set('foo', 'bar');

$cache->has('foo'); // true
$cache->has('baz'); // false
```

### clear()

[](#clear)

Wipes all cache.

Note

The `$cacheInstance->clear()` method will only work for the instance in which it was called.

```
$cache->set('foo1', 'bar1')->has('foo1'); // true
$cache->set('foo2', 'bar2')->has('foo2'); // true

$cache->clear();

$cache->has('foo1'); // false
$cache->has('foo2'); // false
```

### forget()

[](#forget)

Delete cache by key.

ParameterRequiredDefault`string $key``true````
$cache->set('foo', 'bar')->has('foo'); // true

$cache->forget('foo');

$cache->has('foo'); // false
```

### pluck()

[](#pluck)

Removes and returns an item from the cache by its key.

ParameterRequiredDefault`string $key``true``mixed $default``false``null````
$cache->set('foo', 'bar')->has('foo'); // true

$cache->pluck('foo'); // bar

$cache->has('foo'); // false
```

### through()

[](#through)

If the closure is not cached, then executes it, otherwise returns the cached result of closure execution.

This method used [`ClosureHash`](/src/Support/ClosureHash.php) under hood.

Note

The closure must return a value suitable for serialization by the [serializer](/src/Serializers/) you choose.

ParameterRequiredDefault`string $callback``true``int|string $ttl``false``1 year````
$closure = function () {
    return mt_rand();
};

$cache->through($closure);
$cache->through($closure); // returns cached result of closure execution
```

License
-------

[](#license)

Open-sourced software licensed under the [MIT license](https://opensource.org/license/mit/).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

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

Total

5

Last Release

762d ago

Major Versions

1.2.0 → 2.0.02024-04-16

### Community

Maintainers

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

---

Top Contributors

[![chipslays](https://avatars.githubusercontent.com/u/19103498?v=4)](https://github.com/chipslays "chipslays (21 commits)")

---

Tags

cachephp-cachecachephp cache

### Embed Badge

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

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

###  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)[nielse63/phpimagecache

Image Cache is a very simple PHP module that accepts an image source and will compress and cache the file, move it to a new directory, and return the cache source for the image.

44713.3k](/packages/nielse63-phpimagecache)[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)[mmamedov/page-cache

PageCache is a lightweight PHP library for full page cache. It uses various strategies to differentiate among separate versions of the same page.

7912.7k1](/packages/mmamedov-page-cache)

PHPackages © 2026

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