Home >Backend Development >PHP Tutorial >Simple use of memcache under PHP
1. First download memcached, there are many on Baidu, download the corresponding one according to your corresponding system
2. Unzip it to any local place, remember the location, here I unzipped it to the memcached folder on the D drive .
3. Open the terminal, which is the dos command console (press win+r under windows, enter cmd, and hit enter). At this time, the familiar dos interface will appear. First enter the disk with the remembered location above and take a look (I unzipped it in D:memcached, so I typed "D:" and hit Enter, and then checked in dir to see if there is a corresponding folder) as shown in the picture
As you can see, there is this folder, then we will start the installation
4. Enter this folder ("cd memcached"), then enter "memcached.exe -d install" and press Enter. After a slight pause, the installation will be completed. , as shown in the figure.
5. Now to start the memcache service, enter "memcached.exe -d start" and press Enter. After a slight pause, it will start.
6. Check whether this service is started, or press Enter with win + r, enter "services.msc", and then you will enter the service window. Look inside to see if you have seen the memcached service. This It starts automatically at boot.
The installation is over, now let’s start using it
1.memcached The client is installed, but our PHP still can’t operate it, so we need to add the memcache extension to PHP (memcached’s driver is divided into memcached and memcache Two extensions (this is also the answer I got when I asked my colleagues). I use the integrated development environment of phpStudy, which has various drivers built in. If you use wamp server, you can search and download it online and put it in the ext folder under the PHP directory. Next, add an "extension=memcache.dll" item to the php.ini configuration file and restart "apache". Print "phpinfo()" Take a look, have you seen memcache?
2. Then you can use it. As mentioned above, there are two extensions. You can search in the php manual according to your corresponding extension. Wrong use.
3. Paste one of my demos.
demo1.php
<?php $memcache = new Memcache(); $memcache->connect('localhost') or die("error"); //链接memcache 端口默认是11211,可写可不写 $memcache->set('key','777',MEMCACHE_COMPRESSED,10); //这里设置的过期时间是10秒
<?php $memcache = new Memcache(); $memcache->connect('localhost') or die("error"); echo "<pre class="brush:php;toolbar:false">"; var_dump($memcache->get('key'));
Through the settings of demo1.php, it can also be accessed in demo2.php. Is it successful? There are many examples on the Internet, so I won’t repeat them one by one here. Also, when linking to the memcache service, there is a number 11211 after it, which is the default listening port of memcache.
Happy New Year to everyone!
The above introduces the simple use of memcache in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.