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程式設計有所幫助,
原文來源:https://www.newbii.cn/20200505222222.htm
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/45110.html
標籤:PHP
上一篇:PHP7的Yaconf使用教程
