Add license & readme
This commit is contained in:
21
LICENSE.md
Normal file
21
LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Industrial Quality Parts
|
||||
|
||||
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
> of this software and associated documentation files (the "Software"), to deal
|
||||
> in the Software without restriction, including without limitation the rights
|
||||
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
> copies of the Software, and to permit persons to whom the Software is
|
||||
> furnished to do so, subject to the following conditions:
|
||||
>
|
||||
> The above copyright notice and this permission notice shall be included in
|
||||
> all copies or substantial portions of the Software.
|
||||
>
|
||||
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
> THE SOFTWARE.
|
||||
124
README.md
Normal file
124
README.md
Normal file
@@ -0,0 +1,124 @@
|
||||
# IQParts Cache
|
||||
PHP Cache library with namespace, filesystem and (p)redis implementations.
|
||||
|
||||
### Requirements
|
||||
Requires PHP 7.0+. Older versions might work but have not been tested.
|
||||
|
||||
### Another cache library
|
||||
This cache library has been highly inspired by [Genkgo.Cache](https://raw.githubusercontent.com/genkgo/cache). We recommend using this library as it implements more adapters. The reason for this library was the need to have a backup for redis which still was able to store cached values with a variable TTL.
|
||||
|
||||
### Code
|
||||
This library attempts to comply with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) & [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md).
|
||||
|
||||
### Create your own adapter
|
||||
|
||||
Create an adapter that implements CacheAdapterInterface. Take the MemoryAdapter as example:
|
||||
|
||||
``` php
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace IQParts\Cache\Adapter;
|
||||
|
||||
final class MemoryAdapter implements CacheAdapterInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $data = [];
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $timeToLive = [];
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
if (!isset($this->data[$key])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isset($this->timeToLive[$key])) {
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
if ($this->ttl($key) > 0) {
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
unset($this->timeToLive[$key]);
|
||||
unset($this->data[$key]);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param $value
|
||||
* @param int|null $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null)
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
if ($ttl !== null) {
|
||||
$this->timeToLive[$key] = time() + $ttl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
unset($this->data[$key]);
|
||||
unset($this->timeToLive[$key]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function keys($key = '*')
|
||||
{
|
||||
$matches = [];
|
||||
foreach ($this->data as $name => $value) {
|
||||
if (fnmatch($key, $name)) {
|
||||
$matches[] = $name;
|
||||
}
|
||||
}
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return int
|
||||
*/
|
||||
public function ttl($key): int
|
||||
{
|
||||
if (isset($this->timeToLive[$key])) {
|
||||
return max($this->timeToLive[$key] - time(), 0);
|
||||
}
|
||||
return self::NO_TTL;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Multiple Adapters
|
||||
Adapters can be queued. Example:
|
||||
``` php
|
||||
$cache = new NamespaceAdapter(
|
||||
'my-namespace:',
|
||||
new MemoryAdapter()
|
||||
);
|
||||
|
||||
/**
|
||||
* This will result in 'my-namespace:my-key'
|
||||
* set to 'my-value'
|
||||
*/
|
||||
$cache->set('my-key', 'my-value');
|
||||
```
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"name": "iqparts/cache",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"predis/predis": "^1.1"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user