<?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的

解决方案 »

  1.   

    还没弄过memcache 的前排就坐.
      

  2.   

    前排就座。
    我们的nosql部分划分为三个级别,一个是高速临时缓存 一个是高速持久缓存 一个是低俗持久缓存。
    高速临时缓存用memcached来实现,服务一重启就导致缓存数据丢失了,因此用来保存可重建的缓存数据。
    高速持久缓存用redis来实现,那个东西会把缓存内容定期写入磁盘因此能持久化,用来保存无法重建但对性能要求较高的数据。
    低俗持久缓存用文件实现。
      

  3.   

    在父查询里面。但是调用某个指定的值的memcache的值.比较麻烦把。呵呵.
      

  4.   

    如果写在操作父类上,那delete 某个值如何操作啊 ?呵呵
      

  5.   

    memcahce都是应用在查询上面至于上面delete什么的这种事不需要缓存的,没有一点点意义,而select缓存我一直是将sql语句md5后放到memcache中,每次查询前检测memcache中是否有东西
      

  6.   

    那如果md5($sql)这个key对应的的内容日后更新了呢。你不delete 怎么同步新数据进去呢