Home Backend Development PHP Tutorial Memcache/Memcached PHP operation manual (pure manuscript version) memcached java memcached official website redis memcached comparison

Memcache/Memcached PHP operation manual (pure manuscript version) memcached java memcached official website redis memcached comparison

Jul 29, 2016 am 08:52 AM
memcached

data-id="1190000005001849" data-licence="">

Memcache and Memcached are actually the same thing, but if the extensions used in php are different, one is php_memcache.dll and php_memcached.dll. Around 2009, someone enriched the usage of memcache. and performance, memcached has been introduced, and its usage has been greatly improved, such as adding getMulti() to obtain key values ​​in batches


Memcache class

<code>$memcache = new Memcache;
</code>
Copy after login
<code>$memcache->connect('127.0.0.1', 11211);
</code>
Copy after login
<code>$memcache->pconnec('127.0.0.1', 11211); 
// 打开一个到服务器的持久化连接 , 连接不会在脚本执行结束后或者close()被调用后关闭
</code>
Copy after login
<code>$memcache->addServer('123.57.210.55', 11211,$persistent,$weight); 
// 向连接池中添加一个memcache服务器 $persistent 是否持久化连接 $weight 
   控制桶的数量提升被选中的权重 $timeout 表示连续持续时间
</code>
Copy after login
<code>$memcache->set('name', 'TK'); 
// 默认存储不压缩 不过期 , 其中字符串和数值直接存储,其他类型序列化后存储 
   set其实是add方法和replace方法集合
</code>
Copy after login
<code>$memcache->set('email', 'julylovin@163.com',MEMCACHE_COMPRESSED,5);
// MEMCACHE_COMPRESSED设置存储是否压缩 , 5表示5秒后过期但是最大只能设置2592000秒(30天)
   如果设置为0 表示永不过期, 可以设置将来的时间戳
</code>
Copy after login
<code>$memcache->set('info',array('age'=>'26','salary'=>'1000'));  
// 可以直接存储数组,redis中存储需要手动serialize()序列化
</code>
Copy after login
<code>$memcache->add('counter', '10', MEMCACHE_COMPRESSED, 0); 
//如果键值存在会返回false , 如果不存在, 和set方法一样,生成一个counter的key并赋值10
</code>
Copy after login
<code>$memcache->replace ('counter', '10');
 //如果键值不存在会返回false , 如果存在, 替换counter的值为10
</code>
Copy after login
<code>$memcache->increment('counter', 3); 
// 首先将元素当前值转换成数值然后减去value 操作counter键值+3 
   若键不存在 则返回false 不能用于压缩的键值操作,否则get键会失败
</code>
Copy after login
<code>$memcache->decrement('counter', 3); // 操作counter键值-3 , 若键不存在 则返回false
</code>
Copy after login
<code>$memcache->delete('counter', 3); // 操作删除键counter , 3表示3秒内删除,默认是0即立即删除
</code>
Copy after login
<code>$memcache->flush(); //flush()立即使所有已经存在的元素失效
</code>
Copy after login
<code>$memcache->getExtendedStats (); 
// 返回一个二维关联数据的服务器统计信息。数组的key由host:port方式组成
</code>
Copy after login
<code>$memcache->getServerStatus ('127.0.0.1'); // 获取返回一个服务器的在线/离线状态  0表示离线 非0在线
</code>
Copy after login
<code>$memcache->getStats(); // 获取服务器统计信息
</code>
Copy after login
<code>$memcache->getVersion(); // 返回服务器版本信息
</code>
Copy after login
<code>$memcache->setCompressThreshold ($threshold, $min_saving); 
//  开启大值自动压缩   $threshold设置压缩阀值 2000字节 ,即字节数大于2K 就压缩
    $min_saving  0--1之间  0.2表示压缩20%
</code>
Copy after login
<code>$memcache->setServerParams('memcache_host', 11211, 1, 15, true, '_callback_memcache_failure'); 
// $memcache->addServer('memcache_host', 11211, false, 1, 1, -1, false);
   已经通过addServer 配置过服务器 使用setServerParams 重新设置配置信息
