diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bcc0b7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +vendor/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d6e41bd --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 minicli + +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..d4c4a54 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# minicache + +A simple JSON file cache built for minicli (but can be used in any app) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..82e4795 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "minicli/minicache", + "type": "library", + "description": "A simple json file cache for minicli", + "license": "MIT", + "homepage": "https://github.com/minicli/minicli", + "keywords": ["micro","cache","json"], + "autoload": { + "psr-4": { + "Minicli\\Minicache\\": "src/" + } + }, + "require": { + "minicli/minicli": "^1.0", + "ext-json": "*" + } +} diff --git a/src/FileCache.php b/src/FileCache.php new file mode 100644 index 0000000..73843f6 --- /dev/null +++ b/src/FileCache.php @@ -0,0 +1,71 @@ +cache_dir = $cache_dir; + $this->cache_expiry = $cache_expiry; + } + + /** + * @param string $unique_identifier + * @return string + */ + public function getCacheFile($unique_identifier) + { + return $this->cache_dir . '/' . md5($unique_identifier) . '.json'; + } + + /** + * @param string $unique_identifier + * @return null|string + */ + public function getCached($unique_identifier) + { + $cache_file = $this->getCacheFile($unique_identifier); + + if (is_file($cache_file)) { + return file_get_contents($cache_file); + } + + return null; + } + + /** + * @param string $unique_identifier + * @return null|string + */ + public function getCachedUnlessExpired($unique_identifier) + { + $cache_file = $this->getCacheFile($unique_identifier); + + // is it still valid? + if (is_file($cache_file) && (time() - filemtime($cache_file) < 60*$this->cache_expiry)) { + return file_get_contents($cache_file); + } + + return null; + } + + /** + * @param string $content + * @param string $unique_identifier + */ + public function save($content, $unique_identifier) + { + $cache_file = $this->getCacheFile($unique_identifier); + + file_put_contents($cache_file, $content); + } +} \ No newline at end of file