PHPackages                             sanovskiy/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. [Database &amp; ORM](/categories/database)
4. /
5. sanovskiy/simple-cache

ActiveLibrary[Database &amp; ORM](/categories/database)

sanovskiy/simple-cache
======================

A simple and transparent caching library for PHP with multiple driver support

1.0.0(9mo ago)00MITPHPPHP ^8.0

Since Aug 21Pushed 9mo agoCompare

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

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Simple Cache
============

[](#simple-cache)

A simple and transparent caching library for PHP with multiple driver support.

Features
--------

[](#features)

- Lightweight and easy-to-use API
- Multiple drivers: File, Memcached, Redis, Database, Mock
- Built-in support for the "remember" pattern
- Mock driver for testing

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

[](#installation)

```
composer require sanovskiy/simple-cache
```

Requirements
------------

[](#requirements)

- PHP 8.0 or higher
- For File driver: No additional requirements
- For Memcached driver: `ext-memcached` (version &gt;= 3.0 recommended)
- For Redis driver: `ext-redis` (version &gt;= 5.0 recommended)
- For Database driver: `ext-pdo` with appropriate database driver (e.g., `ext-pdo-mysql`)
- For Mock driver: No additional requirements

Configuration
-------------

[](#configuration)

### Files

[](#files)

```
use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\FileCacheDriver;

$cache = new Cache([
    'driver' => FileCacheDriver::class,
    'config' => [
        'directory' => '/var/cache/',
        'file_extension' => '.cache'
    ]
]);
```

### Memcached

[](#memcached)

```
use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\MemcachedCacheDriver;
$cache = new Cache([
    'driver' => MemcachedCacheDriver::class,
    'config' => [
        'servers' => [['127.0.0.1', 11211]],
        'prefix' => 'app_'
    ]
]);
```

### Redis

[](#redis)

```
use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\RedisCacheDriver;
$cache = new Cache([
    'driver' => RedisCacheDriver::class,
    'config' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'prefix' => 'app_',
        'database' => 0
    ]
]);
```

### SQL database

[](#sql-database)

```
use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\DatabaseCacheDriver;
$cache = new Cache([
    'driver' => DatabaseCacheDriver::class,
    'config' => [
        'dsn' => 'mysql:host=localhost;dbname=cache_db',
        'username' => 'user',
        'password' => 'pass',
        'table' => 'cache'
    ]
]);
```

> Note: The DatabaseCacheDriver automatically creates cache table with columns key, value, expires\_at, created\_at, and updated\_at (with an index on expires\_at) if it doesn't exist. Ensure the database user has CREATE TABLE and INDEX permissions.

### Mock cache for testing

[](#mock-cache-for-testing)

```
$cache = new Cache([
    'driver' => \Sanovskiy\SimpleCache\Drivers\MockCacheDriver::class,
    'config' => [
        'emulate_failures' => false, // If true will emulate failure at every operation
        'log_operations' => true
    ]
]);
```

### Usage

[](#usage)

#### Basic usage

[](#basic-usage)

```
$cache->set('key', 'value', 3600);
$value = $cache->get('key', 'default');
$has = $cache->has('key');
$cache->delete('key');
$cache->clear();
```

#### Using the "remember" pattern:

[](#using-the-remember-pattern)

```
$data = $cache->remember('expensive_data', function() {
    return expensiveOperation();
}, 1800);
```

Drivers
-------

[](#drivers)

Simple Cache supports the following drivers:

- **File**: Stores cache in files on the filesystem.
    - Config: `directory` (path to cache directory), `file_extension` (e.g., `.cache`)
- **Memcached**: Uses a Memcached server for distributed caching.
    - Config: `servers` (array of \[host, port\]), `prefix` (key prefix)
- **Redis**: Uses a Redis server for high-performance caching.
    - Config: `host`, `port`, `prefix`, `database`, `password`, `timeout`
- **Database**: Stores cache in an SQL database (e.g., MySQL, PostgreSQL).
    - Config: `dsn`, `username`, `password`, `table`
- **Mock**: In-memory driver for testing, does not persist data.
    - Config: `emulate_failures` (bool), `log_operations` (bool)

Testing
-------

[](#testing)

To run tests locally, ensure Memcached, Redis, and MySQL are running and PHP extensions (`ext-memcached`, `ext-redis`, `ext-pdo_mysql`) are installed. Then run:

```
composer install
./vendor/bin/phpunit tests/
```

To run the tests using Docker Compose:

```
docker-compose up --build
```

To generate a code coverage report:

```
docker-compose run php sh -c "composer install && ./vendor/bin/phpunit tests/ --coverage-html coverage"
```

Why Simple Cache?
-----------------

[](#why-simple-cache)

Simple Cache is designed to be a lightweight, transparent, and easy-to-use caching library for PHP. Unlike other caching libraries that come with complex configurations or heavy dependencies, Simple Cache focuses on:

- **Simplicity**: Intuitive API with minimal setup.
- **Flexibility**: Supports multiple drivers (File, Memcached, Redis, Database, Mock) out of the box.
- **Testability**: Includes a Mock driver for easy unit testing.
- **Lightweight**: Minimal dependencies for better performance in small to medium projects.

Roadmap
-------

[](#roadmap)

- Add support for PSR-6 (Caching Interface) in future versions
- Add support for additional drivers (e.g., APCu)

Contributing
------------

[](#contributing)

Found a bug or have an idea? Open an issue or submit a pull request on [GitHub](https://github.com/sanovskiy/simple-cache).

License
-------

[](#license)

This library is licensed under the MIT License

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance58

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity40

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

270d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a5521376dceab8922e3b4333bbc17e749e3d39a5b0d63b1afa2a84f5c07331b?d=identicon)[sanovskiy](/maintainers/sanovskiy)

---

Top Contributors

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

---

Tags

databaserediscachecachingmemcachedfile cachemock-cache

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  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)[matthiasmullie/scrapbook

Scrapbook is a PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APCu, SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

3212.5M32](/packages/matthiasmullie-scrapbook)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[apix/cache

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing to Redis, Memcached, PDO/SQL, APC and other adapters.

114542.8k6](/packages/apix-cache)[aplus/session

Aplus Framework Session Library

2141.6M3](/packages/aplus-session)[ymigval/laravel-model-cache

Laravel package for caching Eloquent model queries

7642.2k3](/packages/ymigval-laravel-model-cache)

PHPackages © 2026

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