Home >Backend Development >PHP Tutorial >php 打包memcache类

php 打包memcache类

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:23:05914browse

php 封装memcache类
/*
 * memcache类
  */
class Memcacheds{
    //声明静态成员变量
    private static $m = null;
    private static $cache = null;
    
    private function __construct() {
        self::$m = new Memcache();
        self::$m->connect('www.cat.com','11211'); //写入缓存地址,端口
    }

    //为当前类创建对象
    private static function Men(){
        self::$cache = new Memcacheds();
        return self::$m;
    }
    
    /*
     * 添加缓存数据
     * @param string $key 获取数据唯一key
     * @param String||Array $value 缓存数据
     * @param $time memcache生存周期(秒)
     */
    public static function setMen($key,$value,$time){
        self::Men()->set($key,$value,0,$time);
    }
    /*
     * 获取缓存数据
     * @param string $key
     * @return
     */
    public static function getMen($key){
        return self::Men()->get($key);
    }
    /*
     * 删除对应缓存数据
     * @param string $key
     * @return
     */
    public static function delMen($key){
        self::Men()->delete($key);
    }
    /*
     * 删除所有缓存数据
     */
    public static function delAllMen(){
        self::Men()->flush();
    }
    
    /*
     * 删除所有缓存数据
     */
    public static function menStatus(){
        return self::Men()->getStats();
    }

}


调用类

include_once 'memcache.php'; 引入类文件

$key = 'myKey';
$value = 'insert into 12334234';
$time = 60;
Memcacheds::setMen($key,$value,$time); //写入缓存

$get = Memcacheds::getMen($key); //读取

echo '

';<br>print_r($get);<br><p>?><br></p><p>结果输出: <br></p><p></p><pre class="brush:php;toolbar:false">insert into 12334234


版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn