From 38303b10a9f0601db1ba02b2cab718ea23ea166b Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 9 Aug 2023 20:12:41 +0300 Subject: [PATCH] initial commit --- .gitignore | 4 ++++ composer.json | 15 ++++++++++++ src/Memcached.php | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Memcached.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0604025 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vscode/ +/vendor/ + +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ceb985b --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "yggverse/cache", + "description": "Cache tools for PHP applications", + "type": "library", + "require": { + "php": ">=8.1" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Yggverse\\Cache\\": "src/" + } + }, + "minimum-stability": "alpha" +} diff --git a/src/Memcached.php b/src/Memcached.php new file mode 100644 index 0000000..6eb699d --- /dev/null +++ b/src/Memcached.php @@ -0,0 +1,60 @@ +_memcached = new Memcached(); + $this->_memcached->addServer($host, $port); + + $this->_namespace = $namespace; + $this->_timeout = $timeout; + } + + public function get(string $key, mixed $value = null, int $timeout = null) : mixed + { + if (false === $result = $this->_memcached->get($this->_key($key))) + { + if (true === $this->set($key, $value, $timeout)) + { + return $value; + } + else + { + return false; + } + } + else + { + return $result; + } + } + + public function set(string $key, mixed $value, int $timeout = null) + { + return $this->_memcached->set($this->_key($key), $value, ($timeout ? $timeout : $this->_timeout) + time()); + } + + public function delete(string $key) : bool + { + return $this->_memcached->delete($this->_key($key)); + } + + public function flush(int $delay = 60) + { + return $this->_memcached->flush(); + } + + private function _key(string $key) : string + { + return sprintf('%s.%s', $this->_namespace, $key); + } +} \ No newline at end of file