</code>
Copy after login
rrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeerrreeer

The above introduces the PHP operation manual of Memcache/Memcached (pure manuscript version), including the content of Memcached. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap

Video Face Swap

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

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)

Memcached caching technology optimizes Session processing in PHP Memcached caching technology optimizes Session processing in PHP May 16, 2023 am 08:41 AM

Memcached is a commonly used caching technology that can greatly improve the performance of web applications. In PHP, the commonly used Session processing method is to store the Session file on the server's hard disk. However, this method is not optimal because the server's hard disk will become one of the performance bottlenecks. The use of Memcached caching technology can optimize Session processing in PHP and improve the performance of Web applications. Session in PHP

Caching library in PHP8.0: Memcached Caching library in PHP8.0: Memcached May 14, 2023 am 08:16 AM

Caching library in PHP8.0: Memcached With the rapid development of the Internet, modern applications require efficient and reliable caching technology to improve performance and handle large amounts of data. Due to PHP's popularity and open source nature, the PHP caching library has become an essential tool in the web development community. Memcached is a widely used open source high-speed memory caching system that can handle millions of simultaneous connected cache requests and can be used in many different types of applications, such as social networks, online

How to optimize PHP application CPU usage using Memcached caching technology? How to optimize PHP application CPU usage using Memcached caching technology? Jun 21, 2023 pm 05:07 PM

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

PHP and Memcached database backup and recovery PHP and Memcached database backup and recovery May 15, 2023 pm 09:12 PM

With the rapid development of the Internet, large-scale MySQL database backup and recovery has become one of the essential skills for major enterprises and websites. With the widespread application of Memcached, how to back up and restore Memcached has also become an important issue. As one of the main languages ​​for web development, PHP has unique advantages and skills in handling backup and recovery of MySQL and Memcached. This article will introduce in detail the implementation method of PHP processing MySQL and Memcached backup and recovery.

PHP and Memcached performance monitoring PHP and Memcached performance monitoring May 15, 2023 pm 09:51 PM

With the rapid development of modern Internet applications, user experience is crucial to the success of an application. How to ensure high performance and high availability of applications has become one of the important issues that developers need to solve. As one of the widely used programming languages, PHP's performance monitoring and optimization are also very important. Memcached is a high-performance, distributed memory object caching system that can help applications improve performance and scalability. This article will introduce how to use PHP and Memcached to implement performance monitoring.

Cache management with PHP and Memcached Cache management with PHP and Memcached May 23, 2023 pm 02:21 PM

With the continuous increase of network applications and the continuous expansion of data volume, data reading and writing efficiency has become one of the important factors affecting application performance. The application of caching technology can solve this problem well. In PHP applications, Memcached is the most commonly used cache server. Memcached is a high-performance distributed memory object caching system that can store commonly used data in memory and improve the efficiency of data retrieval. This article will introduce how to use PHP and Memcached for cache management, and how to optimize

How to use Memcached with CakePHP? How to use Memcached with CakePHP? Jun 04, 2023 am 08:14 AM

With the rapid growth of modern applications, caching has become a vital part of many developers. Caching can greatly improve application performance and reduce server load. In CakePHP, one way to implement caching is to use Memcached. Memcached is a memory-based distributed caching system. It stores data in memory and can read and write data quickly. In a multi-server environment, Memcached can store data in a distributed manner and share it over the network. not only can

Use Memcached caching technology to optimize audio and video playback in PHP Use Memcached caching technology to optimize audio and video playback in PHP May 17, 2023 pm 04:01 PM

With the continuous development of Internet technology, audio and video resources have become a very important form of content on the Internet, and PHP, as one of the most widely used languages ​​in network development, is also constantly used in the field of video and audio playback. However, with the increasing number of users of audio and video websites, many websites have discovered a problem: under high concurrency conditions, PHP's processing speed of audio and video slows down significantly, resulting in problems such as inability to play in time or stuck playback. To solve this problem, Memcached caching technology should

See all articles