<?php
namespace ticky\cache\driver;
use ticky\cache\contract;
class memcached extends contract {
protected $link = null;
protected $config = array(
'host' => '127.0.0.1', 'port' => 11211, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', );
public function __construct($config = array()) {
if (!extension_loaded('memcache')) {
showmsg('not support: memcache', 'stop');
}
if (!empty($config)) {
$this->config = array_merge($this->config, $config);
}
$this->link = new Memcache();
$this->link->addServer($this->config['host'], $this->config['port'], $this->config['persistent']);
}
public function exists($key) {
$file = $this->_file($key);
if (!is_file($file)) {
return false;
}
return true;
}
public function set($name, $value, $expire = null) {
$expire = $expire ? $expire : $this->config['expire'];
if (is_array($value)) {
$value = json_encode($value);
}
return $this->link->set($name, $value, false, $expire);
}
public function get($name) {
$value = $this->link->get($name);
$value_serl = json_decode($value, true);
if (is_array($value_serl)) {
return $value_serl;
}
return $value;
}
public function delete($name) {
return $this->link->delete($name);
}
public function clean() {
return $this->link->flush();
}
}