zoukankan      html  css  js  c++  java
  • 自己的memcache类

    Mem类代码:
    class Mem
    {
        //类型是memcache或memcached
        private $type = 'Memcached';
        //会话
        private $m;
        //缓存时间,0代表永久
        private $time = 0;
        //错误信息
        private $error;
        //调试模式
        private $debug = 'true';

        public function __construct()
        {
            if (!class_exists($this->type)) 
            {
                $this->error = 'No '.$this->type;
                return false;
            }
            else
            {
                $this->m = new $this->type;
            }
        }

        //添加服务器
        //$arr应该为数组
        public function addServer($arr)
        {
            $this->m->addServers($arr);
        }

        //数据操作
        public function s($key, $value = '', $time = 0)
        {
            $number = func_num_args();
            if($number == 1)
            {
                //get
                return $this->get($key);
            }
            else if ($number >= 2) 
            {
                if ($value === NULL) 
                {
                    //delete
                    $this->delete($key);
                }
                else
                {
                    //set
                    $this->set($key, $value, $time);
                }
            }
        }

        //set
        private function set($key, $value, $time  = 0)
        {
            if ($time === NULL) 
            {
                $time = $this->time;
            }
            $this->m->set($key, $value, $time);
            if ($this->debug) 
            {
                 if ($this->m->getResultCode() != 0) 
                {
                    return false;
                }
            }
        }

        //get
        private function get($key)
        {
            $result = $this->m->get($key);
            if ($this->debug) 
            {
                if ($this->m->getResultCode() != 0) 
                {
                    return false;
                }
                else 
                {
                    return $result;
                }
            }
            else 
            {
                return $result;
            }
        }

        //delete
        private function delete($key)
        {
            $this->m->delete($key);
        }

        //获取错误信息
        public function getError()
        {
            if($this->error)
            {
                return $this->error;
            }
            else
            {
                return $this->m->getResultMessage();
            }
        }
    }


    Mem类测试代码:
     




  • 相关阅读:
    vector详解
    笔记
    积木大赛
    codevs 1086 栈(Catalan数)
    不要把球传我
    同余方程 (codevs1200)
    最小集合
    数的计算
    产生数
    逃跑的拉尔夫
  • 原文地址:https://www.cnblogs.com/phonecom/p/281cf6cdc25d068923686fddb58ca085.html
Copyright © 2011-2022 走看看