knot-lib/cache

View on GitHub
README.md

Summary

Maintainability
Test Coverage
Cache library for kNot Framework.
=======================

[![Latest Version on Packagist](https://img.shields.io/packagist/v/knot-lib/cache.svg?style=flat-square)](https://packagist.org/packages/knot-lib/cache)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://travis-ci.org/knot-lib/cache.svg?branch=master)](https://travis-ci.org/knot-lib/cache)
[![Coverage Status](https://coveralls.io/repos/github/knot-lib/cache/badge.svg?branch=master)](https://coveralls.io/github/knot-lib/cache?branch=master)
[![Code Climate](https://codeclimate.com/github/knot-lib/cache/badges/gpa.svg)](https://codeclimate.com/github/knot-lib/cache)
[![Total Downloads](https://img.shields.io/packagist/dt/knot-lib/cache.svg?style=flat-square)](https://packagist.org/packages/knot-lib/cache)

## Description

Cache library for kNot Framework.


## Feature

- PSR16: SimpleCache compliant
- File cache
- Array cache

## Demo(FileCache)

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

### get/set
```php
$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
```php
$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
```php
$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
```php
$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
```php
$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
```php
$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
```php
$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](http://getcomposer.org).

```bash
composer require knot-lib/cache
```

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

```php
require 'vendor/autoload.php';
```

## License
This library is licensed under the MIT license.

## Author

[stk2k](https://github.com/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.