PHP uses memcached simple example sharing_PHP tutorial
Sharing a simple example of using memcached in PHP
On many occasions, we will hear the name memcached, but many students have only heard of it and have not used it or actually understood it. I know it is a very good thing. Here is a brief introduction: memcached is an efficient and fast distributed memory object caching system, mainly used to accelerate WEB dynamic applications. Today we will briefly discuss the usage of memcached
1. Add expansion pack
The code is as follows:
php_memcache.dll
2. Add
in PHP.INI
The code is as follows:
extension=php_memcache.dll
3. Procedure
The code is as follows:
//Create a mem object instance
$mem=new Memcache;
if(!$mem->connect("10.18.110.213",11211)){
die('Connection failed!');
}
//Add
//1. Add a string
/* if($mem->set('key1',"beijing",MEMCACHE_COMPRESSED,60)){
echo 'Add ok';
}*/
//2. Add value
/* if($mem->set('key1',100,MEMCACHE_COMPRESSED,60)){
echo 'Add ok';
}*/
//3.Add array
//When adding the array, as needed. I hope the serial number will be put into ,
//serialize<=>unserialize, if needed, you can also json_encode <=> json_decode
$arr=array("bj",'tj');
if($mem->set('key1',$arr,MEMCACHE_COMPRESSED,time()+31*3600*24)){
echo 'Add array ok99111';
}
//4. Add object
/* class Dog{
public $name;
public $age;
public function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
}
$dog1=new Dog('puppy',50);
if($mem->set('key1',$dog1,MEMCACHE_COMPRESSED,60)){
echo 'Add object ok';
}*/
//5.Add null Boolean value
/* if($mem->set('key1',false,MEMCACHE_COMPRESSED,60)){
echo 'Add boolean ok';
}*/
//6. Put the resource type.
/* $con=mysql_connect("127.0.0.1","root","root");
if(!$con){
die('Failed to connect to database');
}
var_dump($con);
echo "
";
if($mem->set('key1',$con,MEMCACHE_COMPRESSED,60)){
echo 'Add resource ok';
}*/
//Query
$val=$mem->get('key1');
//Modify
//You can use replace
if($mem->replace("key11",'hello',MEMCACHE_COMPRESSED,60)){
echo 'replace ok';
}else{
echo 'replace no ok';
}
//Delete
echo "
";
if($mem->delete('key14')){
echo 'key14 delete';
}else{
echo 'key14 does not exist';
}
The above is the introduction of this article on the use of memcache in php. I hope it can be helpful to everyone.

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

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

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

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

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
