<?php
class cache_memcache { private $memcache = null;
private $isconnect ='false';
public function __construct($host = '' , $port = '',$type = true) {
$this->memcache = new Memcache;
$this->isconnect =@$this->memcache->connect($host, $port, $type);
}
public function is_connect_memcache(){
return $this->isconnect; }
public function cache_memcache($host = '' , $port = '',$type = true) {
$this->__construct($host, $port, $type);
} public function get($name) {
if($this->is_connect_memcache()){
$value = $this->memcache->get($name);
return $value;
}else{
static $result = array();
if (!empty($result[$name]))
{
return $result[$name];
}
$cache_file_path = ROOT_PATH . '/temp/static_caches/' . $name . '.php';
if (@file_exists($cache_file_path))
{
include_once($cache_file_path);
$result[$name] = $data;
return $result[$name];
}
else
{
return false;
}
}
} public function set($name, $value, $ttl = 0, $ext1='', $ext2='') { if($this->is_connect_memcache()){
return $this->memcache->set($name, $value, false, $ttl); }else{
$cache_file_path = ROOT_PATH . '/temp/static_caches/' . $name . '.php';
$content = "<?php\r\n";
$content .= "\$data = " . var_export($value, true) . ";\r\n";
$content .= "?>";
file_put_contents($cache_file_path, $content, LOCK_EX); return true;
}
} public function delete($name) { if($this->is_connect_memcache()){
return $this->memcache->delete($name); }else{ $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $name . '.php';
if (file_exists($cache_file_path))
{
@unlink($cache_file_path);
} return true;
}
} public function flush() { if($this->is_connect_memcache()){
return $this->memcache->flush(); }else{ return true;
}
}
}
?>想在自己的站上用memcache。但是memcache部署在另外一台服务器上。怕当另外一台服务器遭遇意外。就写了个memcache结合php 文件缓存的类。当memcache失效的时候起用文件缓存。请问大家有什么好建议吗。以上是代码我想知道,大家讨论下。大家在项目中是怎么实施memcache的
class cache_memcache { private $memcache = null;
private $isconnect ='false';
public function __construct($host = '' , $port = '',$type = true) {
$this->memcache = new Memcache;
$this->isconnect =@$this->memcache->connect($host, $port, $type);
}
public function is_connect_memcache(){
return $this->isconnect; }
public function cache_memcache($host = '' , $port = '',$type = true) {
$this->__construct($host, $port, $type);
} public function get($name) {
if($this->is_connect_memcache()){
$value = $this->memcache->get($name);
return $value;
}else{
static $result = array();
if (!empty($result[$name]))
{
return $result[$name];
}
$cache_file_path = ROOT_PATH . '/temp/static_caches/' . $name . '.php';
if (@file_exists($cache_file_path))
{
include_once($cache_file_path);
$result[$name] = $data;
return $result[$name];
}
else
{
return false;
}
}
} public function set($name, $value, $ttl = 0, $ext1='', $ext2='') { if($this->is_connect_memcache()){
return $this->memcache->set($name, $value, false, $ttl); }else{
$cache_file_path = ROOT_PATH . '/temp/static_caches/' . $name . '.php';
$content = "<?php\r\n";
$content .= "\$data = " . var_export($value, true) . ";\r\n";
$content .= "?>";
file_put_contents($cache_file_path, $content, LOCK_EX); return true;
}
} public function delete($name) { if($this->is_connect_memcache()){
return $this->memcache->delete($name); }else{ $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $name . '.php';
if (file_exists($cache_file_path))
{
@unlink($cache_file_path);
} return true;
}
} public function flush() { if($this->is_connect_memcache()){
return $this->memcache->flush(); }else{ return true;
}
}
}
?>想在自己的站上用memcache。但是memcache部署在另外一台服务器上。怕当另外一台服务器遭遇意外。就写了个memcache结合php 文件缓存的类。当memcache失效的时候起用文件缓存。请问大家有什么好建议吗。以上是代码我想知道,大家讨论下。大家在项目中是怎么实施memcache的
我们的nosql部分划分为三个级别,一个是高速临时缓存 一个是高速持久缓存 一个是低俗持久缓存。
高速临时缓存用memcached来实现,服务一重启就导致缓存数据丢失了,因此用来保存可重建的缓存数据。
高速持久缓存用redis来实现,那个东西会把缓存内容定期写入磁盘因此能持久化,用来保存无法重建但对性能要求较高的数据。
低俗持久缓存用文件实现。