PHPackages                             eftec/cacheone - 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. eftec/cacheone

ActiveLibrary[Caching](/categories/caching)

eftec/cacheone
==============

A Cache library with minimum dependency

2.20(1y ago)103.5k34MITPHPPHP &gt;=7.4CI failing

Since Jun 10Pushed 1y ago2 watchersCompare

[ Source](https://github.com/EFTEC/CacheOne)[ Packagist](https://packagist.org/packages/eftec/cacheone)[ Docs](https://github.com/EFTEC/CacheOne)[ RSS](/packages/eftec-cacheone/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (4)Versions (34)Used By (4)

CacheOne
========

[](#cacheone)

CacheOne is a cache class of service for php. It supports Redis, Memcache, PDO and/or APCU.

Unlikely other cache libraries, this library is based in group (optional). So it's suitable to invalidate a single key or an entire group of elements.

[![Packagist](https://camo.githubusercontent.com/096462bf272c085d1e6f49aee5eb0fedd2a65c3192f6fa31b5f3f5fe916bc8ec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65667465632f43616368654f6e652e737667)](https://packagist.org/packages/eftec/CacheOne)[![Total Downloads](https://camo.githubusercontent.com/ea48fa032060238d07dc4745927330edf4c844fdaffffecdba9b322d71f9e984/68747470733a2f2f706f7365722e707567782e6f72672f65667465632f43616368654f6e652f646f776e6c6f616473)](https://packagist.org/packages/eftec/CacheOne)![Maintenance](https://camo.githubusercontent.com/0c8f829897840ac35cb3daf181a719612c0f64c0ed5fca3c7b90ed7591169162/68747470733a2f2f696d672e736869656c64732e696f2f6d61696e74656e616e63652f7965732f323032352e737667)![composer](https://camo.githubusercontent.com/08623182d7b037246f11c0ad4aec3fe405dcf9d668058398497abd1a9a770e9d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6d706f7365722d253345312e362d626c75652e737667)![php](https://camo.githubusercontent.com/59558613d05bebac3748d4f75f0c94435dec5fb11d059b448c2d172e25d82120/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e342d677265656e2e737667)![php](https://camo.githubusercontent.com/c48ca3b6589987e6b0e68313352f78dfb49dce207864f5a5e9911e263992496d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e332d677265656e2e737667)![CocoaPods](https://camo.githubusercontent.com/347353606ed8f26b45bcf9da083db0063fa1dadd1baef36a5f3bf9ce1d127548/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63732d37302532352d79656c6c6f772e737667)

Example
=======

[](#example)

```
use eftec\CacheOne;
include "vendor/autoload.php"; // composer's autoload
$cache=new CacheOne("redis","127.0.0.1","",6379);

$cacheValue=$cache->get('','countries'); // read the cache (if any) otherwise false
if($cacheValue===false) {
    echo "generating a new list of countries..";
    $countries=['USA','Canada','Japan','Chile'];
    $cache->set('','countries',$countries,500); // store into the cache for 500 seconds.
} else {
    echo "read from cache";
    $countries=$cacheValue;
}
var_dump($countries);
```

Table of Contents
=================

[](#table-of-contents)

- [CacheOne](#cacheone)
- [Example](#example)
- [Table of Contents](#table-of-contents)
- [Definitions](#definitions)
    - [Creating a new instance of CacheOne](#creating-a-new-instance-of-cacheone)
    - [Storing a value](#storing-a-value)
    - [Getting a value](#getting-a-value)
    - [setDefaultTTL](#setdefaultttl)
    - [Pushing and Popping values form an array](#pushing-and-popping-values-form-an-array)
        - [push](#push)
        - [unshift](#unshift)
        - [pop](#pop)
        - [shift](#shift)
    - [invalidate a key](#invalidate-a-key)
    - [invalidate a group](#invalidate-a-group)
    - [invalidate all](#invalidate-all)
    - [setSerializer($serializer)](#setserializerserializer)
    - [getSerializer();](#getserializer)
    - [Select a database (Redis/PdoOne)](#select-a-database-redispdoone)
- [CLI](#cli)
    - [Example REDIS](#example-redis)
- [Version](#version)
- [License](#license)

Definitions
===========

[](#definitions)

Creating a new instance of CacheOne
-----------------------------------

[](#creating-a-new-instance-of-cacheone)

Creates a new connection using Redis (Redis is an on memory cache library)

```
use eftec\CacheOne;
include "../vendor/autoload.php";
$cache=new CacheOne("redis","127.0.0.1","",6379);
```

Creates a new connection using apcu (APCU is an extension for PHP to cache content)

```
use eftec\CacheOne;
include "../vendor/autoload.php";
$cache=new CacheOne("apcu");
```

Creates a new connection using **PdoOne** (PdoOne is a library to connect to the database using PDO)

```
use eftec\PdoOne;
use eftec\CacheOne;
include "../vendor/autoload.php";

$pdo=new PdoOne('mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLE');
$pdo->logLevel=3; // optional, if you want to debug the errors.
$pdo->open();
// $pdo->createTableKV();  // you should create the key-value table if it doesn't exist.
$cache=new CacheOne("pdoone"); // the instance $pdo is injected automatically into CacheOne.
```

or you could use to create a PdoOne instance:

```
$cache=new CacheOne(
  "pdoone",
  ['mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLA']
  );
```

Creates a new connection using memcache (Memcache is an old, but it is still a functional memory cache server)

```
use eftec\CacheOne;
include "../vendor/autoload.php";
$cache=new CacheOne("memcache","127.0.0.1"); // minimum configuration
$cache=new CacheOne("memcache","127.0.0.1",11211,'schema'); // complete configuration
```

Creates a new connection using the class **DocumentOne** (file system)

This example requires the library **eftec/documentstoreone**

```
use eftec\CacheOne;
include "../vendor/autoload.php";
$cache=new CacheOne("documentone",__DIR__."/base","schema"); // folder /base/schema must exists
```

The library **DocumentStoreOne** works with concurrency.

or creating a new connection, using redis, memcache, apcu or documentone (it takes the first available)

```
use eftec\CacheOne;
include "../vendor/autoload.php";
$cache=new CacheOne("auto");
```

Storing a value
---------------

[](#storing-a-value)

> function set($group, $key, $value, $duration = 1440): bool

It stores a value inside a group and a key. It returns false if the operation failed.

> Note: The duration is ignored by "documentone"

```
$cache->set("group","key1","hello world",500);
$cache->set("group","key2","hola mundo",500);
```

Group is optional, and it could be used if we need to invalidate (delete) an entire group.

Getting a value
---------------

[](#getting-a-value)

> function get($group, $key, $defaultValue = false)

It gets a value stored in a group (optional) and key. If the value is not found then it returns false. Note: a false value could be a valid value.

```
$result=$cache->get("group","key1");
$result=$cache->get("","key2");
$result=$cache->get("","key2","not found"); // if not key2 (groupless) then it returns not found
```

setDefaultTTL
-------------

[](#setdefaultttl)

```
$result=$cache->setDefaultTTL(50); // it sets the default time to live. "documentone" one uses it.
$result=$cache->getDefaultTTL();   // it gets the time to live
```

Pushing and Popping values form an array
----------------------------------------

[](#pushing-and-popping-values-form-an-array)

### push

[](#push)

It pushes (adds) a new value at the **end of the array**. If the array does not exist, then it is created a new array. This command allows to limit the numbers of elements of the array.

Syntax:

> push($groups, $key, $value, $duration = null, $limit = 0, $limitStrategy = 'shiftold') : bool

- **$Limit** is used to limit the number maximum of elements of the array, if zero, then it does not limit the elements.
- **$LimitStrategy** is used to determine what to do when we are adding a new element and the limit has been reached, **shiftold** removes the first element of the array, **nonew** does not allow to add a new element, **popold** removes the latest element of the array (if the limit has been reached).

```
// cart could be [1,2,3]
$cache->push('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [1,2,3,4]
$cache->push('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5]
$cache->push('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]
```

### unshift

[](#unshift)

It unshift (add) a new value at the **beginner of the array**. If the array does not exist, then it is created a new array. This command allows to limit the numbers of elements of the array.

Syntax:

> unshift($groups, $key, $value, $duration = null, $limit = 0, $limitStrategy = 'popold') : bool

- **$Limit** is used to limit the number maximum of elements of the array, if zero, then it does not limit the elements.
- **$LimitStrategy** is used to determine what to do when we are adding a new element and the limit has been reached, **shiftold** removes the first element of the array, **nonew** does not allow to add a new element, **popold** removes the latest element of the array (if the limit has been reached).

```
// cart could be [1,2,3]
$cache->unshift('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [4,1,2,3]
$cache->unshift('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5]
$cache->unshift('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]
```

### pop

[](#pop)

It pops (extract) a value at the **end of the array**. If the value does not exist then it returns **$defaultValue** The original array is modified removing the last element of the array.

Syntax:

> pop($group, $key, $defaultValue = false, $duration = null) : mixed

```
// cart could be [1,2,3,4];
$element=$this->pop('','cart'); // now cart is [1,2,3] and $element is 4
```

### shift

[](#shift)

It shifts (extract) a value at the **beginner of the array**. If the value does not exist then it returns **$defaultValue** The original array is modified removing the last element of the array.

Syntax:

> pop($group, $key, $defaultValue = false, $duration = null) : mixed

```
// cart could be [1,2,3,4];
$element=$this->shift('','cart'); // now cart is [2,3,4] and $element is 1
```

invalidate a key
----------------

[](#invalidate-a-key)

> function invalidate($group = '', $key = ''): bool

It invalidates a specific key. If the operation fails, then it returns false

```
$cache->invalidate("group","key1"); // invalidate a key inside a group
$cache->invalidate("","key1"); // invalidate a key without a group.
```

invalidate a group
------------------

[](#invalidate-a-group)

> invalidateGroup($group): bool

It invalidates every key(s) inside a group of groups. It also cleans the catalog of the group and sets it to an empty array.

```
$cache->invalidateGroup("group"); // invalidate all keys inside group
$cache->invalidateGroup(["group1","group2"]); // invalidate all key inside group1 and group2
```

invalidate all
--------------

[](#invalidate-all)

> invalidateAll()

It invalidates all cache.

In **redis**, it deletes the current schema. If not schema, then it deletes all Redis

In **memcached** and **apcu**, it deletes all cache

In **documentone** it deletes the content of the database-folder

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

setSerializer($serializer)
--------------------------

[](#setserializerserializer)

It sets how the values are serialized. By default, it's PHP.

```
$cache->setSerializer('php'); // set the serializer to php (default value). It is fastest but it uses more space.
$cache->setSerializer('json-array'); // set the serializer to json-array. It uses fewer space than PHP however it is a bit slower.
$cache->setSerializer('json-object'); // set the serializer to json-object.
$cache->setSerializer('none'); // set the serializer to none (the value must be serialized)

```

getSerializer();
----------------

[](#getserializer)

Get then how the values are serialized.

```
$type=$cache->getSerializer(); // get php,json-array,json-object or none
```

Select a database (Redis/PdoOne)
--------------------------------

[](#select-a-database-redispdoone)

> select($dbindex)

It selects a different database. By default, the database is 0.

```
$cache->select(1);
$cache->select('table'); // PdoOne
```

CLI
===

[](#cli)

This library also has an interactive CLI.In this CLI, you can create the configuration, test it, load and save. [![example/cli1.jpg](example/cli1.jpg)](example/cli1.jpg)

To open it, you must run the next script:

```
php vendor/bin/cacheonecli
# or (windows)
vendor/bin/cacheonecli.bat
```

### Example REDIS

[](#example-redis)

- Open the CLI
- go to menu cache
- select redis
- select yes
- and select the default information
- Once it is done, select test to test the connection

[![example/cliredis.jpg](example/cliredis.jpg)](example/cliredis.jpg)

- and finally, save the configuration (select save)
- select yes
- select the filename (c1 in this example)

[![example/cliredis2.jpg](example/cliredis2.jpg)](example/cliredis2.jpg)

The configuration will be saved as: **c1.config.php**.
It is a configuration file that you can include or copy and paste in your code

```
