2017 © Pedro Peláez
 

library cache-simple

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

image

koded/cache-simple

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  • Thursday, July 5, 2018
  • by kodeart
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2,673 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 23 Versions
  • 11 % Grown

The README.md

Koded - Simple Caching Library

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Packagist Downloads Minimum PHP Version, (*1)

A PSR-16 caching library for PHP 8 using several caching technologies. It supports JSON caching for Redis., (*2)

Requirements

The library is not tested on any Windows OS and may not work as expected there., (*3)

The recommended installation method is via Composer ```shell script composer require koded/cache-simple, (*4)


### Redis There are two client flavors for Redis by using the - [Redis extension][2] - [Predis library][5] and they are not mutually exclusive. These clients supports JSON serialization for the cache, useful for handling the cached data in other programming languages. Since there is no Redis native support for JSON serialization, it's done in userland and that always introduces some overhead. **Be aware that the native PHP and Igbinary functions are superior.** - the `RedisClient` is preferred if Redis extension is installed - the `PredisClient` can be used otherwise ```php // with Redis extension simple_cache_factory('redis'); // with Predis library simple_cache_factory('predis');

Memcached

Please install the Memcached extension., (*5)

Usage

The factory function always creates a new instance of specific SimpleCacheInterface client implementation., (*6)

/*
 * Creates a simple cache instance
 * with MemcachedClient and default configuration
 */

$cache = simple_cache_factory('memcached');
/*
 * Some configuration directives for the cache client
 * are passed in the second argument as array
 */

$cache = simple_cache_factory('redis', [
    'host'       => '127.0.0.1',
    'serializer' => 'json',
    'prefix'     => 'test:',
    'ttl'        => 3600 // 1 hour global TTL
]);

A bit verbose construction for the same instance is, (*7)

$config = new ConfigFactory(['serializer' => 'json', 'prefix' => 'test:', 'ttl' => 3000]);
$cache = (new ClientFactory($config))->new('redis');

Configuration directives

Current available configuration classes, (*8)

RedisConfiguration

Please refer to Redis extension connect method., (*9)

Parameter Value
host 127.0.0.1
port 6379
timeout 0.0
reserved null
retry 0
// Without defining the parameters the above directives are used as default
$cache = simple_cache_factory('redis');

Serializers

  • php (default)
  • json

The special config directive is binary(string) for setting the internal serializer functions to either PHP native un/serialize(), igbinary_un/serialize() or msgpack_un/pack()., (*10)

$cache = simple_cache_factory('redis', [
    'binary' => \Koded\Stdlib\Serializer::MSGPACK
]);

The binary directive is effective if igbinary and/or msgpack extensions are installed and loaded. Otherwise it defaults to PHP un/serialize() functions., (*11)

You can change the binary flag on already cached data, but you should invalidate the previously cached items, since they are already serialized and stored in the cache., (*12)

JSON serializer options

The default options for json_encode() function are: - JSON_PRESERVE_ZERO_FRACTION - JSON_UNESCAPED_SLASHES - JSON_THROW_ON_ERROR, (*13)

To set the desired options, use the options configuration directive:, (*14)

$cache = simple_cache_factory('redis', [
    'serializer' => 'json',
    'options' => JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT
]);

