Home > Backend Development > PHP Tutorial > [Original] Memcached development under linux_PHP tutorial

[Original] Memcached development under linux_PHP tutorial

WBOY
Release: 2016-07-13 10:38:02
Original
778 people have browsed it

PS: I will open it for the company soon because the company is about to close down.

memcached This thing is a very good thing. As a PHPer, I don’t know how to use it. If you are among the PHP masters. So there is this chapter

Configured under your own LINUX memcached , follow the online configuration to access the problem They are not big. The installation tutorials for memcached on the Internet are quite correct.

But you should pay attention to a few points according to your own environment.

No. One is that before installing memcached you must first compile and install the libevent package. There is a link to this. Baidu

The second is to remember when compiling the libevent package./ configure --prefix=/usr Compile to the /usr directory.

Because there is a lib directory under the usr directory. All of them are LIB libraries such as xx.so and xx.so.1.

After installing libevent, start the installation of memcached. Not very good.

What is talked about online is to change the extension dir address of php.ini. In fact, as long as we compile and generate memcached.so, we can set it up later. Just put it in the directory where so is stored.

The above points are what I got during the installation. You can figure it out yourself. In fact, the installation is quite simple.Then there are Use
memcached

. There are two ways to use

memcached. The first one is to use pecl package memcached module. But this method requires compilation. The second method is to use the memcached-client .php file. This speed is certainly not good enough. So I chose The first method.Usememcached It’s actually quite simple. But it depends on the specific environment you use. It may be complicated.

Now I will try the methods in pecl's memcached one by one. Haha. Anyway, I am almost out of work. I have a lot of time...

1Memcache::add methodThis method is to add variables to the memory. It’s cool to see the example



If the variable already exists in memory. Then it will return false. I only gave the basic moves. I used them flexibly.


Memcached:: set method[Original] Memcached development under linux_PHP tutorial
Set the value of the variable in memory

The parameters are the same as Memcached::add

1 is the flag of the variable in memory 2 is the value of the variable 3 is the compression flag 4 is the number of seconds the variable survives in memory

Look at the example. I can’t upload the picture. It’s too big. I can only write it by hand

$mem = new Memcache;
$mem->connect('localhost ',11211) or die("not connect");
!$mem->set('mykey','good job'') && die("not input memory");
echo($ mem->get('mykey')); //Before compression

!$mem->set('mykey','good job',MEMCACHE_COMPRESSED,50) && die("not input memory");
echo($mem->get('mykey')); // Compress and set the survival time of variables in memory
?>
Is compression useful? If we Store a large variable in memory in bytes. Of course, it takes up less memory after compression.

?>

3 As you can see from the name, this method obtains variables from memory. We have saved variables before, remember. Haha

The method of use is very simple
See:
$mem=new Memcache;
$mem->connect('localhost',11211) or die("not connect");
if(!$mem->get('kesdy')) {
echo 'xx';
}

?>
If the variable to be obtained has not been stored in memory before or has expired, it will return empty.

4
Memcache::connect This method is not used Let’s talk. All the above examples are useful.
The first parameter is: The IP address of the memcache server. The second parameter is: The listening port of memcache. The default is 11211

Memcache ::pconnect Needless to say this.. Haha Long connection
5
Memcache :: replace() method

You can tell by the name of this method. It replaces the values ​​and parameters of variables that already exist in the memory.

If the variable It already exists in the memory but if we want to change its value, it is best not to use the Memcache::set () method. In this case, replace() is faster and better in terms of efficiency


Serve

$mem = new Memcache;
$mem->connect('localhost',11211);
$mem->set('mykey ','xxeedd');
$mem->replace('mykey','dddddddddddddd');
echo($mem->get('mykey'));
?>

The above is the output dddddddddddddddddd

It can be seen that it has been replaced. Haha

6

Memcache::increment method

This It is valid for memory variables whose values ​​are numeric values.

$mem = new Memcache;
$mem->connect('localhost',11211);
$mem->set('mykey',5);
$mem->increment('mykey',2); //In this case, the value of mykey will be automatically increased by 2 to become 7.
echo ($mem->get('mykey'));
?>


Memcache::decrement method is just the opposite.


7 Memcache::delete() method

This method is to manually delete a variable in memory.

$mem = new Memcache;
$mem->connect('localhost',11211);
$mem->set('mykey',5);
$mem->delete('mykey'); // To delete, you can also bring a parameter which is how many seconds to delete. $mem->delete('mykey',50);
echo($mem->get('mykey'));
?>

It’s simple enough. Take a look for yourself

8

Memcache::flush()

Make all variables stored in memory Invalid.

Usage is also very simple.

$mem = new Memcache;
$mem->connect('localhost',11211);
$mem->set('mykey',5);
$mem->flush();
?>



Well, that’s basically it. . Know how to use these things. memcached That’s basically it..

I’ll try it next memcached What is the effect of under multi-process and cluster .

I will explain it later...[Original] Memcached development under linux_PHP tutorial Simple comprehensive application of memcached and mysql.rar( 8.51 KB)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735129.htmlTechArticlePS: I will open it for the company soon because the company is about to close down. Memcached is a very good thing. Do it If you don’t know how to use PHPer, what if you become one of the php masters? So I have this chapter...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template