`
zhengdl126
  • 浏览: 2509995 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

memcache运用代码

 
阅读更多

interface ICache {

    public function put($key,$var,$timeout=0);

    public function get($key);

    public function delete($key);

    public function close();

    public function open($params);

}









class FileCache{

   private $filecache_path;
  /**
     * 構造函數
     * @param String $file DbFlat文件名,不存在將創建
     * @param Int $mode DbFlat文件的讀寫模式
     * @param Int $perm DbFlat文件權限
     */
    function __construct($filePath){
        if(file_exists($filePath)){
            $this->filecache_path = $filePath;
        }else{
            $this->make_dir($filePath);
            $this->filecache_path = $filePath;
        }
    }
  
   /**
     * 存儲一個Key
     * @param String $key Key
     * @param String $val Value
     * @return Boolean
     */
    function store($key,$val){
         $files = $this->filecache_path."/{$key}_filecache";
         return file_put_contents($files, serialize($val));
    }
   
    /**
     * 獲得指定Key的值
     * @param String $key Key
     * @return String
     */
    public function fetch($key){
        $files = $this->filecache_path."/{$key}_filecache";
        if(file_exists($files)){
            $str = file_get_contents($files);
            if($str === false || $str === '') {
                return null;
            }
            else{
                 return unserialize($str);
            }
        }
    }
   
  /**
     * 刪除一個Key
     * @param String $key Key
     * @return Boolean
     */
    public function delete($key){
        $files = $this->filecache_path."/{$key}_filecache";
        if(file_exists($files)){
            unlink($files);
        }
    }
   
    /**
     * 刪除一個數組里的所以Key
     * @param Array $key_array
     * @return void
     */
    public function delete_array($key_array){
        foreach($key_array as $key){
            $this->delete($key);
        }
    }
   
    /**
     * 遞迴生成文件夾
     * @param str $path 文件夾path, 不是文件的path
     * @return mixed 成功時返回創建成功的path,失敗時返回false
     */
    public function make_dir($path)
    {
        if(! file_exists($path))
        {
            $this->make_dir(dirname($path));
            mkdir($path, 0777);
        }
        return realpath($path);
    }
}






require_once('ICache.iface.php');

class ApcCache implements ICache {
    public function put($key,$var,$timeout=0) {
        $succ = true;//apc_store($key,$var,$timeout);
        return $succ;
    }
   
    public function get($key) {
//echo "<hr>get==$key==from apc ==<hr>";
        //return apc_fetch($key);
        return '';
    }

    public function delete($key) {
        //return apc_delete($key);
        return '';
    }

    public function close() {    }

    public function open($params) {    }

}




require_once('ICache.iface.php');

class MemCached implements ICache {
    //实例化memcache对象
    public  $mc = null;
   
    /**
     * 构造实例化
     * $config_array:memcache服务器IP和端口
     *@return  obj
     */
    public function __construct($config_array){
        //服务器端调用方式
        $this->mc = new Memcache;
        //$mc->connect($memcache_url, 11212);   #[单一服务器可用]
        if (is_array($config_array)) {
            foreach ($config_array as $key=>$value){
                $this->mc->addServer($value['IP'], $value['port']);
            }
        }
        $this->mc->setCompressThreshold(10240, 0.2);

 

/*

 $this->mc->setCompressThreshold(10240, 0.2);  此函数在memcache2.0.0加入。
Memcache::setCompressThreshold()开启对于大值的自动压缩。 同样你也可以使用函数memcache_set_compress_threshold()。
参数一:控制多大值进行自动压缩的阈值。
参数二:指定经过压缩实际存储的值的压缩率,支持的值必须在0和1之间。默认值是0.2表示20%压缩率。

$memcache_obj = new Memcache;
$memcache_obj->addServer('memcache_host', 11211);
$memcache_obj->setCompressThreshold(20000, 0.2);

*/
    }
   
    /**
     * 值加入到memcache
     * $key:标识
     * $var:值
     * $timeout:失效时间 默认时间86400表示为一天时间。
     *@return  true or false
     */
    public function put($key,$var,$timeout=86400,$is_compress=0) {
        $succ = $this->mc->set($key,$var,$is_compress,$timeout);
        if($succ === false){
            //如果失败,休息一秒钟,则继续重写
            sleep(1);
            $succ = $this->mc->set($key,$var,$is_compress,$timeout);
            if($succ  === false ){
                log_error('set memache key failed!! key:'.$key.' value:'.serialize($var).' timeout:'.$timeout, __FILE__, __LINE__); 
            }
        }
        return $succ;
    }
   
    /**
     * 根据key值从memcache得到值
     * $key:标识
     *@return  string or array
     */
    public function get($key) {
        $result = $this->mc->get($key);
        if($result === false && (is_string($key) && strpos($key,'online') === false)){
            //log_error('get memache key ('.$key.') failed!!', __FILE__, __LINE__);
        }
        return $result;
    }
   
    /**
     * 根据key值从memcache删除值
     * $key:标识
     *@return  true or false
     */
    public function delete($key) {
        $succ = $this->mc->delete($key);
        if($succ === false){
            //如果失败,休息一秒钟,则继续删除
            sleep(1);
            $succ = $this->mc->delete($key);
            //if($succ === false){
            //    log_error('delete memache key('.$key.') failed!!',__FILE__,__LINE__);
            //}
        }
        return $succ;
    }

    /**
     * 关键memcache
     *@return  true or false
     */
    public function close(){
        return $this->mc->close();
    }
   
    public function open($params) {    }

}






require_once('ICache.iface.php');
require_once('LocalCache.class.php');

class CacheStore{
    private $_caches;
    private $_storeName;
    private $_defaultCache;
   
    public function setName($name){
        $this->_storeName = $name;
    }
   
    public function setCache(ICache $cache, $privilege=0){
        $this->_caches[$privilege] = $cache;
    }
   
    public function getCache($privilege=0){
        return $this->_caches[$privilege];   
    }

    public function init(){
        //if(!is_array($this->_caches)){
        $this->setCache(new LocalCache());
        //}
        ksort($this->_caches);
       
    }
   
    public function put($key, $value, $privilege=-1){
       
        if($privilege == 0){ //指定赋值给默认的
            $this->_caches[0]->put($this->_storeName . '_' . $key, $value);
        }else{
            foreach($this->_caches as $cache){
                $cache->put($this->_storeName . '_' . $key, $value);
            }
        }

    }
   
    public function get($key){
        $i = 0;
        foreach($this->_caches as $cache){
            $val = null;
            $val = $cache->get($this->_storeName . '_' . $key);
            if($val!=null){
                if($i > 0){
                    $this->put($key,$val,0);
                }
                return $val;
            }
            $i++;
        }
        return $val;
    }
}






$arr_server_memcache = array(
    array(
        'IP' => '192.168.101.73',
        'port' => '12000'
    ),
    array(
        'IP' => '192.168.101.74',
        'port' => '12000'
    ),
    array(
        'IP' => '192.168.101.75',
        'port' => '12000'
    ),
    array(
        'IP' => '192.168.101.76',
        'port' => '12000'
    ),
    array(
        'IP' => '192.168.101.77',
        'port' => '12000'
    )
);


require('pworks/util/cache/CacheStore.class.php');
require_once('pworks/util/cache/ApcCache.class.php');
require_once('pworks/util/cache/MemCache.class.php');


 $appCache = new CacheStore();
    $appCache->setName(MARKSIX_SYSTEM.'_mvc_app_obj');
    //$appCache->setCache(new ApcCache());
    //加入memcached缓存
    if (MEMCACHE) {
         $appCache->setCache(new MemCached($arr_server_memcache), 'mc');
    }
    $appCache->init();

 

 

 

 

 

 

/**
     * 得到memcache实例
     */
    function memc(){
        return FrontController::getCache('mc');
    }

 

 

    /**
     * 得到memcache里存放当前用户标识的key
     */
    function get_mem_key(){
        return COM_TYPE == 'f' ? get_online_member_key($_SESSION['memberId']) : get_online_user_key($_SESSION['userId']);
    }

    /**
     * 给memcache里存放当前用户标识的key赋值当前的session,EXPIRE_TIME默认为 10
     */
    function set_mem_key(){
        return memc()->put (get_mem_key(), get_new_session_id(get_session_id()), EXPIRE_TIME);
    }

 

 

 

分享到:
评论

相关推荐

    C语言memcache代码文档

    C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档

    memcache配置源代码

    memcache配置源代码

    C语言memcache代码2

    C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码

    C语言memcache代码

    C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码

    java遍历Memcache详细代码

    最为高效的java遍历Memcache详细代码

    memcache源代码分析

    memcached 源代码分析,出自社区大咖,货真价实,值得信赖。

    memcache 范例(附源代码)

    memcache 范例 附源代码 缓存 超级不错的缓存解决方案

    memcache.php

    memcache也有一款图形界面的监控工具(memcachephp),可以通过这个工具查看到局域网内所有部署memcache机器或者端口的memcache的运行情况,对我们监控memcache的缓存命中率、cache利用率、点击率等信息有很好的帮助...

    memcache图形监控工具phpmemcache

    memcache图形监控工具phpmemcache,尽是一个PHP文件就可以实现对memcache的监控。 使用方法:本地测试监控机安装Apache或者下载XAMPP(Apache+MySQL+PHP+PERL),安装后把memcachephp.zip中的memcache.php文件放到...

    memcache1.2.1 for windows

    windows下memcache安装包 附带php扩展包

    memcache安装包,memcache

    memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问。

    memcache的源代码.zip感觉自己水平高的,可以下载研究研究

    memcached的源代码.zip

    php_memcache 服务扩展

    测试代码:注意端口的匹配 $memcache = new Memcache; $memcache-&gt;connect("localhost",11211); echo "Server's version: " . $memcache-&gt;getVersion() . "\n"; $tmp_object = new stdClass; $tmp_object-&gt;str_attr ...

    MemCache开发说明文档

    Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从...

    最新windows版php_memcache.dll和memcache.exe

    最新windows的memcache模块下载 这个模块是平和php5.3的,在我的windowsxp php5.3.5上安装成功 里面有两个php库,一个php_memcache.dll.vc6 和一个php_memcache.dll.vc9 另外一个windows的memcache.exe文件,都是网上...

    memcache

    将信息保持memcache中

    Memcache完全剖析 最实用的Memcache文档

    Memcache就不用多介绍了,做开发的人都知道。 但要用得好,却并不是那么容易的事。 如果用得不好,反而得不偿失。 这篇文档短小精悍,囊括了使用过程中需要要注意的方方面面。值得一读。

    delphi memcache MemCache.0.2.0.zip

    MemCache.0.2.0.zip Memcached Client for Delphi 客户端调用类 MemCache.0.2.0.zip Show all LinksExternal links Memcached Project This project is a delphi unit which implements a thread safe client for ...

    简单的memcache命令

    简单的memcache命令

    Memcache win32

    windows memcache 安装服务,php_memcache.dll所有版本扩展dll 安装说明 在命令行下安装Memcache,输入 ‘c:/memcached/memcached.exe -d install’。 3.启动Memcache,再输入: ‘c:/memcached/memcached.exe -d ...

Global site tag (gtag.js) - Google Analytics