1.21. PHP connects to Memcached service

发布时间 :2025-10-25 12:24:30 UTC      

In the previous section, we have shown how to install the Memcached service,and then we will show you how PHP uses the Memcached service.

1.21.1. PHP Memcache extension installation

PHP Memcache expansion pack download address: http://pecl.php.net/package/memcache , you can download the latest stable pack (stable).

wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

Note: /usr/local/php/ for php the installation path needs to be adjusted according to the actual directory of your installation.

You will be displayed after the installation is successful memcache.so Expanded location, such as mine:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

Finally, we need to add this extension to the php , open yours php.ini add the following at the end of the file:

[Memcache]
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = memcache.so

Restart after adding php I am using the nginx+php-fpm process, so the command is as follows:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

If it is apache use the following command:

/usr/local/apache2/bin/apachectl restart

Check the installation result

/usr/local/php/bin/php -m | grep memcache

If the installation is successful, it will output: memcache .

Or access it through a browser phpinfo() function, as shown in the following figure:

image0

1.21.2. PHP connection Memcached

<?php
$memcache = new Memcache;             //Create a memcache object
$memcache->connect('localhost', 11211) or die ("Could not connect"); //Connect to Memcached server
$memcache->set('key', 'test');        //Set a variable to memory with the name key and the value test
$get_value = $memcache->get('key');   //Retrieve the value of the key from memory
echo $get_value;
?>

For more PHP operations Memcached, please see: http://php.net/manual/zh/book.memcache.php

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.