knot-lib/cache

Cache library for kNot Framework.

0.3.0 2021-06-05 20:31 UTC

This package is auto-updated.

Last update: 2023-09-06 01:16:51 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

Cache library for kNot Framework.

Feature

  • PSR16: SimpleCache compliant
  • File cache
  • Array cache

Demo(FileCache)

config

$config = [
    'cache' => [
        'root' => '/path/to/cache',
        'expire' => 5,      // expires 5 seconds after cache item is set
    ]
];

get/set

$cache = new FileCache($config);

$cache->set('my favorite artist', 'stevie wonder');

echo $cache->get('my favorite artist');    // stevie wonder

$cache->set('my favorite artist', 'justin bieber');

echo $cache->get('my favorite artist');    // justin bieber

expire

$cache = new FileCache($config);

$cache->set('my favorite artist', 'stevie wonder');

echo $cache->get('my favorite artist');    // stevie wonder

sleep(6);

echo $cache->get('my favorite artist');    // 

$cache->set('my favorite artist', 'stevie wonder', 10);

sleep(6);

echo $cache->get('my favorite artist');    // stevie wonder

delete

$cache = new FileCache($config);

$cache->set('my favorite artist', 'stevie wonder');

echo $cache->get('my favorite artist');     // stevie wonder

$cache->delete('my favorite artist');

echo $cache->get('my favorite artist');     //

clear

$cache = new FileCache($config);

$cache->set('my favorite artist', 'stevie wonder');
$cache->set('my favorite movie', 'Jurassic Park');

echo $cache->get('my favorite artist');     // stevie wonder
echo $cache->get('my favorite movie');      // Jurassic Park

$cache->clear();

echo $cache->get('my favorite artist');     // 
echo $cache->get('my favorite movie');      // 

get multiple cache

$cache = new FileCache($config);

$cache->set('my favorite artist', 'stevie wonder');
$cache->set('my favorite movie', 'Jurassic Park');

$values = $cache->getMultiple([
    'my favorite artist', 'my favorite movie'
]);

echo print_r($values, true);
// Array
// (
//     [my favorite artist] => stevie wonder
//     [my favorite movie] => Jurassic Park
// )

set multiple cache

$cache = new FileCache($config);

$cache->setMultiple([
    'my favorite artist' => 'stevie wonder',
    'my favorite movie' => 'Jurassic Park',
]);

echo $cache->get('my favorite artist');     // stevie wonder
echo $cache->get('my favorite movie');      // Jurassic Park

array cache

$cache = new ArrayCache($config);

$cache->set('my favorite artist', 'stevie wonder');

echo $cache->get('my favorite artist');     // stevie wonder

sleep(6);

echo $cache->get('my favorite artist');     // 

Usage

1. Make config array

  • cache/root is required for FileCache
  • cahe/expire is optional(unit: seconds)

2. Create cache object

  • pass config array to the constructor

3. Set or get cache

  • use get() method to get cache item
  • use set() method to update cache item

Requirement

PHP 7.2 or later

Installing knot-lib/cache

The recommended way to install knot-lib/cache is through Composer.

composer require knot-lib/cache

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

This library is licensed under the MIT license.

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.