1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- namespace Tqdev\PhpCrudApi\Cache;
-
- class MemcacheCache implements Cache
- {
- protected $prefix;
- protected $memcache;
-
- public function __construct(String $prefix, String $config)
- {
- $this->prefix = $prefix;
- if ($config == '') {
- $address = 'localhost';
- $port = 11211;
- } elseif (strpos($config, ':') === false) {
- $address = $config;
- $port = 11211;
- } else {
- list($address, $port) = explode(':', $config);
- }
- $this->memcache = $this->create();
- $this->memcache->addServer($address, $port);
- }
-
- protected function create(): object
- {
- return new \Memcache();
- }
-
- public function set(String $key, String $value, int $ttl = 0): bool
- {
- return $this->memcache->set($this->prefix . $key, $value, 0, $ttl);
- }
-
- public function get(String $key): String
- {
- return $this->memcache->get($this->prefix . $key) ?: '';
- }
-
- public function clear(): bool
- {
- return $this->memcache->flush();
- }
- }
|