在項(xiàng)目中,涉及大訪問量時(shí),合理的使用緩存能減輕數(shù)據(jù)庫的壓力,同時(shí)提升用戶體驗(yàn)。即在非實(shí)時(shí)性的需求的前提下,一小段時(shí)間內(nèi)(若干秒),用于顯示的數(shù)據(jù)從緩存中獲取的,而不用直接讀取數(shù)據(jù)庫,能有效的減少數(shù)據(jù)庫的讀取壓力。這里記錄一下php語言使用memcache的情形:
首先,我們建立一個(gè)memcachepool,可以根據(jù)不同的配置讀取,生成不同的memcache實(shí)例。用到$memcache->addServer($host,$port,$flag);向連接池中添加一個(gè)memcache服務(wù)器。代碼示例如下:
- class memcachePool{
- private static $instance;
- private $memcacheList = array();
- private function __construct(){
- }
- public static function getInstance(){
- if(self::$instance != null)
- return self::$instance;
- self::$instance = new memcachePool();
- return self::$instance;
- }
- /**
- * get memcache object from pool
- * @param [type] $host 服務(wù)器
- * @param [type] $port 端口
- * @param [type] $flag 控制是否使用持久化連接。默認(rèn)TRUE
- * @return [type]
- */
- public function getMemcache($host,$port,$flag){
- if(isset($this->memcacheList[$host.$port]))
- return $this->memcacheList[$host.$port];
- $memcache = new Memcache();
- // 向連接池中添加一個(gè)memcache服務(wù)器
- $memcache->addServer($host,$port,$flag);
- //開啟大值自動(dòng)壓縮,第一個(gè)參數(shù)表示處理數(shù)據(jù)大小的臨界點(diǎn),第二個(gè)參數(shù)表示壓縮的比例,默認(rèn)為0.2
- $memcache->setCompressThreshold(2000,0.2);
- $this->memcacheList[$host.$port] = $memcache;
- return $memcache;
- }
- }
接著實(shí)現(xiàn)一個(gè)包含memcache常用方法如add,set,get,flush,delete等的方法類,這里命名為dlufmemcache
- class dlufMemcache{
- private $memcache = null;
- function __construct($host,$port){
- $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true);
- }
- /**
- * memcache set value
- * @param [type] $key 鍵
- * @param [type] $value 值
- * @param integer $expire 到期的時(shí)間,如果此值設(shè)置為0表明此數(shù)據(jù)永不過期
- * @param integer $flag 標(biāo)志位 使用MEMCACHE_COMPRESSED指定對值進(jìn)行壓縮(使用zlib)
- * @param [type] $serializetype
- */
- public function set($key,$value,$expire=0,$flag=0,$serializetype=null){
- if($serializetype == 'json' && is_array($value)){
- $value = json_encode($value);
- }
- $this->memcache->set($key,$value,$flag,$expire);
- }
- /**
- * 從服務(wù)端查找元素
- * @param [type] $key
- * @return [type]
- */
- public function get($key){
- return $this->memcache->get($key);
- }
- /**
- * 增加一個(gè)條目到緩存服務(wù)器
- * @param [type] $key
- * @param [type] $value
- * @param integer $expire
- * @param integer $flag
- * @param [type] $serializetype
- */
- public function add($key,$value,$expire=0,$flag=0,$serializetype=null){
- if($serializetype == 'json' && is_array($value)){
- $value = json_encode($value);
- }
- $ret = $this->memcache->add($key,$value,$flag,$expire);
- return $ret;
- }
- /**
- * 清洗(刪除)已經(jīng)存儲(chǔ)的所有的元素
- * @return [type]
- */
- public function flush(){
- return $this->memcache->flush();
- }
- /**
- * 從服務(wù)端刪除一個(gè)元素
- * @param [type] delete 參數(shù):key要?jiǎng)h除的元素的key 刪除該元素的執(zhí)行時(shí)間 timeout如果值為0,則該元素立即刪除。
- * @return [type]
- */
- public function delete($key){
- $ret = $this->memcache->delete($key,0);
- return $ret;
- }
- }
然后調(diào)用dlufmemcache:
- $memcache = new dlufMemcache('127.0.0.1',11211);
- $memcache->set('memcache','come on dluf&baidu !!!!!!');
- $ret = $memcache->get('memcache');
- echo print_r($ret,true);
新聞熱點(diǎn)
疑難解答