mirror of
https://github.com/YGGverse/cache-php.git
synced 2026-04-01 18:15:29 +00:00
Compare commits
No commits in common. "main" and "0.1.0" have entirely different histories.
2 changed files with 19 additions and 148 deletions
56
README.md
56
README.md
|
|
@ -1,58 +1,2 @@
|
||||||
# cache-php
|
# cache-php
|
||||||
Cache tools for PHP applications
|
Cache tools for PHP applications
|
||||||
|
|
||||||
### Memory
|
|
||||||
|
|
||||||
Extends [PHP memcached](https://www.php.net/manual/en/book.memcached.php)
|
|
||||||
|
|
||||||
#### Init
|
|
||||||
|
|
||||||
```
|
|
||||||
$memory = new \Yggverse\Cache\Memory(
|
|
||||||
|
|
||||||
'localhost', // memcached server host, localhost by default
|
|
||||||
11211, // memcached server port, 11211 by default
|
|
||||||
|
|
||||||
'my_app', // application namespace
|
|
||||||
3600 + time() // cache time by default
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Supported methods
|
|
||||||
|
|
||||||
##### Memory::set
|
|
||||||
|
|
||||||
##### Memory::delete
|
|
||||||
|
|
||||||
##### Memory::flush
|
|
||||||
|
|
||||||
##### Memory::get
|
|
||||||
|
|
||||||
##### Memory::getByValueCallback
|
|
||||||
|
|
||||||
Return cached or cache new value of plain value callback
|
|
||||||
|
|
||||||
```
|
|
||||||
$value = $memory->getByValueCallback(
|
|
||||||
'key_name', // string, unique key name
|
|
||||||
'value', // mixed, plain value
|
|
||||||
3600 + time(), // optional, cache timeout for this value
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Memory::getByMethodCallback
|
|
||||||
|
|
||||||
Return cached or cache new value of object method callback
|
|
||||||
|
|
||||||
```
|
|
||||||
$value = $memory->getByMethodCallback(
|
|
||||||
$class_object, // object of method class
|
|
||||||
'method_name', // object method name
|
|
||||||
[
|
|
||||||
$method_attribute_1, // optional, array of attributes callback method requires
|
|
||||||
$method_attribute_2,
|
|
||||||
...
|
|
||||||
]
|
|
||||||
3600 + time(), // optional, cache timeout for this value
|
|
||||||
);
|
|
||||||
```
|
|
||||||
111
src/Memory.php
111
src/Memory.php
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Yggverse\Cache;
|
namespace YGGverse\Cache;
|
||||||
|
|
||||||
class Memory {
|
class Memory {
|
||||||
|
|
||||||
|
|
@ -19,58 +19,11 @@ class Memory {
|
||||||
$this->_timeout = $timeout;
|
$this->_timeout = $timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(mixed $key) : mixed
|
public function get(string $key, mixed $value = null, int $timeout = null) : mixed
|
||||||
{
|
{
|
||||||
$key = $this->_setKey(
|
if (false === $result = $this->_memcached->get($this->_key($key)))
|
||||||
[
|
|
||||||
$this->_namespace,
|
|
||||||
$key
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->_memcached->get($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function set(mixed $key, mixed $value = null, int $timeout = null) : bool
|
|
||||||
{
|
|
||||||
$key = $this->_setKey(
|
|
||||||
[
|
|
||||||
$this->_namespace,
|
|
||||||
$key
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function delete(mixed $key) : bool
|
|
||||||
{
|
|
||||||
$key = $this->_setKey(
|
|
||||||
[
|
|
||||||
$this->_namespace,
|
|
||||||
$key
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->_memcached->delete($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getByValueCallback(mixed $key, mixed $value = null, int $timeout = null) : mixed
|
|
||||||
{
|
|
||||||
$key = $this->_setKey(
|
|
||||||
[
|
|
||||||
$this->_namespace,
|
|
||||||
$key
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (false !== $value = $this->_memcached->get($key))
|
|
||||||
{
|
{
|
||||||
return $value;
|
if (true === $this->set($key, $value, $timeout))
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout)))
|
|
||||||
{
|
{
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
@ -79,55 +32,29 @@ class Memory {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function getByMethodCallback(object $object, string $method, array $arguments = [], int $timeout = null) : mixed
|
|
||||||
{
|
|
||||||
$key = $this->_setKey(
|
|
||||||
[
|
|
||||||
$this->_namespace,
|
|
||||||
$object,
|
|
||||||
$method,
|
|
||||||
$arguments
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (false !== $value = $this->_memcached->get($key))
|
|
||||||
{
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$value = call_user_func_array(
|
return $result;
|
||||||
[
|
|
||||||
$object,
|
|
||||||
$method
|
|
||||||
],
|
|
||||||
$arguments
|
|
||||||
);
|
|
||||||
|
|
||||||
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout)))
|
|
||||||
{
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function flush(?int $delay = 0) : bool
|
public function set(string $key, mixed $value, int $timeout = null)
|
||||||
{
|
{
|
||||||
return $this->_memcached->flush(
|
return $this->_memcached->set($this->_key($key), $value, ($timeout ? $timeout : $this->_timeout) + time());
|
||||||
$delay
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _setKey(mixed $key) : string
|
public function delete(string $key) : bool
|
||||||
{
|
{
|
||||||
return md5(
|
return $this->_memcached->delete($this->_key($key));
|
||||||
json_encode($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