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

AbandonedArchivedLibrary[Caching](/categories/caching)

berny/cache
===========

Unified set of tools for common caching tasks

1.0(11y ago)223MITPHPPHP &gt;=5.3.0

Since Oct 20Pushed 11y ago1 watchersCompare

[ Source](https://github.com/xphere/Cache)[ Packagist](https://packagist.org/packages/berny/cache)[ Docs](https://github.com/xphere/Cache)[ RSS](/packages/berny-cache/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (2)Used By (0)

Berny\\Cache
============

[](#bernycache)

This library provides a unified set of tools for common caching tasks.

[![SensioLabsInsight](https://camo.githubusercontent.com/8860533bffe0aa31ac251bc51dba14009ef7b8c65685808b78eb8d1474025b2d/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f31313939343465332d383336342d346531332d383666392d6332623065653966356266612f736d616c6c2e706e67)](https://insight.sensiolabs.com/projects/119944e3-8364-4e13-86f9-c2b0ee9f5bfa)

Features
--------

[](#features)

Moves from the classic cache interface to a item interface.

Instalation
-----------

[](#instalation)

### From [composer/packagist](https://getcomposer.org)

[](#from-composerpackagist)

- Add `"berny/cache": "*@dev"` to your `composer.json` file

### From [github](https://github.com)

[](#from-github)

- [Download the code](https://github.com/xphere/Cache) into your project

Storage
-------

[](#storage)

You want to store your cached data somewhere, and this is where `StorageInterface` comes handy.

Its only job is to create Items from keys through the `getItem($key)` method.

Some implementations of this interface are:

- `FilesystemStorage`
- `MemoryStorage`
- `DoctrineCacheAdapter`

Item
----

[](#item)

Items allow management of ONE key inside a given Storage. You can:

- `key()`: Get the item's key
- `get()`: Get its cached value (if not missed the cache)
- `set($value)`: Set the cached value
- `miss()`: Check if it missed the cache
- `remove()`: Clear it

### Example

[](#example)

```
function cacheComplexFunction(ItemInterface $item, Closure $complex)
{
    if ($item->miss()) {
        $value = $complex($item->key());
        $item->set($value);
    }
    return $item->get();
}

$storage = new MemoryStorage();
$result = cacheComplexFunction($storage->getItem('key'), complexCalculation);

```

Cache
-----

[](#cache)

The main brain is `Cache`, a concrete class which receives a `StorageInterface` (and optionally a `StrategyInterface`) to proxy all requests through it. It defines:

- `get($key, $default = null)`: Our plain get by key, returning `$default` on cache misses.
- `getOrSet($key, $value)`: Gets cached values by key, storing and returning `$value` on cache misses.
- `getOrLazySet($key, $callback)`: Same as `getOrSet` but with lazy evaluation of `$callback` on cache misses.
- `set($key, $value)`: No secrets, a vanilla set by key.
- `remove($key)`: Remove items only when not missed the cache.
- `getItem($key)`: Return the `ItemInterface` created by the underlying `StorageInterface`.

### Example

[](#example-1)

```
// Storage is any StorageInterface implementation
$cache = new Cache($storage);

$result = $cache->get('key_1'); // null on cache misses
$result = $cache->get('key_2', 'default'); // 'default' on cache misses
$result = $cache->getOrSet('key_3', 'value'); // 'value' stored on cache misses
$result = $cache->getOrLazySet('key_4', $callback); // $callback called only on cache misses

```

Strategy
--------

[](#strategy)

Implement the `StrategyInterface` to automate generation of concrete keys from any user-domain ones. This allows for automatic cache invalidation.

If injected into `Cache`, keys go first through this object before calling `setItem($key)`.

Implementations:

- `FilesystemStrategy`: Treats keys as path filenames. Returns a key with last modified time to invalidate cache entries on file changes.

### Example

[](#example-2)

```
$path = __DIR__ . '/to-do/';
$strategy = new FilesystemStrategy();
// Storage is any StorageInterface implementation
$cache = new Cache($storage, $strategy);
// Callback only called the first time and every time the $path content changes
$cache->getOrLazySet($path, $callback);

```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

4264d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/608fb6577803c78b31cf7ade299388f44ffe2e13f4cc773c331944773cca6b97?d=identicon)[xphere](/maintainers/xphere)

---

Top Contributors

[![xphere](https://avatars.githubusercontent.com/u/170968?v=4)](https://github.com/xphere "xphere (28 commits)")

---

Tags

cache

### Embed Badge

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

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

###  Alternatives

[psr/simple-cache

Common interfaces for simple caching

8.1k757.1M2.6k](/packages/psr-simple-cache)[psr/cache

Common interface for caching libraries

5.2k712.8M1.6k](/packages/psr-cache)[react/cache

Async, Promise-based cache interface for ReactPHP

445124.5M44](/packages/react-cache)[beste/in-memory-cache

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

2514.6M10](/packages/beste-in-memory-cache)[fastd/fastd

A High Performance API Framework By Swoole Extension

42015.4k18](/packages/fastd-fastd)[anahkiasen/flatten

A package for the Illuminate framework that flattens pages to plain HTML

33113.0k](/packages/anahkiasen-flatten)

PHPackages © 2026

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