knot-lib / cache
Cache library for kNot Framework.
Installs: 1 468
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
- ext-json: *
- knot-lib/config: ~0.1
- psr/simple-cache: ^1.0
- stk2k/file-system: ~0.1
Requires (Dev)
- mikey179/vfsstream: 1.6.8
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^8.5.15
This package is auto-updated.
Last update: 2023-09-06 01:16:51 UTC
README
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
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.