mirror of
https://github.com/YGGverse/cache-php.git
synced 2026-03-31 17:45:28 +00:00
Compare commits
6 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e9169066a | ||
|
|
639f1e8b3e | ||
|
|
bbbf224979 | ||
|
|
b2111aee02 | ||
|
|
e6903cc640 | ||
|
|
895a3dd6a2 |
2 changed files with 68 additions and 10 deletions
56
README.md
56
README.md
|
|
@ -1,2 +1,58 @@
|
||||||
# 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
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace YGGverse\Cache;
|
namespace Yggverse\Cache;
|
||||||
|
|
||||||
class Memory {
|
class Memory {
|
||||||
|
|
||||||
|
|
@ -19,7 +19,7 @@ class Memory {
|
||||||
$this->_timeout = $timeout;
|
$this->_timeout = $timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $key) : mixed
|
public function get(mixed $key) : mixed
|
||||||
{
|
{
|
||||||
$key = $this->_setKey(
|
$key = $this->_setKey(
|
||||||
[
|
[
|
||||||
|
|
@ -31,7 +31,7 @@ class Memory {
|
||||||
return $this->_memcached->get($key);
|
return $this->_memcached->get($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set(string $key, mixed $value = null, int $timeout = null) : bool
|
public function set(mixed $key, mixed $value = null, int $timeout = null) : bool
|
||||||
{
|
{
|
||||||
$key = $this->_setKey(
|
$key = $this->_setKey(
|
||||||
[
|
[
|
||||||
|
|
@ -40,10 +40,10 @@ class Memory {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout) + time());
|
return $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(string $key) : bool
|
public function delete(mixed $key) : bool
|
||||||
{
|
{
|
||||||
$key = $this->_setKey(
|
$key = $this->_setKey(
|
||||||
[
|
[
|
||||||
|
|
@ -55,7 +55,7 @@ class Memory {
|
||||||
return $this->_memcached->delete($key);
|
return $this->_memcached->delete($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getByValueCallback(string $key, mixed $value = null, int $timeout = null) : mixed
|
public function getByValueCallback(mixed $key, mixed $value = null, int $timeout = null) : mixed
|
||||||
{
|
{
|
||||||
$key = $this->_setKey(
|
$key = $this->_setKey(
|
||||||
[
|
[
|
||||||
|
|
@ -70,7 +70,7 @@ class Memory {
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout) + time()))
|
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout)))
|
||||||
{
|
{
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
@ -106,7 +106,7 @@ class Memory {
|
||||||
$arguments
|
$arguments
|
||||||
);
|
);
|
||||||
|
|
||||||
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout) + time()))
|
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout)))
|
||||||
{
|
{
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
@ -117,9 +117,11 @@ class Memory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function flush(int $delay = 60) : bool
|
public function flush(?int $delay = 0) : bool
|
||||||
{
|
{
|
||||||
return $this->_memcached->flush();
|
return $this->_memcached->flush(
|
||||||
|
$delay
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _setKey(mixed $key) : string
|
private function _setKey(mixed $key) : string
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue