Home Backend Development PHP Tutorial 收拾php操作memcache缓存基础方法

收拾php操作memcache缓存基础方法

Jun 13, 2016 pm 12:21 PM
gt memcache return this

整理php操作memcache缓存基础方法
php操作memcache缓存方法分享


使用memcache的前提是需要在服务端先配置好memcahche的环境!确认memcahce可以正常连接之后就可以在程序使用了!


<?php /** * Memcache缓存操作 * @update http://www.lai18.com */class MCache extends Object implements CacheFace{  private $mem = null; //Mem对象     private $sId = 1;  //servier服务ID     /**   * 初始化Memcache   *   * @return Object   */  public function __construct()  {    if ( !class_exists(&#39;Memcache&#39;) )    {      throw new QException(&#39;PHP extension does not exist: Memcache&#39;);    }    $this->mem = new Memcache();  }     /**   * 链接memcahce服务   *   * @access private   * @param  string $key 关键字   * @param  string $value 缓存内容   * @return array   * @update http://www.lai18.com   */  private function connect( $sid )  {    $file = $this->CacheFile();    require $file;    if(! isset($cache) )    {      throw new QException('缓存配置文件不存在'.$file);    }    $server = $cache[$this->cacheId];    $sid  = isset($sid) == 0 ? $this->sId : $sid;//memcache服务选择    if ( ! $server[$sid])    {      throw new QException('当前操作的缓存服务器配置文件不存在');    }    $host = $server[$sid]['host'];    $port = $server[$sid]['port'];    try {      $this->mem->connect( $host , $port );    } catch (Exception $e) {      exit('memecache连接失败,错误信息:'. $e->getMessage());    }  }     /**   * 写入缓存   *   * @access private   * @param  string $key 关键字   * @param  string $value 缓存内容   * @return array   */  public function set( $key , $value , $sid , $expire = 0)  {    $data = $this->get($key , $sid); //如果已经存在key值    if( $data )     {      return $this->mem->set( $key , $value ,MEMCACHE_COMPRESSED , $expire);    } else {      return $this->mem->add( $key , $value ,MEMCACHE_COMPRESSED , $expire);    }  }     /**   * 读取缓存   *   * @access private   * @param  string $key 关键字   * @param  int   $sid 选择第几台memcache服务器   * @return array   */  public function get( $key , $sid)  {    $this->connect( $sid );    return $this->mem->get($key);  }     /**   * 清洗(删除)已经存储的所有的元素   *   * @access private   * @return array   */  public function flush()  {    $this->connect();    return $this->mem->flush();  }  /**   * 删除缓存   *   * @access private   * @param  string $key 关键字   * @param  int   $sid 选择第几台memcache服务器   * @return array   */  public function remove( $key , $sid)  {    $this->connect();    return $this->mem->delete($key);  }     /**   * 析构函数   * 最后关闭memcache   */  public function __destruct()  {    /*if(! $this->mem)    {      $this->mem->close();    }*/  }}
Copy after login


2楼u01448087036分钟前
江西某亿万富翁想杀我(欧衡),只为掌握了他重要把柄,n我向他求饶和发誓永不说出口,都无任何作用,而且反反复复恐吓我。
1楼u0112524026小时前
已收
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to use Memcache in PHP development? How to use Memcache in PHP development? Nov 07, 2023 pm 12:49 PM

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

Use the return keyword in JavaScript Use the return keyword in JavaScript Feb 18, 2024 pm 12:45 PM

Usage of return in JavaScript requires specific code examples In JavaScript, the return statement is used to specify the value returned from a function. Not only can it be used to end the execution of a function, it can also return a value to the place where the function was called. The return statement has the following common uses: Return a value The return statement can be used to return a value to the place where the function is called. Here is a simple example: functionadd(a,b){

How to use Memcache for efficient data reading and writing operations in PHP development? How to use Memcache for efficient data reading and writing operations in PHP development? Nov 07, 2023 pm 03:48 PM

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

How to use return value in Python How to use return value in Python Oct 07, 2023 am 11:10 AM

Python return value return usage is that when the function executes the return statement, execution will stop immediately and the specified value will be returned to the place where the function was called. Detailed usage: 1. Return a single value; 2. Return multiple values; 3. Return a null value; 4. End the execution of the function early.

See all articles