这篇文章主要介绍了PHP内存缓存Memcached类,以实例形式分析了PHP内存缓存Memcached的实现方法,是php操作memcached的典型应用,非常具有实用价值,需要的朋友可以参考下
具体实现方法如下:
代码如下:
<?PHP class MemcacheModel { private $mc = null; /** * 构造方法,用于添加服务器并创建memcahced对象 */ function __construct(){ $params = func_get_args(); $mc = new Memcache; //如果有多个memcache服务器 if( count($params) > 1){ foreach ($params as $v){ call_user_func_array(array($mc, 'addServer'), $v); } //如果只有一个memcache服务器 } else { call_user_func_array(array($mc, 'addServer'), $params[0]); } $this->mc=$mc; } /** * 获取memcached对象 * @return object memcached对象 */ function getMem(){ return $this->mc; } /** * 检查mem是否连接成功 * @return bool 连接成功返回true,否则返回false */ 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(); } //如果key不存在,就添加一个 if(!in_array($key, $keys)) { $keys[]=$key; //将新的key添加到本表的keys中 $this->mc->set($tabName, $keys, MEMCACHE_COMPRESSED, 0); return true; //不存在返回true }else{ return false; //存在返回false } } /** * 向memcache中添加数据 * @param string $tabName 需要缓存数据表的表名 * @param string $sql 使用sql作为memcache的key * @param mixed $data 需要缓存的数据 */ function addCache($tabName, $sql, $data){ $key=md5($sql); //如果不存在 if($this->addKey($tabName, $key)){ $this->mc->set($key, $data, MEMCACHE_COMPRESSED, 0); } } /** * 获取memcahce中保存的数据 * @param string $sql 使用SQL的key * @return mixed 返回缓存中的数据 */ function getCache($sql){ $key=md5($sql); return $this->mc->get($key); } /** * 删除和同一个表相关的所有缓存 * @param string $tabName 数据表的表名 */ function delCache($tabName){ $keys=$this->mc->get($tabName); //删除同一个表的所有缓存 if(!emptyempty($keys)){ foreach($keys as $key){ $this->mc->delete($key, 0); //0 表示立刻删除 } } //删除表的所有sql的key $this->mc->delete($tabName, 0); } /** * 删除单独一个语句的缓存 * @param string $sql 执行的SQL语句 */ function delone($sql){ $key=md5($sql); $this->mc->delete($key, 0); //0 表示立刻删除 } } ?>
【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通
3. bootstrap教程

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool