From f92c2bbd8b97c64f7de3c412289ec0cac634445b Mon Sep 17 00:00:00 2001 From: User Date: Tue, 10 Oct 2023 16:33:22 +0300 Subject: [PATCH] Init --- .gitignore | 3 + README.md | 21 +++++++ composer.json | 19 ++++++ src/Cache/Cache.php | 111 +++++++++++++++++++++++++++++++++++ src/Cache/CacheInterface.php | 40 +++++++++++++ 5 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Cache/Cache.php create mode 100644 src/Cache/CacheInterface.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31dc5a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +/vendor +composer.lock \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a154a8 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +## Rmphp/Cache + +Cache component for **Rmphp** + +## Install + +Stable version + +```bash +composer require rmphp/cache +``` +```bash +composer require rmphp/cache:"^1.0" +``` + + +Dev version contains the latest changes + +```bash +composer require rmphp/cache:"1.x-dev" +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..161f42d --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "rmphp/cache", + "license": "proprietary", + "authors": [ + { + "name": "Yuri Zuev", + "email": "y_zuev@mail.ru" + } + ], + "require": { + "php": "^8.1" + }, + "autoload": { + "psr-4": { + "Rmphp\\Cache\\": "src/" + } + } + +} diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php new file mode 100644 index 0000000..6ff0ac6 --- /dev/null +++ b/src/Cache/Cache.php @@ -0,0 +1,111 @@ +path = rtrim($path, "/"); + if(!is_dir($this->path) && !mkdir($this->path, 0777, true)) throw new Exception("Error create path"); + } + + /** @inheritDoc */ + public function save(string $name, $value, int $expires = 0) : bool { + if(empty($expires)) $expires = time() + $this->defaultTime; + if(!$this->fileSave($name, $expires."::".$name."::".serialize($value))) return false; + return true; + } + + /** @inheritDoc */ + public function getItem(string $key) { + if($this->hasItem($key)){ + $data = explode("::", $this->fileRead($key), 3); + return unserialize($data[2]); + } + return null; + } + + /** @inheritDoc */ + public function hasItem(string $key) : bool { + if (!empty($fileValue = $this->fileRead($key))){ + if(count($data = explode("::", $fileValue, 3)) != 3) return false; + if(time() < (int)$data[0]) return true; + } + $this->fileDelete($key); + return false; + } + + /** @inheritDoc */ + public function get(string $name, callable $function, int $expires = 0) : mixed { + if($this->hasItem($name)){ + $data = explode("::", $this->fileRead($name), 3); + return unserialize($data[2]); + } + $value = $function(); + if(empty($expires)) $expires = time() + $this->defaultTime; + $this->fileSave($name, $expires."::".$name."::".serialize($value)); + return $value; + } + + /** + * @param string $name + * @return object + */ + private function getPath(string $name) : object { + $hexName = md5($name); + $subPath = substr($hexName, 0, $this->subLength); + return (object)[ + "path" => $this->path."/".$subPath, + "file" => $this->path."/".$subPath."/".$hexName, + ]; + } + + /** + * @param string $name + * @param string $data + * @return bool + */ + private function fileSave(string $name, string $data) : bool { + $pathObject = $this->getPath($name); + if(!is_dir($pathObject->path)) mkdir($pathObject->path, 0777, true); + if(!file_put_contents($pathObject->file, $data)) return false; + return true; + } + + /** + * @param string $name + * @return string + */ + private function fileRead(string $name) : string { + $pathObject = $this->getPath($name); + if (file_exists($pathObject->file) && $fileValue = file_get_contents($pathObject->file)) return $fileValue; + return ""; + } + + /** + * @param $key + * @return bool + */ + public function fileDelete($key) : bool { + $pathObject = $this->getPath($key); + if(file_exists($pathObject->file)) unlink($pathObject->file); + return true; + } +} \ No newline at end of file diff --git a/src/Cache/CacheInterface.php b/src/Cache/CacheInterface.php new file mode 100644 index 0000000..2041131 --- /dev/null +++ b/src/Cache/CacheInterface.php @@ -0,0 +1,40 @@ +