Table of Contents
1. Memcache concept
2. Memcached server installation and testing
3. Installation and testing of memcache
4. Installation and testing of memcached
Home Backend Development PHP Tutorial How to configure memcache in php

How to configure memcache in php

Jun 11, 2018 pm 01:39 PM
memcache memcached

This article mainly introduces how to configure memcache in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. Memcache concept

First of all We must distinguish three concepts: Memcached, memcache and memcached.
1. Memcached refers to the Memcached server, which is an independently running Memcached background server. Just like mysqld, it is a key-value pair used to store data. "Database".
2. Memcached and memcache are both Memcached clients, and you can access and connect to the Memcached server through them. They are both PHP plug-ins. The difference between the two is an old topic, you can refer to Other articles say. I personally feel that memcached is better. After all, it has many functions and is based on the extension of libmemcached.

2. Memcached server installation and testing

If it is fedora, there are built-in sources memcached, you can install it directly with yum

yum -y install memcached.x86_64
Copy after login

After installation, start the background process.

 memcached -d -m 10 -u user -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
Copy after login

Among them, -d means to start the background service process, -m means the maximum use of 10m memory, -u means the current User, -l represents the ip address, -p represents the port number, -c represents the maximum number of concurrent connections, and -P represents the location of the pid file. Other parameters can be directly queried by man memcached.

Now you can use telnet to test Next, check the opening status of memcached.

telnet 127.0.0.1 12000Trying 127.0.0.1...Connected to 127.0.0.1.
Escape character is '^]'.
set a 0 0 41234STORED
get a
VALUE a 0 41234END
^C
quit
Copy after login

The above message indicates that the Memcached server is installed successfully.

3. Installation and testing of memcache

wget -c http://pecl.php.net/get/memcache-3.0.8.tgztar zxvf memcache-3.0.8.tgz
phpize && ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-configmake && make install
Copy after login

In this way, a line of Installing shared extensions will appear below: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
The so file is generated, and then just import this module in the php.ini file.

extension_dir=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/extension=memcache.so
Copy after login

Then restart php-fpm to load php.ini, check phpinfo. If you see memcached, it means the module installation is successful. Sometimes php.ini may not be loaded. You can force php-fpm to load the ini file.

php-fpm -c /usr/local/php/lib/php.ini
Copy after login

Test memcached in the php file.

<?php
$memcache = memcache_connect(&#39;localhost&#39;, 12000);
if ($memcache) {    
$memcache->set("key1", "String");    
$memcache->set("key2", 123);    
$object = new StdClass;   
$object->attribute = &#39;test&#39;;    
$memcache->set(&#39;key3&#39;, $object);

    var_dump($memcache->get(&#39;key1&#39;));
    var_dump($memcache->get(&#39;key2&#39;));
    var_dump($memcache->get(&#39;key3&#39;));
} else {    
echo "Connection to memcached failed";
}
?>
Copy after login

You can know whether the access is successful by accessing it through the browser.

There is a file called memcache.php in memcache, you can check the operation of the memcache server Status.
Modify the memcache.php file.

#$MEMCACHE_SERVERS[] = &#39;mymemcache-server1:11211&#39;; // add more as an array
#$MEMCACHE_SERVERS[] = &#39;mymemcache-server2:11211&#39;; // add more as an array
$MEMCACHE_SERVERS[] = &#39;localhost:12000&#39;;
Copy after login

You can see the status of the memcached server when you open the browser.

4. Installation and testing of memcached

Installation It’s similar to memcache, but you need to install libmemcached first.

yum -y install libmemcached.x86_64
Copy after login

Then just follow the installation and testing methods of memcache.

The above is the entire content of this article, I hope it will be helpful to everyone’s study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

About recursive analysis in php

The above is the detailed content of How to configure memcache in php. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
How to use Memcache in PHP development? How to use Memcache in PHP development? Nov 07, 2023 pm 12:49 PM

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to optimize PHP application CPU usage using Memcached caching technology? How to optimize PHP application CPU usage using Memcached caching technology? Jun 21, 2023 pm 05:07 PM

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

How to use Memcache for efficient data writing and querying in PHP development? How to use Memcache for efficient data writing and querying in PHP development? Nov 07, 2023 pm 01:36 PM

How to use Memcache for efficient data writing and querying in PHP development? With the continuous development of Internet applications, the requirements for system performance are getting higher and higher. In PHP development, in order to improve system performance and response speed, we often use various caching technologies. One of the commonly used caching technologies is Memcache. Memcache is a high-performance distributed memory object caching system that can be used to cache database query results, page fragments, session data, etc. By storing data in memory

How to use Memcache for efficient data reading and writing operations in PHP development? How to use Memcache for efficient data reading and writing operations in PHP development? Nov 07, 2023 pm 03:48 PM

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

How to use Memcache for distributed caching in PHP development? How to use Memcache for distributed caching in PHP development? Nov 07, 2023 pm 03:04 PM

As web applications become increasingly complex, performance has become a critical issue. In many applications, database queries are one of the most time-consuming operations. In order to avoid frequently reading data from the database, a caching system can be used to store frequently read data in memory for quick access. In PHP development, using Memcached for distributed caching is an extremely common practice. In this article we will introduce how to use Memcached for distributed caching. What is Memca

How to use Memcache to optimize data storage operations in your PHP application? How to use Memcache to optimize data storage operations in your PHP application? Nov 08, 2023 pm 09:06 PM

How to use Memcache to optimize data storage operations in your PHP application? In web application development, data storage is a crucial link. In PHP applications, Memcache, as a memory cache system, can effectively improve the efficiency of data storage and reading operations. This article will introduce how to use Memcache to optimize data storage operations in PHP applications, and attach specific code examples. Step 1: Install the Memcache extension First, you need to install Me in your PHP environment

Use Pagoda Panel to deploy cache servers such as Redis and Memcached Use Pagoda Panel to deploy cache servers such as Redis and Memcached Jun 21, 2023 am 09:56 AM

With the development of the Internet, caching technology plays an increasingly important role in Web development. Redis and Memcached, two popular cache servers, are widely used in various web application development. However, for developers who are not familiar with Linux systems, installing and configuring these cache servers can cause some trouble. However, with the help of Pagoda Panel, this process will become quite simple. 1. What is a pagoda panel? Pagoda panel is a Linux server management panel that can

How to use Memcache to achieve efficient data caching and sorting operations in PHP development? How to use Memcache to achieve efficient data caching and sorting operations in PHP development? Nov 07, 2023 pm 02:28 PM

PHP is a very popular programming language commonly used for server-side web application development. As the user scale of web applications continues to grow and the amount of data continues to increase, efficient data caching and sorting operations become more and more important. Memcache is a very useful tool in this situation. This article will introduce how to use Memcache to achieve efficient data caching and sorting operations in PHP development, and provide specific code examples. What is Memcache? Memcache is

See all articles