Home Backend Development PHP Tutorial PHP之分布式缓存memcached熟习和操作

PHP之分布式缓存memcached熟习和操作

Jun 13, 2016 pm 12:16 PM
memcache memcached quot

PHP之分布式缓存memcached熟悉和操作

如今互联网崛起的时代,各大网站都面临着一个大数据流问题,怎么提高网站访问速度,减少对数据库的操作;作为PHP开发人员,我们一般能想到的方法有页面静态化处理、防盗链、CDN内容分发加速访问、mysql数据库优化建立索引、架设apache服务器集群()、还有就是现在流行的各种分布式缓存技术:如memcached/redis;


1.什么是Memcached?

a.Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

b.Memcached的键key一般是字符串,该值不能重复;value可以放入字符串、数组、数值、对象、布尔,二进制数据和图片视频

c.Memcached默认服务端口是11211

2.PHP使用Memcached步骤

准备:下载Memcached服务安装包:memcached-1.2.6-win32-bin.7z和访问Memcached服务的dll库:php_memcache.dll

www.memcached.org(官网进不去好像,可以从其他地方下载)

解压包memcached-1.2.6-win32-bin.7z(可以解压完复制放到web服务器同级目录),然后操作cmd,进入到刚才解压的目录用命令安装:memcached.exe -d install

安装完(判断是否安装完毕可以到服务列表里面查看是否有memcached服务),然后cmd用命令启动:memcached.exe -d start

具体操作如下:



启动完memcached服务后,再把下载的php_memcache.dll放到web服务器php5目录下的ext目录下


在php.ini里面修改,加载扩展库php_memcache.dll,然后重启apache服务器


开始实践,memcached主要有crud操作(即创建、读取、更新、删除值操作,具体可以查阅手册),下面弄个简单的设置值,然后读取值的操作

a.设置值页面

<?phpheader ("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache();  //连接Memcache服务器if(!$mem->connect("127.0.0.1")) {    echo "连接Memcache服务器失败!";}//设置,'myword'参数代表键key,'hello world'代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){    echo "设置值成功!";}?>
Copy after login


注:如果值在内存存放的时间要超过30天,要用时间戳来设置100天:如time()+3600*24*100;设置0则表示永不过期


b.读取值页面

<?phpheader ("Content-type:text/html;charset=utf-8");$mem = new Memcache();  if(!$mem->connect("127.0.0.1")) {    echo "连接Memcache服务器失败!";}//读取键myword值$value = $mem->get('myword');if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}
Copy after login

c.删除、更新例子:

<?phpheader ("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache();  //连接Memcache服务器if(!$mem->connect("127.0.0.1")) {    echo "连接Memcache服务器失败!";}//设置,'myword'参数代表键key,'hello world'代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){    echo "设置值成功!";}//读取键myword值$value = $mem->get('myword');if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}//更新键值$mem->replace('myword','hello everybody!');$value = $mem->get('myword');if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}//删除键myword值$mem->delete('myword');$value = $mem->get('myword');if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}//关闭$mem->close();  ?>
Copy after login

注:mem对象下还有许多方法,可以通过翻阅手册了解。

多个memcached服务器设置,其实就比一个memcached服务器改变一点点,就是把多个memcached的服务器通过方法addserver添加到连接池中,这样设置完后,crud操作时,内部就会通过相应算法均衡连接相应服务器并执行相应操作中。

<?phpheader ("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache(); //添加多台memcached服务器$mem->addserver('192.168.0.1',11211); $mem->addserver('192.168.0.2',11211);$mem->addserver('192.168.0.3',11211);$mem->addserver('192.168.0.4',11211);//设置,'myword'参数代表键key,'hello world'代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set('myword','hello world',MEMCACHE_COMPRESSED,50)){    echo "设置值成功!";}//读取键myword值$value = $mem->get('myword');if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}?>
Copy after login

memcache的访问是无用户状态,安全性需要考虑,一般通过放在内网,并通过防火墙限制外网访问memcache端口来达到安全

通过修改php.ini,可以把session的值放入memcache服务器中

session.save_handler = files改成session.save_handler = memcached

 session.save_path = "N;MODE;/path"改成 session.save_path = "tcp://127.0.0.1:11211"






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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks 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)

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 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

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 Memcache for efficient data writing and querying in PHP development? How to use Memcache for efficient data writing and querying in PHP development? Nov 07, 2023 pm 01:36 PM

How to use Memcache for efficient data writing and querying in PHP development? With the continuous development of Internet applications, the requirements for system performance are getting higher and higher. In PHP development, in order to improve system performance and response speed, we often use various caching technologies. One of the commonly used caching technologies is Memcache. Memcache is a high-performance distributed memory object caching system that can be used to cache database query results, page fragments, session data, etc. By storing data in memory

How to use Memcache for distributed caching in PHP development? How to use Memcache for distributed caching in PHP development? Nov 07, 2023 pm 03:04 PM

As web applications become increasingly complex, performance has become a critical issue. In many applications, database queries are one of the most time-consuming operations. In order to avoid frequently reading data from the database, a caching system can be used to store frequently read data in memory for quick access. In PHP development, using Memcached for distributed caching is an extremely common practice. In this article we will introduce how to use Memcached for distributed caching. What is Memca

How to use Memcache to optimize data storage operations in your PHP application? How to use Memcache to optimize data storage operations in your PHP application? Nov 08, 2023 pm 09:06 PM

How to use Memcache to optimize data storage operations in your PHP application? In web application development, data storage is a crucial link. In PHP applications, Memcache, as a memory cache system, can effectively improve the efficiency of data storage and reading operations. This article will introduce how to use Memcache to optimize data storage operations in PHP applications, and attach specific code examples. Step 1: Install the Memcache extension First, you need to install Me in your PHP environment

How to use Memcache to achieve efficient data caching and sorting operations in PHP development? How to use Memcache to achieve efficient data caching and sorting operations in PHP development? Nov 07, 2023 pm 02:28 PM

PHP is a very popular programming language commonly used for server-side web application development. As the user scale of web applications continues to grow and the amount of data continues to increase, efficient data caching and sorting operations become more and more important. Memcache is a very useful tool in this situation. This article will introduce how to use Memcache to achieve efficient data caching and sorting operations in PHP development, and provide specific code examples. What is Memcache? Memcache is

Use Pagoda Panel to deploy cache servers such as Redis and Memcached Use Pagoda Panel to deploy cache servers such as Redis and Memcached Jun 21, 2023 am 09:56 AM

With the development of the Internet, caching technology plays an increasingly important role in Web development. Redis and Memcached, two popular cache servers, are widely used in various web application development. However, for developers who are not familiar with Linux systems, installing and configuring these cache servers can cause some trouble. However, with the help of Pagoda Panel, this process will become quite simple. 1. What is a pagoda panel? Pagoda panel is a Linux server management panel that can

See all articles