PHP calls MEMCACHE caching technology example
In projects, when large amounts of visits are involved, reasonable use of cache can reduce the pressure on the database and improve user experience. That is, under the premise of non-real-time requirements, within a short period of time (several seconds), the data used for display is obtained from the cache without directly reading the database, which can effectively reduce the reading pressure of the database. Here is a record of the use of memcache in PHP language:
First, we create a memcachepool, which can be read according to different configurations and generate different memcache instances. Use $memcache->addServer($host,$port,$flag); to add a memcache server to the connection pool. The code example is as follows:
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 服务器 * @param [type] $port 端口 * @param [type] $flag 控制是否使用持久化连接。默认TRUE * @return [type] */ public function getMemcache($host,$port,$flag){ if(isset($this->memcacheList[$host.$port])) return $this->memcacheList[$host.$port]; $memcache = new Memcache(); // 向连接池中添加一个memcache服务器 $memcache->addServer($host,$port,$flag); //开启大值自动压缩,第一个参数表示处理数据大小的临界点,第二个参数表示压缩的比例,默认为0.2 $memcache->setCompressThreshold(2000,0.2); $this->memcacheList[$host.$port] = $memcache; return $memcache; } }
Then implement a method class that contains common memcache methods such as add, set, get, flush, delete, etc., here named 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 到期的时间,如果此值设置为0表明此数据永不过期 * @param integer $flag 标志位 使用MEMCACHE_COMPRESSED指定对值进行压缩(使用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); } /** * 从服务端查找元素 * @param [type] $key * @return [type] */ public function get($key){ return $this->memcache->get($key); } /** * 增加一个条目到缓存服务器 * @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; } /** * 清洗(删除)已经存储的所有的元素 * @return [type] */ public function flush(){ return $this->memcache->flush(); } /** * 从服务端删除一个元素 * @param [type] delete 参数:key要删除的元素的key 删除该元素的执行时间 timeout如果值为0,则该元素立即删除。 * @return [type] */ public function delete($key){ $ret = $this->memcache->delete($key,0); return $ret; } }
Then call dlufmemcache:
1 $memcache = new dlufMemcache('127.0.0.1',11211); 2 $memcache->set('memcache','come on dluf&baidu !!!!!!'); 3 $ret = $memcache->get('memcache'); 4 echo print_r($ret,true);
The running output is visible:
Want to know more about PHP? Then hurry up and follow the PHP video tutorial on the PHP Chinese website!
The above is the detailed content of PHP calls MEMCACHE caching technology example. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
