mirror of
https://github.com/YGGverse/cache-php.git
synced 2026-03-31 17:45:28 +00:00
initial commit
This commit is contained in:
parent
fd7fdb416d
commit
38303b10a9
3 changed files with 79 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
/.vscode/
|
||||||
|
/vendor/
|
||||||
|
|
||||||
|
composer.lock
|
||||||
15
composer.json
Normal file
15
composer.json
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
60
src/Memcached.php
Normal file
60
src/Memcached.php
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace YGGverse\Cache;
|
||||||
|
|
||||||
|
class Memcached {
|
||||||
|
|
||||||
|
private $_memcached;
|
||||||
|
private $_namespace;
|
||||||
|
private $_timeout;
|
||||||
|
|
||||||
|
public function __construct(string $host, int $port, string $namespace, int $timeout)
|
||||||
|
{
|
||||||
|
$this->_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue