From 79ae1a80a2dcda3241d85078a692a78ff72bd8dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A8ir=20Noordermeer?= Date: Wed, 28 Feb 2018 15:23:57 +0100 Subject: [PATCH] Add license & readme --- LICENSE.md | 21 +++++++++ README.md | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 1 + 3 files changed, 146 insertions(+) create mode 100644 LICENSE.md create mode 100644 README.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..920810c --- /dev/null +++ b/LICENSE.md @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c65a859 --- /dev/null +++ b/README.md @@ -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 +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'); +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 29df977..d331dff 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "iqparts/cache", "type": "library", + "license": "MIT", "require": { "predis/predis": "^1.1" },