php 封装memcache类

<?php
/*
 * 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'); //写入缓存地址,port
    }

    //为当前类创建对象
    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();
    }

}


<----------------------------------------------------------->

调用类

<?php

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

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

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

echo '<pre>';
print_r($get);

?

>

结果输出:

insert into 12334234


原文地址:https://www.cnblogs.com/yangykaifa/p/6851730.html