PHPackages                             originphp/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. originphp/cache

ActiveLibrary[Caching](/categories/caching)

originphp/cache
===============

OriginPHP Cache

2.0.0(5y ago)111.2k↓80%5MITPHPPHP &gt;=7.3.0CI failing

Since Oct 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/originphp/cache)[ Packagist](https://packagist.org/packages/originphp/cache)[ Docs](https://www.originphp.com)[ RSS](/packages/originphp-cache/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (14)Used By (5)

Cache
=====

[](#cache)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/originphp/cache/workflows/CI/badge.svg)](https://github.com/originphp/cache/actions)[![coverage](https://camo.githubusercontent.com/55984444c52d86e7b19894771d612fb7895f30541377133ac33a2bf62948fceb/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f726967696e7068702f63616368652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/originphp/cache?branch=master)

The OriginPHP Cache library `Apcu`, `Memcached` and `Redis` out of the box, you can also use file caching for large objects or time consuming generating content.

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

[](#installation)

To install this package

```
$ composer require originphp/cache

```

In your bootstrap file you will the configuration for cache. The caching library can work with multiple configurations and engines at the same time.

Once the configuration is out the way, using the cache is pretty straightforward. Configure a default Cache store

```
Cache::config('default', [
    'engine' => 'File',
    'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
    'prefix' => 'cache_'
    'serialize' => true // set to false if you going to cache strings such as output
    'path' => dirname(__DIR__) . '/cache'
     ]);
```

Caching
-------

[](#caching)

### Write

[](#write)

To add an item to the cache.

```
Use Origin\Cache\Cache;

$success = Cache::write('key',$value);
```

### Read

[](#read)

> From version 3.x when reading from the cache, if there is no hit then it will return `null` instead of `false` as in previous versions.

To read an item from the cache, if it does not find an item it will return `null`

```
Use Origin\Cache\Cache;

$value = Cache::read('key');
```

### Exists

[](#exists)

To check whether a key exists in the cache

```
Use Origin\Cache\Cache;

if(Cache::exists('key')){
    $bool = Cache::read('key');
}
```

### Delete

[](#delete)

Items are automatically deleted based upon the duration setting in the configuration, however if you want to delete an item manually then use the delete method.

```
Use Origin\Cache\Cache;

Cache::delete('key');
```

### Clearing the Cache

[](#clearing-the-cache)

```
Cache::clear();
```

### Enabling and disabling the cache

[](#enabling-and-disabling-the-cache)

Sometimes you will need to disable the cache, when you disable we switch the engine to the `NullEngine` and your program can work as normal.

```
Cache::disable();
Cache::enable();
```

### Working with Multiple Configurations

[](#working-with-multiple-configurations)

Whether you are using multiple caching engines, or you multiple configurations for a single cache engine (e.g. short duration and long duration caches), the Cache utility is flexible.

You can get the configured Cache store:

```
$cache = Cache::store('long-duration');
$value = $cache->read('My.key');
```

Or you can pass an options array telling the Cache object which configuration to use

```
$value = Cache::read('My.key',[
     'config'=>'long-duration'
     ]);
```

Engines
-------

[](#engines)

In all these examples,we are only configuring the default configuration, you can set different configuration names instead of default. When you use the caching functions the default configuration is used by default unless you say otherwise.

### File Engine

[](#file-engine)

```
Cache::config('default', [
    'engine' => 'File',
    'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
    'prefix' => 'cache_'
    'serialize' => true // set to false if you going to cache strings such as output,
    'mode' => 0664
     ]);
```

### Apcu Engine

[](#apcu-engine)

```
Cache::config('default', [
    'engine' => 'Apcu',
    'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
    'prefix' => 'cache_'
     ]);
```

### Memcached Engine

[](#memcached-engine)

This is a simple configuration for using Memcached.

```
Cache::config('default', [
        'engine' => 'Memcached',
        'host' => '127.0.0.1',
        'port' => '11211',
        'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
        'prefix' => 'cache_'
     ]);
```

If your Memcached server is configured with username and password then

```
Cache::config('default', [
        'engine' => 'Memcached',
        'host' => '127.0.0.1',
        'port' => '11211',
        'username' => 'james',
        'password' => 'secret',
        'duration' => '+60 minutes', // 3600,
        'prefix' => 'cache_'
     ]);
```

If you are going to use socket then instead of setting host and port, then set the `path` key with the location of the socket.

```
Cache::config('default', [
     'engine' => 'Memcached',
     'path' => '/var/sockets/memcached'
     ]);
```

You can also make connections persistent by setting the `persistent` key to true, or a string which will be the persistent id.

Memcached supports server pools, if you are going to use them then set an array using the `servers` key instead of host and port. The array should be compatible with [memcached addservers](http://php.net/manual/en/memcached.addservers.php).

### Redis Engine

[](#redis-engine)

This is a simple configuration for using Redis.

```
Cache::config('default', [
        'engine' => 'Redis',
        'host' => '127.0.0.1',
        'port' => 6379,
        'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
        'timeout' => 0,
        'prefix' => 'cache_'
     ]);
```

If your Redis server is configured with a password then

```
Cache::config('default', [
        'engine' => 'Redis',
        'host' => '127.0.0.1',
        'port' =>  6379,
        'password' => 'secret',
        'duration' => 3600, // duration can also be string e.g. +60 minutes
        'prefix' => 'cache_'
     ]);
```

If you are going to use socket then instead of setting host and port, then set the `path` key with the location of the socket.

```
Cache::config('default', [
        'engine' => 'Redis',
        'path' => '/var/sockets/redis',
     ]);
```

You can also make connections persistent by setting the `persistent` key to true, or a string which will be the persistent id.

```
Cache::config('default', [
        'engine' => 'Redis',
        'host' => '127.0.0.1',
        'port' => 6379,
        'persistent' => 'my-app',
        'duration' => 3600, // duration can also be string e.g. +60 minutes
        'timeout' => 0,
        'prefix' => 'cache_'
     ]);
```

### Custom Engine

[](#custom-engine)

If you want to work with a different backend, it easy to create your own. When configuring cache, instead of passing the `engine` key, use `className` and include the full namespace.

```
Cache::config('default', [
    'className' => 'App\Cache\CustomEngine',
    'duration' => 3600,
    'prefix' => 'cache_'
     ]);
```

```
namespace App\Cache;
use Origin\Cache\Engine\BaseEngine;
class CustomEngine extends BaseEngine
{

}
```

Installing Cache Engines
------------------------

[](#installing-cache-engines)

The command line instructions have been tested with Ubuntu 18.x.

### Apcu

[](#apcu)

Apcu is already install in the docker container, however if you need to install this manually.

```
sudo apt-get update
sudo apt-get install php-apcu
sudo echo 'apc.enable_cli=1' >>  /etc/php/7.2/cli/php.ini
```

### Memcached

[](#memcached)

**To install Memcached in the Docker container**

First add the following to the `docker-compose.yml` file, this will load the memcached image.

```
  memcached:
      image: memcached

```

In the `Dockerfile` add `php-memcached` to the lines where it installs the php extensions

```
    php-memcached \

```

Then run the build command in docker-compose.

```
docker-compose build

```

Then set the host to `memcached` in your cache config.

**To install Memcached on a Ubuntu/Debain based server**

```
sudo apt-get update
sudo apt-get install memcached
sudo apt-get install php-memcached
```

### Redis

[](#redis)

**To install Redis in the Docker container**

First add the following to the `docker-compose.yml` file, this will load the Redis image.

```
  redis:
      image: redis

```

In the `Dockerfile` add the following lines to install and enable the Redis PHP extension.

```
RUN pecl install redis
RUN echo 'extension=redis.so' >> /etc/php/7.2/apache2/php.ini
RUN echo 'extension=redis.so' >> /etc/php/7.2/cli/php.ini

```

Then run the build command in docker-compose.

```
docker-compose build

```

Then set the host to `redis` in your cache config.

**To install Redis on a Ubuntu/Debain based server**

```
pecl install redis
sudo echo 'extension=redis.so' >> /etc/php/7.2/apache2/php.ini
sudo echo 'extension=redis.so' >> /etc/php/7.2/cli/php.ini
```

Testing
-------

[](#testing)

To test this first build the docker container

```
docker-compose build

```

To start the container, and access bash

```
$ docker-compose up
$ docker-compose run app bash
$ vendor/bin/phpunit

```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity60

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

Total

13

Last Release

1957d ago

Major Versions

1.2.6 → 2.x-dev2021-01-04

PHP version history (3 changes)1.0.0PHP ^7.2.0

1.2.6PHP &gt;=7.2.0

2.x-devPHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e8a821333d9c7b7bc2ad3d164d142f65cd3912dea78033d31f76b0f19ba8a0c?d=identicon)[originphp](/maintainers/originphp)

---

Top Contributors

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

---

Tags

rediscachefilememcachedapcuoriginPHP

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[desarrolla2/cache

Provides an cache interface for several adapters Apc, Apcu, File, Mongo, Memcache, Memcached, Mysql, Mongo, Redis is supported.

1322.5M47](/packages/desarrolla2-cache)[robinn/phpcacheadmin

A web dashboard for your favorite caching system.

4441.1k1](/packages/robinn-phpcacheadmin)[matomo/cache

PHP caching library based on Doctrine cache

38854.1k4](/packages/matomo-cache)[ihor/cachalot

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

2528.1k](/packages/ihor-cachalot)

PHPackages © 2026

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