JSON options are applied with bitmask operators. The above example will - remove JSON_UNESCAPED_SLASHES (because it's already set) - add JSON_FORCE_OBJECT, (*15)

MemcachedConfiguration

Memcached arguments Type Required Description
id string no Memcached persistent_id value
servers array no A list of nested array with [server, port] values
options array no A list of Memcached options
ttl int no Global TTL (in seconds)

The following options are set by default when an instance of MemcachedConfiguration is created, except for OPT_PREFIX_KEY which is there as a reminder that it may be set., (*16)

Memcached option Default value
OPT_DISTRIBUTION DISTRIBUTION_CONSISTENT
OPT_SERVER_FAILURE_LIMIT 2
OPT_REMOVE_FAILED_SERVERS true
OPT_RETRY_TIMEOUT 1
OPT_LIBKETAMA_COMPATIBLE true
OPT_PREFIX_KEY null

Options with NULL value will be removed., (*17)

There are many Memcached options that may suit the specific needs for the caching scenarios and this is something the developer/s needs to figure it out., (*18)

Examples:, (*19)

[
    // Memcached client `persistent_id`
    'id' => 'items',

    // your Memcached servers list
    'servers' => [
        ['127.0.0.1', 11211],
        ['127.0.0.2', 11211],
        ['127.0.0.2', 11212],
    ],

    // Memcached client options
    'options' => [
        \Memcached::OPT_PREFIX_KEY            => 'i:',  // cache item prefix
        \Memcached::OPT_REMOVE_FAILED_SERVERS => false, // changes the default value
        \Memcached::OPT_DISTRIBUTION          => null   // remove this directive with NULL
    ],

    // the global expiration time (for ALL cached items)
    'ttl' => 120,
]

PredisConfiguration

By default the parameters are:, (*20)

Parameter Value
scheme tcp
host 127.0.0.1
port 6379

Examples:, (*21)

$cache = simple_cache_factory('predis');
$cache = simple_cache_factory('predis', [
    'scheme' => 'unix',
    'path' => '/path/to/redis.sock',
    'options' => [
        'prefix' => 'i:',
        'exceptions' => true,
        'parameters' => [
            'password' => getenv('REDIS_PASSWORD'),
            'database' => 1
        ]
    ]
]);

There are many configuration options for this package. Please refer to Predis configuration page., (*22)

Shared Memory (shmop)

Requires a [PHP shmop extension][11]., (*23)

$cache = simple_cache_factory('shmop', [
    'dir' => '/path/to/app/cache', // optional
    'ttl' => null,                 // global TTL
]);

FileConfiguration

Please avoid it on production environments, or use it as a last option., (*24)

If cache directory is not provided in the configuration, the PHP function sys_get_temp_dir() is used to store the cached files in the OS "temporary" directory., (*25)

$cache = simple_cache_factory('file', ['dir' => '/tmp']);

MemoryClient

This client will store the cached items in the memory for the duration of the script's lifetime. It is useful for development, but not for production., (*26)

MemoryClient is also the default client if you do not require a specific client in cache_simple_factory(), (*27)

$cache = simple_cache_factory('memory');
$cache = simple_cache_factory();  // also creates a MemoryClient

Code quality

```shell script vendor/bin/phpunit, (*28)


```shell script vendor/bin/phpbench run --report=default --group=factory vendor/bin/phpbench run --report=default --group=read-write

or ```shell script vendor/bin/phpbench run --report=benchmark --group=read-write, (*29)


### Benchmarks The benchmarks are flaky and dependant on the environment. This table gives a non-accurate insight how client performs at write-read-delete operations, and should have an informative comparison. To find out what may be the fastest choice for your environment, run

vendor/bin/phpbench run --report=default --group=read-write, (*30)

+-----------------+-----------+-----------+-----------+--------+ | subject | best | mean | worst | rstdev | +-----------------+-----------+-----------+-----------+--------+ | bench_predis | 1.354ms | 1.403ms | 1.431ms | ±2.49% | | bench_redis | 581.000μs | 592.667μs | 609.000μs | ±2.01% | | bench_memcached | 581.000μs | 593.333μs | 606.000μs | ±1.72% | | bench_file | 355.000μs | 367.667μs | 385.000μs | ±3.45% | | bench_shmop | 349.000μs | 364.000μs | 374.000μs | ±2.97% | | bench_memory | 77.000μs | 79.667μs | 82.000μs | ±2.58% | +-----------------+-----------+-----------+-----------+--------+ ```, (*31)

License

Software license, (*32)

The code is distributed under the terms of The 3-Clause BSD license., (*33)

The Versions

05/07 2018

dev-master

9999999-dev

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

05/07 2018

1.9.0

1.9.0.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

05/07 2018

dev-memory-client

dev-memory-client

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

27/06 2018

1.8.0

1.8.0.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

16/03 2018

1.7.1

1.7.1.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

16/03 2018

dev-refactor-redis-trait

dev-refactor-redis-trait

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

16/03 2018

1.7.0

1.7.0.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

16/03 2018

dev-remove-key-regex

dev-remove-key-regex

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

28/02 2018

1.6.0

1.6.0.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

28/02 2018

dev-refactor-things

dev-refactor-things

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

22/02 2018

1.5.0

1.5.0.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

22/02 2018

dev-json-utf8-encoding

dev-json-utf8-encoding

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

22/01 2018

1.4.1

1.4.1.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

19/01 2018

dev-fix-timestamps

dev-fix-timestamps

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

18/01 2018

1.4.0

1.4.0.0

A simple caching library for PHP 7 using several caching technologies. Supports JSON data caching for Redis.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache json redis caching memcached php7 psr-16 file-cache

26/12 2017

1.3.0

1.3.0.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

13/11 2017

1.2.0

1.2.0.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

04/05 2017

1.1.4

1.1.4.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

24/04 2017

1.1.3

1.1.3.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

23/04 2017

1.1.2

1.1.2.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

12/04 2017

1.1.1

1.1.1.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

10/04 2017

1.1.0

1.1.0.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache

10/04 2017

1.0.0

1.0.0.0

A simple caching library for PHP 7 using several caching technologies.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

cache redis caching memcached php7 psr-16 file-cache