PHPackages                             ascetik/cacheable - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ascetik/cacheable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ascetik/cacheable
=================

Convert Closures and objects into strings

v1.0.0(2y ago)05MITPHP

Since Nov 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ascetik/cacheable)[ Packagist](https://packagist.org/packages/ascetik/cacheable)[ RSS](/packages/ascetik-cacheable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

Cacheable
=========

[](#cacheable)

Convert Closures and objects to strings in order to provide callable caching

Release notes
-------------

[](#release-notes)

> v1.0.0 :

- Breaking change : **CacheableCall** now extends **CallableType** from *ascetik/callapsule* package. Methods have been updated to match with required behavior.
- **CacheableCall** decorates a **CallableType**.

Purpose
-------

[](#purpose)

Each time a client switches the page or submits some data, the server has to build whole system, with services, routes, middlewares... I wondered if it was possible to store some instances or functions in cache for faster system build. The main problem was the serialization of a **Closure**, which may be used by a router or a service container.

A short time was necessary to find the **opis/closure** package. This was the base to find out how to resolve my problem. This package may be still incomplete. I have to test it in a production context to get some feedback with some logs. Self-made logger package is still not done.

Vocabulary
----------

[](#vocabulary)

**Cacheable** is the main interface. It just extends **Serializable** interface and nothing else for now. **CacheableCall** abstracts the idea of a wrapper able to serialize any callable. **CacheableInstance** extends an instance to be fully serializable. **CacheableProperty** holds the name and the value of each **CacheableInstance** wrapped instance.

Descriptions &amp; Exposed Methods
----------------------------------

[](#descriptions--exposed-methods)

> Notice : The use of serialize and unserialize methods is not recommended and will certainly lead to serialization inconsistencies. Use native serialize/unserialize functions instead.

### CacheableFactory (not Cacheable itself)

[](#cacheablefactory-not-cacheable-itself)

- wrapCall(**callable**): **CacheableCall** : Returns a **CacheableCall** instance
- wrapInstance(**object**): **CacheableInstance** : Returns a **CacheableInstance** instance
- wrapProperty(**string**, **mixed**): **CacheableProperty** : Returns a **CacheableProperty**

### CacheableCall

[](#cacheablecall)

- action(): **callable** : Returns registered callable as is
- apply(**iterable**): **mixed** : Calls registered callable

> **CacheableCall** is invokable to keep it simple to use on runtime

### CacheableInstance

[](#cacheableinstance)

- getClass(): **string** : Returns registered class name
- getProperties(): **Ds\\Set** : Returns a container of **CacheableProperties** built from registered instance properties
- getInstance(): **object** : Returns registered instance

### CacheableProperty

[](#cacheableproperty)

- getName(): **string** : Returns property name
- getValue(): **mixed** : Returns property value

Usage
-----

[](#usage)

### Callable Wrap

[](#callable-wrap)

The **CacheableFactory** provides methods to build **Cacheable** instances for callable types. There are three sorts of wrapper to serialize a callable :

```
// Closure use case
$func = function(string $name): string {
    return 'Closure : hello '.$name;
}

// class method use case
class Greeter
{
    public function greet($name): string
    {
        return 'Method : hello ' . $name;
    }
}

// invokable class use case
class InvokableGreeter
{
    public function __invoke(string $name): string
    {
        return 'Invkable : hello ' . $name;
    }
}

$closureWrapper = CacheableFactory::wrapCall($func); // returns a CacheableClosure instance
$methodWrapper = CacheableFactory::wrapCall(new Greeter()); // returns a CacheableMethod instance
$invokableWrapper = CacheableFactory::wrapCall(new InvokableGreeter()); // returns a CacheableInvokable instance
```

As they all are **Cacheable**, so **Serializable**, serialization is easy :

```
$serial = serialize($closureWrapper);
```

And unserialization is easy too :

```
$serial = goAndFindInCache('something-satisfying-my-needs'); // file, external service, SQL, noSQL, hyper-speed keyboard typing world champion...
$wrapper = unserialize($serial);
echo $wrapper->apply(['John']); // prints 'Closure : hello John'
// A CacheableCall is invokable
echo $wrapper(['John']); // same result
```

### Instances Wrap

[](#instances-wrap)

```
$instance = new MyInstance();
$wrapper = CacheableFactory::wrapInstance($instance);
$serial = serialize($wrapper);

// get wrapper back
$extractedWrapper = unserialize($serial);
$extractedInstance = $extractedWrapper->getInstance();
$extractedInstance->doWhatIsExpected();
// Or use the extracted wrapper directly
$extractedWrapper->doWhatIsExpected();
$property = $extractedWrapper->someProperty();
```

When using wrapper magic methods, Exceptions are thrown if :

- called method does not exist
- property does not exist
- property is public

Finalities
----------

[](#finalities)

This package provides serialization and unserialization. The usage of the output is up to you.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

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

Total

3

Last Release

919d ago

Major Versions

v0.1.1 → v1.0.02023-11-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a4d8042606038f6fe934a289278b31406405b77801d3e77142b29341a2614d6?d=identicon)[ascetik](/maintainers/ascetik)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ascetik-cacheable/health.svg)

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

###  Alternatives

[malios/php-to-ascii-table

A PHP library to generate plain text tables.

30118.8k1](/packages/malios-php-to-ascii-table)[samsara/fermat

A library providing math and statistics operations for numbers of arbitrary size.

653.1k3](/packages/samsara-fermat)[heptacom/heptaconnect-portal-base

HEPTAconnect base dataset that every other portal is based on

1024.9k13](/packages/heptacom-heptaconnect-portal-base)[codespede/simple-multi-threader

122.6k](/packages/codespede-simple-multi-threader)

PHPackages © 2026

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