search
Homephp教程php手册PHP中memcached缓存应用基础实例

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载,它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度.

1.添加扩展包

php_memcache.dll

2.在PHP.INI添加如下代码:

extension=php_memcache.dll

3.程序代码如下:

<?php 
	//创建一个mem对象实例 
	$mem=new Memcache; 
	 
	if(!$mem->connect("10.18.110.213",11211)){ 
	    die(&#39;连接失败!&#39;);  
	} 
	 
	//增加 
	 
	//1.增加一个字串 
	/*    if($mem->set(&#39;key1&#39;,"beijing",MEMCACHE_COMPRESSED,60)){ 
	     
	    echo &#39;添加ok&#39;; 
	}*/ 
	 
	//2.添加数值 
	/*    if($mem->set(&#39;key1&#39;,100,MEMCACHE_COMPRESSED,60)){ 
	     
	    echo &#39;添加ok&#39;; 
	}*/ 
	 
	//3.添加数组 
	//在添加数组是,根据需要. 希望序列号放入  , 
	//serialize<=>unserialize, 如果根据需要,也可以json_encode <=> json_decode 
	$arr=array("bj",&#39;tj&#39;); 
	if($mem->set(&#39;key1&#39;,$arr,MEMCACHE_COMPRESSED,time()+31*3600*24)){ 
	     
	    echo &#39;添加数组ok99111&#39;; 
	} 
	//4.添加对象 
	/*    class Dog{ 
	    public $name; 
	    public $age; 
	    public function __construct($name,$age){ 
	        $this->name=$name; 
	        $this->age=$age; 
	    } 
	} 
	 
	$dog1=new Dog(&#39;小狗&#39;,50); 
	if($mem->set(&#39;key1&#39;,$dog1,MEMCACHE_COMPRESSED,60)){ 
	     
	    echo &#39;添加对象ok&#39;; 
	}*/ 
	 
	//5.添加null 布尔值 
	/*    if($mem->set(&#39;key1&#39;,false,MEMCACHE_COMPRESSED,60)){ 
	     
	    echo &#39;添加布尔ok&#39;; 
	}*/ 
	 
	//6. 资源类型放入. 
	/*    $con=mysql_connect("127.0.0.1","root","root"); 
	if(!$con){ 
	    die(&#39;连接数据库失败&#39;); 
	} 
	var_dump($con); 
	echo "<br/>"; 
	if($mem->set(&#39;key1&#39;,$con,MEMCACHE_COMPRESSED,60)){ 
	     
	    echo &#39;添加资源ok&#39;; 
	}*/ 
	 
	 
	//查询 
	 
	$val=$mem->get(&#39;key1&#39;); 
	//修改 
	//可以使用replace 
	if($mem->replace("key11",&#39;hello&#39;,MEMCACHE_COMPRESSED,60)){ 
	    echo &#39;replace ok&#39;; 
	}else{ 
	    echo &#39;replace no ok&#39;; 
	} 
	 
	 
	 
	//删除 
	echo "<br/>"; 
	if($mem->delete(&#39;key14&#39;)){ 
	    echo &#39;key14 删除&#39;; 
	}else{ 
	    echo &#39;key14不存在&#39;; 
	} 
	 

本文讲的是基础应用,如果需要更深入的学习,以后我们会出相应的教程,也可以下手册学习.

教程地址:

欢迎转载!但请带上文章地址^^

Statement
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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use