PHP内存缓存Memcached类有需要的朋友可参考一下,PHP内存缓存Memcached类代码如下:
- <?PHP
- class MemcacheModel {
- private $mc = null;
-
-
-
- function __construct(){
- $params = func_get_args();
- $mc = new Memcache;
-
- if( count($params) > 1){
- foreach ($params as $v){
- call_user_func_array(array($mc, 'addServer'), $v);
- }
-
- } else {
- call_user_func_array(array($mc, 'addServer'), $params[0]);
- }
- $this->mc=$mc;
- }
-
-
-
-
- function getMem(){
- return $this->mc;
- }
-
-
-
-
- function mem_connect_error(){
- $stats=$this->mc->getStats();
- if(emptyempty($stats)){
- return false;
- }else{
- return true;
- }
- }
-
- private function addKey($tabName, $key){
- $keys=$this->mc->get($tabName);
- if(emptyempty($keys)){
- $keys=array();
- }
-
- if(!in_array($key, $keys)) {
- $keys[]=$key;
- $this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0);
- return true;
- }else{
- return false;
- }
- }
-
-
-
-
-
-
- function addCache($tabName, $sql, $data){
- $key=md5($sql);
-
- if($this->addKey($tabName, $key)){
- $this->mc->set($key, $data, MEMCACHE_COMPRESSED, 0);
- }
- }
-
-
-
-
-
- function getCache($sql){
- $key=md5($sql);
- return $this->mc->get($key);
- }
-
-
-
-
-
-
- function delCache($tabName){
- $keys=$this->mc->get($tabName);
-
- if(!emptyempty($keys)){
- foreach($keys as $key){
- $this->mc->delete($key, 0);
- }
- }
-
- $this->mc->delete($tabName, 0);
- }
-
-
-
-
- function delone($sql){
- $key=md5($sql);
- $this->mc->delete($key, 0);
- }
- }
- ?>
|