PHP内存缓存Memcached类实例,phpmemcached
PHP内存缓存Memcached类实例,phpmemcached
本文实例讲述了PHP内存缓存Memcached类。分享给大家供大家参考。
具体实现方法如下:
复制代码 代码如下:
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 表示立刻删除
}
}
?>
希望本文所述对大家的PHP程序设计有所帮助。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
