Compare commits

...

7 commits
0.1.0 ... main

Author SHA1 Message Date
ghost
3e9169066a fix flush delay 2024-02-03 01:00:45 +02:00
ghost
639f1e8b3e allow mixed key data types 2024-01-27 10:41:04 +02:00
ghost
bbbf224979 update readme 2024-01-24 04:20:29 +02:00
ghost
b2111aee02 fix namespace 2023-10-09 04:50:52 +03:00
ghost
e6903cc640 remove current timestamp prefix from timeout definition 2023-08-13 12:18:44 +03:00
ghost
895a3dd6a2 update readme 2023-08-13 12:04:26 +03:00
ghost
bf12b5b7e5 add callback methods support, update keys format 2023-08-10 15:31:31 +03:00
2 changed files with 148 additions and 19 deletions

View file

@ -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
);
```

View file

@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace YGGverse\Cache; namespace Yggverse\Cache;
class Memory { class Memory {
@ -19,11 +19,58 @@ class Memory {
$this->_timeout = $timeout; $this->_timeout = $timeout;
} }
public function get(string $key, mixed $value = null, int $timeout = null) : mixed public function get(mixed $key) : mixed
{ {
if (false === $result = $this->_memcached->get($this->_key($key))) $key = $this->_setKey(
[
$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))
{ {
if (true === $this->set($key, $value, $timeout)) return $value;
}
else
{
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout)))
{ {
return $value; return $value;
} }
@ -32,29 +79,55 @@ 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
{ {
return $result; $value = call_user_func_array(
[
$object,
$method
],
$arguments
);
if (true === $this->_memcached->set($key, $value, ($timeout ? $timeout : $this->_timeout)))
{
return $value;
}
else
{
return false;
}
} }
} }
public function set(string $key, mixed $value, int $timeout = null) public function flush(?int $delay = 0) : bool
{ {
return $this->_memcached->set($this->_key($key), $value, ($timeout ? $timeout : $this->_timeout) + time()); return $this->_memcached->flush(
$delay
);
} }
public function delete(string $key) : bool private function _setKey(mixed $key) : string
{ {
return $this->_memcached->delete($this->_key($key)); return md5(
} 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);
} }
} }