PHPackages                             ihor/cachalot - 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. ihor/cachalot

ActiveLibrary[Caching](/categories/caching)

ihor/cachalot
=============

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

2.4(8y ago)2528.1k↓33.3%2[1 issues](https://github.com/ihor/Cachalot/issues)MITPHPPHP &gt;=5.3.0CI failing

Since Aug 28Pushed 5y ago3 watchersCompare

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

READMEChangelogDependencies (1)Versions (6)Used By (0)

Cachalot
========

[](#cachalot)

Cachalot (cache a lot) is an easy to use caching library. It supposed to do the only one thing - return cached callback result.

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

[](#installation)

Define the following requirement in your composer.json file:

```
"require": {
    "ihor/cachalot": "2.3"
}

```

Usage
-----

[](#usage)

```
// With Cachalot cache you can easily cache results of different types of functions
$cache = new \Cachalot\ArrayCache();

// built-in functions
$length = $cache->getCached('strlen', ['hello world']);

// user defined functions
$unique = $cache->getCached('uniqueValues', [[1, 2, 3, 1, 2, 3]]);

// static methods
$result = $cache->getCached(['Calculator', 'subtract'], [1, 2]);

// instance methods
$square = $cache->getCached([new Calculator(), 'square'], [5]);

// anonymous functions
$reason = $cache->getCached($getErrorReason, [], \Cachalot\Cache::ONE_DAY, 'error-reason');

// callable objects
$trimed = $cache->getCached(new Trimmer(), [' hello world ']);
```

Reference
---------

[](#reference)

### Cache API

[](#cache-api)

##### getCached($callback, array $args = array(), $expireIn = 0, $suffix = null, $useSuffixAsKey = false)

[](#getcachedcallback-array-args--array-expirein--0-suffix--null-usesuffixaskey--false)

Returns cached $callback result

`$callback` is the function (callable) which results we want to be cached
`$args` are the arguments passed to the `$callback`
`$expireIn` sets cache TTL in seconds
`$suffix` is needed to avoid collisions when callback is an anonymous function `$useSuffixAsKey` when true cache suffix will be used as a cache key

```
$length = $cache->getCached('strlen', ['hello world']);
```

To have possibility to use Cachalot as a regular caching library when needed it contains classic cache methods

##### contains($key)

[](#containskey)

Returns true if cache contains entry with given key

```
if ($cache->contains('lastVisit')) {
    echo 'This is not the first visit';
}
```

##### get($key)

[](#getkey)

Returns cached value by key or false if there is no cache entry for the given key

```
if ($lastVisitDate = $cache->get('lastVisit')) {
    echo sprintf('Last visit was at %s', $lastVisitDate);
}
```

##### set($key, $value, $expireIn = 0)

[](#setkey-value-expirein--0)

Caches value by key. When `$expireIn = 0` the value is cached forever

```
$cache->set('lastVisit', time());
```

##### delete($key)

[](#deletekey)

Deletes cache entry by key

```
$cache->delete('lastVisit');
```

##### clear()

[](#clear)

Deletes all cache entries

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

### Back-ends

[](#back-ends)

##### Cachalot\\ApcCache

[](#cachalotapccache)

Stores data in [APC](http://php.net/manual/en/book.apc.php)

```
$cache = new Cachalot\ApcCache();
```

##### Cachalot\\XcacheCache

[](#cachalotxcachecache)

Stores data in [Xcache](https://xcache.lighttpd.net/)

```
$cache = new Cachalot\XcacheCache();
```

##### Cachalot\\MemcacheCache

[](#cachalotmemcachecache)

Stores data in [Memcached](http://memcached.org) using [Memcache PHP extension](http://php.net/manual/en/book.memcache.php)

```
$memcache = new \Memcache();
$memcache->connect('unix:///usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcacheCache($memcache);
```

##### Cachalot\\MemcachedCache

[](#cachalotmemcachedcache)

Stores data in [Memcached](http://memcached.org) using [Memcached PHP extension](http://php.net/manual/en/book.memcached.php)

```
$memcached = new \Memcached();
$memcached->addServer('/usr/local/var/run/memcached.sock', 0);

$cache = new \Cachalot\MemcachedCache($memcached);
```

##### Cachalot\\RedisCache

[](#cachalotrediscache)

Stores data in [Redis](http://redis.io)

```
$redis = new \Redis();
$redis->connect('127.0.0.1');
$redis->select(1);

$cache = new \Cachalot\RedisCache($redis);
```

##### Cachalot\\CouchbaseCache

[](#cachalotcouchbasecache)

Stores data in [Couchbase](http://www.couchbase.com/) using [Couchbase PHP SDK 1.x](http://docs.couchbase.com/couchbase-sdk-php-1.2/index.html)

```
$couchbase = new \Couchbase('127.0.0.1', '', '', 'default');

$cache = new \Cachalot\CouchbaseCache($couchbase);
```

##### Cachalot\\Couchbase2Cache

[](#cachalotcouchbase2cache)

Stores data in [Couchbase](http://www.couchbase.com/) using [Couchbase PHP SDK 2.x](http://developer.couchbase.com/documentation/server/4.0/sdks/php-2.0/php-intro.html)

```
$cluster = new \CouchbaseCluster('couchbase://localhost');
$bucket = $cluster->openBucket('default');

$cache = new \Cachalot\Couchbase2Cache($bucket);
```

##### Cachalot\\ArrayCache

[](#cachalotarraycache)

Stores data in PHP array

```
$cache = new \Cachalot\ArrayCache();
```

##### Cachalot\\BlackholeCache

[](#cachalotblackholecache)

Never stores any data

```
$cache = new \Cachalot\BlackholeCache();
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 98.7% 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 ~305 days

Total

5

Last Release

3058d ago

Major Versions

1.2 → 2.12014-09-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/73abba5ca14b1bc26d004398d23618fd1c60431eb4ced639b1bc56cfb9db6391?d=identicon)[ihor](/maintainers/ihor)

---

Top Contributors

[![ihor](https://avatars.githubusercontent.com/u/490943?v=4)](https://github.com/ihor "ihor (75 commits)")[![RomanovSci](https://avatars.githubusercontent.com/u/4564289?v=4)](https://github.com/RomanovSci "RomanovSci (1 commits)")

---

Tags

apcucachecouchbasememcachephpredisxcacherediscachexcachememcachedapcmemcachecouchbasearray cacheblackhole

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ihor-cachalot/health.svg)

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

###  Alternatives

[phpfastcache/phpfastcache

PHP Abstract Cache Class - Reduce your database call using cache system. Phpfastcache handles a lot of drivers such as Apc(u), Cassandra, CouchBase, Couchdb, Dynamodb, Firestore, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ravendb, Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.

2.4k5.0M130](/packages/phpfastcache-phpfastcache)[tedivm/stash

The place to keep your cache.

9824.8M124](/packages/tedivm-stash)[tedivm/stash-bundle

Incorporates the Stash caching library into Symfony.

841.4M16](/packages/tedivm-stash-bundle)[sabre/cache

Simple cache abstraction layer implementing PSR-16

541.2M3](/packages/sabre-cache)[robinn/phpcacheadmin

A web dashboard for your favorite caching system.

4441.1k1](/packages/robinn-phpcacheadmin)

PHPackages © 2026

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