PHPackages                             cyber-duck/cacheing-memoizer - 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. cyber-duck/cacheing-memoizer

Abandoned → [cyber-duck/cacheing-memoizer](/?search=cyber-duck%2Fcacheing-memoizer)Library[Caching](/categories/caching)

cyber-duck/cacheing-memoizer
============================

Easy to use, flexible memoizer with optional automated cacheing

1.0.0(9y ago)2461MITPHP

Since Jan 23Pushed 2y ago4 watchersCompare

[ Source](https://github.com/Cyber-Duck/cacheing-memoizer)[ Packagist](https://packagist.org/packages/cyber-duck/cacheing-memoizer)[ RSS](/packages/cyber-duck-cacheing-memoizer/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (4)Used By (1)

🚨 Discontinued 🚨
================

[](#-discontinued-)

Please consider using the [illuminate cache package](https://packagist.org/packages/illuminate/cache).

Simple Cacheing Memoizer
========================

[](#simple-cacheing-memoizer)

Our Simple Cacheing Memoizer is a flexible memoizer that can also optionally be combined with a cache to automatically memoize results of calling functions.

We've implemented one example of the latter function using cyber-duck/laravel-cacheing-memoizer which adds it to Laravel for you, but it can also be used in other frameworks - it was originally written for a legacy site that is being upgraded from Codeigniter to Laravel gradually, and works fine in both.

Usage
-----

[](#usage)

### Memoization

[](#memoization)

add

`use CyberDuck\CacheingMemoizer\MemoizesResults`

to your class.

This will give your class extra methods, the most important of which is `memoize`.

Memoize takes two or more arguments.

The first are the building blocks of the memoization key. The second is a function that will return the value to be memoized. The third and any subsequent arguments will be passed to the function when it is invoked.

Memoization is by default unique to the object, but it is also possible to use `singletonMemoize`which is exactly the same as `memoize` except it memoizes the value of the result in one sningleton

Important note: Because this is framework agnostic, the memoizer has to manage the singleton itself. So it may not be the 'same' singleton as your framework preservers. If this is important then over-write the `singletonMemoize` method.

There is also a `globallyMemoize` method. This essentially memoizes the result with that key for any object.

### Cacheing

[](#cacheing)

You may also use the memoization features with automated cacheing. This requires two steps:

1. Setup the memoizer to use the cache by:

    1. call `CyberDuck\CacheingMemoizer\CacheingMemoizer::setCacheRetrievalFunction` with a *callable* which should accept one argument - the cache key - and return the result if there is a cache hit, null other wise.
    2. call `CyberDuck\CacheingMemoizer\CacheingMemoizer::addShutdownFunction` at least once with a *callable* which should accept two arguments - key, then value, which will write to the cache
    3. arrange for `CyberDuck\CacheingMemoizer\CacheingMemoizer::shutdown` to be called at the end of the request.

(if you are using Laravel, `cyber-duck/laravel-cacheing-memoizer` will do this for you)

2. Use `globallyMemoizeWithCache` in the same way as `globallyMemoize`

### Other methods

[](#other-methods)

See source code. Documentation coming. `forget` and `getMemoizeKey` are the main ones, their import hopefully obvious.

\###Can I see a couple of examples of this in use please?

Certainly, here's a simple example of using it in one class to avoid the overhead of an expensive call:

```

    public function getPackageAttribute()
    {
        return $this->memoize('package',
            function () {
                return package()->create(['code' => $this->PackageTypeCode]);
            });
    }

```

Here's an example of how quickly we added memoization + cacheing to a function in 5+ year old legacy code, without having to rewrite anything fresh. The original function looked like:

```
    public function find_all($params = array(), $skip = array('page', 'limit', 'sort-by'))
    {
        $this->db->select('waOrg.OrgID, waOrg.OrgName, waOrg.PackageTypeCode, waOrg.HasGallery, waOrg.HasShowReel, waOrg.AllowSearchResultsPageLogo');
        $this->db->join($this->dbn.'[wlEntry]', 'wlEntry.OrgID = waOrg.OrgID');
        $this->db->join($this->dbn_live.'[wpCountry]', 'wpCountry.Country = waOrg.Country', 'left');
        $this->db->group_by('waOrg.OrgID, waOrg.OrgName, waOrg.PackageTypeCode, waOrg.HasGallery, waOrg.HasShowReel, waOrg.AllowSearchResultsPageLogo');
        $this->db->distinct();

        $this->_apply_filters($params, $skip);

        $query = $this->db->get($this->dbn.'[waOrg] waOrg');

        if(count($query->result()) > 0)
        {
            return count($query->result());
        }

        return false;
    }

```

With automated memoization + cacheing it now looks like:

```

    public function find_all($params = array(), $skip = array('page', 'limit', 'sort-by'))
    {
        return $this->globallyMemoizeWithCache(['org.find.all', $params, $skip], function($params, $skip){
            $this->db->select('waOrg.OrgID, waOrg.OrgName, waOrg.PackageTypeCode, waOrg.HasGallery, waOrg.HasShowReel, waOrg.AllowSearchResultsPageLogo');
            $this->db->join($this->dbn.'[wlEntry]', 'wlEntry.OrgID = waOrg.OrgID');
            $this->db->join($this->dbn_live.'[wpCountry]', 'wpCountry.Country = waOrg.Country', 'left');
            $this->db->group_by('waOrg.OrgID, waOrg.OrgName, waOrg.PackageTypeCode, waOrg.HasGallery, waOrg.HasShowReel, waOrg.AllowSearchResultsPageLogo');
            $this->db->distinct();

            $this->_apply_filters($params, $skip);

            $query = $this->db->get($this->dbn.'[waOrg] waOrg');

            if(count($query->result()) > 0)
            {
                return count($query->result());
            }

            return false;
        }, $params, $skip);
    }

```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~0 days

Total

2

Last Release

3444d ago

Major Versions

0.0.1 → 1.0.02017-01-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c45a99726cc30692bc4821cfc198df1f5de85b1a15f9b66a0bf739acbac0309?d=identicon)[cyber-duck](/maintainers/cyber-duck)

---

Top Contributors

[![karlos545](https://avatars.githubusercontent.com/u/9590468?v=4)](https://github.com/karlos545 "karlos545 (2 commits)")[![suspiciousfellow](https://avatars.githubusercontent.com/u/7312946?v=4)](https://github.com/suspiciousfellow "suspiciousfellow (1 commits)")[![worzy](https://avatars.githubusercontent.com/u/1092417?v=4)](https://github.com/worzy "worzy (1 commits)")

### Embed Badge

![Health badge](/badges/cyber-duck-cacheing-memoizer/health.svg)

```
[![Health](https://phpackages.com/badges/cyber-duck-cacheing-memoizer/health.svg)](https://phpackages.com/packages/cyber-duck-cacheing-memoizer)
```

###  Alternatives

[beryllium/cachebundle

Provides an interface to Memcache for Symfony2 applications

32136.0k](/packages/beryllium-cachebundle)

PHPackages © 2026

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