PHP教程:php操作memcached方法实例
1首先建立Clsss类文件memcached.class.php
<?phpclass Memcacheds{ //声明静态成员变量 private static $m = null; private static $cache = null; public function __construct() { self::$m = new Memcached(); self::$m->addServer(‘127.0.0.1‘,‘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,$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(); } }?>
2使用方法实例
//引入类文件 require dirname(__FILE__).‘/memcached.class.php‘; function Get_memcached($str,$key,$time){ $key = md5($key); Memcacheds::setMen($key,$str,$time); //写入缓存 $get = Memcacheds::getMen($key); //读取缓存 return $get; } echo function Get_memcached(‘data‘,‘key‘,3600);
希望本文所述对大家的php程序设计有所帮助。
相关推荐
xuebingnan 2020-08-24
聪聪李 2020-09-10
songshijiazuaa 2020-09-01
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28