Home php教程 php手册 Memcache PHP简单教程

Memcache PHP简单教程

Jun 06, 2016 pm 08:07 PM
memcache php Tutorial Purpose Simple Views

近期项目的访问量较大,对数据库的读取较为频繁,服务器有点扛不住的趋势,使用缓存就是一个迫在眉睫的事情。 经过网上的各种遍历,最终使用了memcache作缓存。 由于网上关于memcache的教程甚少,在此把收集的相关资料和使用的心得整理一下。 关于Memcache

近期项目的访问量较大,对数据库的读取较为频繁,服务器有点扛不住的趋势,使用缓存就是一个迫在眉睫的事情。

经过网上的各种遍历,最终使用了memcache作缓存。

由于网上关于memcache的教程甚少,在此把收集的相关资料和使用的心得整理一下。


关于Memcache

我简单的理解为使用内存存储的数据库,好三俗。。。其他的自己google去。。。


安装Memcache

需要安装两个东西,一个是memcached主程序,一个是PHP扩展。

目前最新的Memcache是1.4.15,具体可以看官网:http://memcached.org/

具体安装安装方法:

英文版:https://code.google.com/p/memcached/wiki/NewInstallFromSource

中文版:http://www.ccvita.com/259.html

Win版:http://www.ccvita.com/258.html


PHP使用Memcache

<?php //连接  
$mem = new Memcache;  
$mem->connect("192.168.0.100", 12000);    
//保存数据  
$mem->set('key1', 'This is first value', 0, 3600); 
$val = $mem->get('key1');  
echo "Get key1 value: " . $val ."<br>";    
//保存数组  
$arr = array('aaa', 'bbb', 'ccc', 'ddd');  
$mem->set('key2', $arr, 0, 60);  
$val2 = $mem->get('key2');  
echo "Get key2 value: ";  
print_r($val2);  
echo "<br>";    
//删除数据  
$mem->delete('key1');  
$val = $mem->get('key1');  
echo "Get key1 value: " . $val . "<br>";    
//清除所有数据  
$mem->flush();  
$val2 = $mem->get('key2');  
echo "Get key2 value: ";  
print_r($val2);  
echo "<br>";    
//关闭连接  
$mem->close();  
?>
Copy after login

浏览器的输出结果为:

Get key1 value: This is first value

Get key2 value: Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd )

Get key1 value:

Get key2 value:


代码分析

$mem = new Memcache;
Copy after login

初始化一个Memcache的对象。

$mem->connect("192.168.0.100", 12000);
Copy after login

连接到Memcache服务器端,第一个参数是服务器的IP地址,也可以是主机名,第二个参数是Memcache的开放的端口。

跟Mysql一样,同样有pconnect($host, $port)的长连接方法,这个方法不能被close关闭。

$mem->set('key1', 'This is first value', 0, 3600);
Copy after login

保存一个数据到Memcache服务器上,第一个参数是数据的key,用来定位一个数据(索引),第二个参数是需要保存的数据内容,这里是一个字符串,第三个参数是一个标记,一般设置为0或者MEMCACHE_COMPRESSED(压缩),第四个参数是数据的有效期,就是说数据在这个时间内是有效的,如果过去这个时间,那么会被Memcache服务器端清除掉这个数据,单位是秒,如果设置为0,则是永远有效,我们这里设置了3600,就是一个小时。

同时我还发现还有add()和replace()方法,一个插数据一个修改数据,set是他们的综合版啊。

$val = $mem->get('key1');
Copy after login

从Memcache服务器端获取一条数据,它只有一个参数,就是需要获取数据的key,我们这里是上一步设置的key1。

$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60); 
Copy after login

同样的,Memcache也是可以保存数组的。

$mem->delete('key1');
Copy after login

同样的,删除也通过这个key来删除单条内容。

$mem->flush();
Copy after login

清除Memcache服务器上的数据,慎用喔,如果服务器上有别人的缓存的话。

关于Memcache的更多方法,请看这个:http://www.php.net/memcache

还有分布式应用先看着这篇吧。http://www.ccvita.com/395.html


Memadmin管理工具

在此推荐一个工具:memadmin

这是个对Memcache进行管理的程序,功能强大,界面美观,可以提升开发和debug效率喔,具体使用方法一看就懂,不做介绍啦。

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles