PHPackages                             mcustiel/php-simple-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. mcustiel/php-simple-cache

ActiveLibrary[Caching](/categories/caching)

mcustiel/php-simple-cache
=========================

A simple library to abstract different cache servers.

v1.5.0(10y ago)84.3k1[1 PRs](https://github.com/mcustiel/php-simple-cache/pulls)1GPL-3.0+PHPPHP &gt;=5.5

Since Nov 14Pushed 2y ago1 watchersCompare

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

READMEChangelog (8)Dependencies (8)Versions (10)Used By (1)

php-simple-cache
================

[](#php-simple-cache)

What is it
==========

[](#what-is-it)

php-simple-cache is a PHP Cache library with the minimum functionalities needed that abstracts different cache mechanisms. It is thought to be performant, flexible and easy to use without the need of a containing framework.

Currently the cache systems abstracted by php-simple-cache are:

- **Serialized files ("file" driver)**: Uses files saved in a directory containing the serialized data.
- **Memcache ("memcache" driver)**: Uses php's memcache extension through \\Memcache class to access a memcached server.
- **Redis ("phpredis" driver)**: Uses php's phpredis extension through \\Redis class
- **APCu ("apcu" driver)**: Uses apcu pecl extension for fast local cache.

This library is thought to be used with or without a dependency injection system, that's why the constructor of each driver allows the injection of it's dependencies. If you don't use a dependency injection system, maybe you just want to use a provided factory class that instantiates each driver by it's name, but you need to call init method to configure the driver after it's instantiated.

Installation
============

[](#installation)

### Download code

[](#download-code)

This library supports PSR-4 so you can just download the code and map your autoloader to use it.

### Composer

[](#composer)

php-simple-config also supports composer, just add the packagist dependency:

```
{
    "require": {
    	// ...
        "mcustiel/php-simple-cache": ">=1.3.1"
    }
}
```

How to use it
=============

[](#how-to-use-it)

### Driver instantiation

[](#driver-instantiation)

There's a variety of ways to instantiate the drivers, as an example **memcache** driver is used:

#### Simple instantiation

[](#simple-instantiation)

```
$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache();
$config = new \stdClass();
$config->host = 'yourmemcached.host.com';
$config->port = 11211;
$config->timeout = 1;
$cacheManager->init($config);
```

Or with your own Memcache object (this technique could also be used with a dependency injection system.

```
$connection = new \Memcache();
$connection->connect();
$cacheManager = new \Mcustiel\SimpleCache\Drivers\memcache\Cache($connection);
// No init() call required here.
```

#### Using provided factory class

[](#using-provided-factory-class)

```
use \Mcustiel\SimpleCache\SimpleCache;

$factory = new SimpleCache();
$cacheManager = $factory->getCacheManager('memcache');
$config = new \stdClass();
$config->host = 'yourmemcached.host.com';
$config->port = 11211;
$config->timeout = 1;
$cacheManager->init($config);
```

### Caching and retrieving data

[](#caching-and-retrieving-data)

Using php-simple-cache for caching data and retrieve it after is really simple, the idea is (as in most cache systems) to check if data is cached, if not generate the data and cache it.

```
use \Mcustiel\SimpleCache\Types\Key;

$key = new Key('cached-data-key');
$data = $cacheManager->get($key);
if ($data === null) {
	$data = someHeavyProcessThatGeneratesDataThatCanBeCached();
	$cacheManager->set($key, $data);
}
```

### Removing cached data

[](#removing-cached-data)

When there is cached data that isn't needed to be cached anymore, just use the delete method:

```
use \Mcustiel\SimpleCache\Types\Key;

$key = new Key('cached-data-key');
$cacheManager->delete($key);
```

### Driver-specific configurations

[](#driver-specific-configurations)

Each driver has a unique configuration specification, the best way to avoid this is to inject the dependencies into each driver but, if you need to use it the init() method, these are the config parameters for each driver:

#### Memcache

[](#memcache)

```
$config = new \stdClass()
# Memcache server address, default is localhost
$config->host = 'yourmemcached.host.com';
# Memcache server port, default is php.ini:memcache.default_port
$config->port = 11211;
# Connection timeout in seconds, default is 1
$config->timeoutInSeconds = 1;
# Whether or not to use a persistent connection, default is false
$config->persistent = false;
```

#### Phpredis

[](#phpredis)

```
$config = new \stdClass()
# Redis server address, default is localhost
$config->host = 'yourredis.host.com';
# Redis server port, default is null
$config->port = 6379;
# Connection timeout in seconds, default is null
$config->timeoutInSeconds = 1;
# Redis server auth password, default is null
$config->password = 'yourRedisPasswd';
# Database number, default is 0
$config->database = 2;
# Persistent connection id. If not specified, then  persistent connection is not used.
$config->persistedId = 'yourPersistentConnectionId';
```

#### File

[](#file)

```
$fileService = new Mcustiel\SimpleCache\Drivers\file\Utils\FileService('/path/to/cache/files')
```

### Considerations

[](#considerations)

Please have in mind that each driver is different in it's implementation. There's no much difference between phpredis and memcache drivers, but file driver does not auto expire cache. For the file case I added support for timeout but that makes it a slow cache, so for the future I have as a TODO, to create a "garbage collector" script that processes expired files and remove the logic to delete expired files in get method. Use file driver just in very rare cases in which you don't have access to memcache or redis or for development environment.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

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

Recently: every ~77 days

Total

8

Last Release

3863d ago

Major Versions

0.5 → 1.02014-12-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9b3ff93206038debfa1f16a11bbfc10fca9b2f4ddfdafa00e27365d290cf0d?d=identicon)[mcustiel](/maintainers/mcustiel)

---

Top Contributors

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

---

Tags

abstractioncacheSimplemanagerdriversminimalist

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mcustiel-php-simple-cache/health.svg)

```
[![Health](https://phpackages.com/badges/mcustiel-php-simple-cache/health.svg)](https://phpackages.com/packages/mcustiel-php-simple-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)[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)

PHPackages © 2026

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