Today we will deal with an interesting data caching mechanism: Memcache php... The great thing about memcahe is that we can cache anything from SQL queries to objects and any other data structures.

What is Memcache

Memcache Is not an easy caching technology or algorithm, first of all it is a server program running on a web server. If you use hosting services, then before using the memcahe in the application, you need to make sure that memcahe is available. This can be done using the phpinfo () function.

Concept

After making sure that the memcahe server is available for use, let's get acquainted with the concept of this caching mechanism. The most attractive thing about using memcahe php is that all cached data is stored in random access memory server. This principle of data storage not only saves processor time on generating them again and again, but also greatly increases the speed of access to them.

In a simplified version, the concept of memcahe can be summarized as follows: frequently used php objects are cached and stored in the server's RAM as a set of key-object pairs; if necessary, the previously saved objects are taken from the server's memory bypassing numerous connections to the database checks and cycles. If the memcahe php algorithm does not find the requested object in memory, the required object is created in the usual way and stored in the cache.

How to work with Memcache server in PHP

To work with the Memcache server, we need one of the PHP libraries php-memcache or php-memcached.

The choice of a particular library depends on the individual needs of the developer, for the most part, they are very similar to each other, with the difference that php-memcached provides several great opportunities for work such as:

  • CAS tokens for key versioning
  • Callbacks
  • GetDelayed () method to reduce waiting time by postponing the actual reading of keys
  • Binary protocol support
  • Ability to avoid serialization using igbinary

Both modules php-memcach and php-memcached are not standard php but are available in the PECL repository.

If you work not on your own server, but on a hosting server, you will not have to choose, everything will be already configured.

Example of work

I would like to draw your attention to the fact that this example will work in the same way for php-memcach and php-memcached.

Memcach php uses the following methods to work with data:

  1. get - to get an object from the cache;
  2. set - writing an object by key to the cache;
  3. add - write an object to the cache, if it does not already have such a key;
  4. replace - writing an object to the cache, if the key already exists.

I will give a primitive example of using caching using the memcache server.

1
2
3
4
5
6
7

$ memcache = new Memcache ();
$ memcache -> pconnect ("localhost", 3211); // Connect. Any port can be used by default
$ key = "key_1";
$ value = array (1, 2, 3, 4, 5);
$ expire = 600;
$ memcache -> set ($ key, $ value, 0, $ expire); // Store the value for 10 minutes
$ memcache -> get ("key_1"); // get the previously written array

When using a memkesha server for several projects at once, remember that the data is written to one RAM. This means that if you write new values ​​for the same key from different projects, then the last value in memory will be available for both.

Today I will present you with a small piece of code that will teach you how to interact with Memcache. In order to install Mediatemple on a hosting, you need SSH data.

Who is using Memcache?

Memcache was developed by Danga Interactive to speed up LiveJournal.com. Memcache reduces the load time from the database to almost zero, which increases page load speed for users, faster resource management, etc. On the this moment FaceBook is the largest user of the Memcache service. Considering the number of people arriving on FaceBook daily, it nearly flunked this service- and a separate place was allocated for them.

What is Memcache?

Well, it became interesting? Memcache is a general purpose cached item distribution system. If there is nothing in the cache, then a query is made to the database and the results are written to Memcache:

Memcache provides 5 functions:

  • get () - retrieves a value by key
  • set () - sets a value
  • add () - adds a cache if it doesn't exist
  • replace () - replaces the cache
  • flush () - flushes all cached data

The code

Once you have Memcache installed, you can start using it. It is believed that a lot of cache is bad. I absolutely disagree with that! The faster your site loads, the better!

  • The first two lines connect to Memcache.
  • Next is the database connection script.
  • Then we create a key. When we want to put data into Memcache, we need to pass 3 elements for insertion: key, value and cache lifetime. The key is required to gain access to the data. In this example, you can see that I am using the query hash as the MD5 key.
  • Next, we check if the cache exists. The check will return us true or false. If it is, we get access to it.
  • If there is no cache, then we connect to the database and get the values. To create a cache, we use the following expression: $ memcache-> set ($ key, $ row, TRUE, 20); $ row stores an array of what we got from the database. 20 is the cache lifetime in seconds.

$ memcache = new Memcache; $ memcache-> connect ("127.0.0.1", 11211) or die ("Could not connect"); include ("includes / connect.php"); // create a key, then check the cache $ key = md5 ("SELECT * FROM memcached_test where name =" ashley ""); $ get_result = $ memcache> get ($ key); if ($ get_result) (echo $ get_result ["name"]; echo $ get_result ["username"]; echo "Data Pulled From Cache";) else (// Get data from the database and create a cache $ query = "SELECT * FROM memcached_test where name = "ashley"; "; $ result = mysql_query ($ query); $ row = mysql_fetch_array ($ result); print_r ($ row); $ memcache> set ($ key, $ row, TRUE, 20) ; // Store the result for 20 seconds echo "Data Pulled from the Database";)

This is the most basic example of using memcache, but I hope this was a good start for you.

This article is in the nature of an ambulance. A detailed description of the specific actions that allow you to install and configure memcached on the server.

Let's start with a definition.

Memcached - software, which implements a service for caching data in RAM based on a hash table.

Initial data:

  • VPS working on operating system Debian;
  • a valid site that can be accessed via FTP, SSH.

Let's start with the last point. Due to the running Internet resource (or its similarity), we can simply and quickly find out what we have installed (perhaps, among other things, we will find memcached). Let's create a file called info.php in which we write:

Let's execute the code by accessing http://example.com/info.php:

If, among other things, you found a similar entry, then everything is in order and the job has already been done. Otherwise, we will get this result based on the results of the actions described in this publication.

Installing Memcached Server

Let's start the installation with the command

Apt-get install memcached php5-memcache

We are waiting for the installation to complete. Then check the results with the command netstat

As a result, we get:

Tcp 0 0 *: 11211 *: * LISTEN 13053 / memcached

As we can see, by default memcached "listens" to port 11211 by default. Therefore, anyone can connect through this port and use our memcached server. Let's secure ourselves, give this right only to our server (localhost). Let's open the /etc/memcached.conf file through the control panel, or through the console:

Vi /etc/memcached.conf

This file contains detailed comments. Be sure to check them out.

Add the line -l 127.0.0.1.

Restart the memcached server with the command:

/etc/init.d/memcached restart

And let's check again:

Netstat -tap | grep memcached

As a result, we should get something like

Tcp 0 0 localhost.localdo: 11211 *: * LISTEN 13092 / memcached

Now memcached only works on the local server.

Installing memcache

List of commands (memcache version may differ!):

Apt-get install php5-dev libmemcache-dev pecl download memcache tar xzvf memcache-2.2.6.tgz cd memcache-2.2.6 / phpize && ./configure --enable-memcache && make cp modules / memcache.so / usr / lib / php5 / 20060613 / echo "extension = memcache.so" >> /etc/php5/apache2/php.ini

Restart Apache:

Apachectl graceful

Let's check the script we posted earlier at http://example.com/info.php. Now we need to find the Memcache item there.

Let's check the work of Memcached

Let's create a file result.php and run it.

connect ("localhost", 11211) or exit ("Unable to connect to Memcached server"); $ version = $ memcache-> getVersion (); echo "Server" s version: ". $ version."
\ n "; $ tmp_object = new stdClass; $ tmp_object-> str_attr =" test "; $ tmp_object-> int_attr = 123; $ memcache-> set (" key ", $ tmp_object, false, 10) or die (" Not it turned out to leave a record in Memcached "); echo" We write data to the Memcached cache (data will be stored for 10 seconds)
\ n "; $ get_result = $ memcache-> get (" key "); echo" Memcached data:
\ n "; var_dump ($ get_result);?>

The result of the script:

Server "s version: 1.4.13 Writing data to Memcached cache (data will be stored for 10 seconds) Data written to Memcached: object (stdClass) # 3 (2) ([" str_attr "] => string (4)" test " ["int_attr"] => int (123))

Is it so or almost so? Then it's all right. If not, try repeating the procedures.

The publication has already been made earlier with. Let's return to this topic and consider the practice of working with memcached by examples.

Unfortunately, I still don't get my hands on actively blogging, but finally the first person appeared who responded to. His name Vladislav Klimenko and it is he who is the author of this post, and I only act as an editor. Maybe this example will push other readers to participate in the return. Insight IT to life.

Yours faithfully,
Ivan Blinkov

So, a couple of words about the subject of the conversation. memcached is a distributed in-memory object caching system. Developed by the firm (by the way, they are the authors of not only memcached, but also other interesting projects). But about them, perhaps next time. Typically memcached is used by applications to temporarily store data that needs to be read frequently. Applications do not (usually) interact directly with the memcached server, but work using client libraries. To date, libraries have been created for many programming languages ​​(and for some also several alternative ones) - a complete list of client libraries is available at. In general, this scheme is similar to working with a database, familiar to many developers.

We will consider installing and using memcached for Linux. Also, when looking at examples in PHP and an overview of session caching, PHP and Apache are required. They may need to be installed, but we will not focus on installation issues.

Memcached server

Let's get down to installing memcached. In almost all Linux distributions, memcached can be installed from the repositories. If you want to collect the latest version, then you can look at (at the time of this writing, the latest version is). You may also need to install libevent. Latest stable version -

Build, install and run memcached in message display mode. It is interesting to see what happens to him:

The process starts and waits for connections (by default on port 11211). The server side is ready to handle client connections and cache the received data.

But for an app developer, this is only halfway there. You need to support memcached in your application. To do this, consider some of the existing memcached client libraries.

Memcached clients

From the whole variety of client libraries, we will consider two:

  • libmemcached (for C);
  • PECL extension for PHP (built on top of the previous library).

Si

The libmemcached library is currently under active development and seems to be the most appropriate choice when working with C and PHP. Also, bundled with the client library itself, additional utilities for working with memcached are supplied, allowing you to view, set, delete values ​​in the memcached cache. By the way, it is surprising that the set of utilities does not come with the server part, but with the client library.

So, let's start installing libmemcached. At the time of this writing, the current version of libmemcached is. We compile, install. First, enjoy reading the man pages:

man libmemcached man libmemcached_examples

The library comes with a description of simple examples of use. For more interesting ways of using it, it makes sense to look into the source codes of the utilities, since everything goes together.

  • memstat - displays information about the memcached server
  • memcat - returns value by key
  • memrm - deletes a value by key
  • memdump - Prints a list of keys

First, let's see what the memcached server, which we started a little earlier in the mode of issuing messages, has to say. Let's query the server statistics using the memstat utility:

memstat --servers localhost Listing 1 Server Server: localhost (11211) pid: 14534 uptime: 1950 time: 1247390264 version: 1.4.0 pointer_size: 32 rusage_user: 0.0 rusage_system: 0.0 curr_items: 0 total_items: 0 bytes: 0 curr_connections: 10 total_connections : 11 connection_structures: 11 cmd_get: 0 cmd_set: 0 get_hits: 0 get_misses: 0 evictions: 0 bytes_read: 0 bytes_written: 0 limit_maxbytes: 67108864 threads: 5

We got statistics - hence memcached is functioning and responding to requests.

So, at the moment, the memcached server and client library are ready to use. The only thing left to do is to implement the use of memcached in the developed application. As for the application, everything is in the hands of the developers, and we will consider a small example of working with basic functions.

memcached provides the following set of basic functions (there are, of course, more, but here are the main ones):

  • set- add a key-value pair to the cache
  • add- add a value to the cache, provided that there are no values ​​with such a key in the cache yet
  • replace- updates the cache, provided that there is already a value with such a key in the cache
  • get- gets a value from the cache by the specified key

Sample C Program

#include "stdio.h" #include "string.h" #include "memcached.h" int main (void) (char * key = "key"; char * value = "(! LANG: value" ; uint32_t flags = 0 ; size_t length = 0 ; char * value2 = NULL ; memcached_return rc ; !} // 1. create a structure for working with the cache memcached_st * memc = memcached_create (NULL); // 2. specify the server with which we will work memcached_server_add (memc, "localhost", 11211); // 3.cache the key-value pair rc = memcached_set (memc, key, strlen (key), value, strlen (value) + 1, (time_t) 0, flags); if (rc == MEMCACHED_SUCCESS) () else ( // handle the error } // 4. get the value value2 = memcached_get (memc, key, strlen (key), & length, & flags, & rc); if (rc == MEMCACHED_SUCCESS) (printf ("% s \ n", value2); free (value2);) else ( // handle the error } // 5.free structure memcached_free (memc); return 0; )

The program consists of 5 basic operations and does not need any special comments. Unless it can be noted that in paragraph 2, you can add many servers, in the case of using a distributed system.

We compile, you may have to explicitly specify the paths to the libraries:

gcc -Wall -o mc mc.c -I / usr / local / include / libmemcached / -lmemcached

Launch:

We see the required value - it should be earned!

To clarify the details, look at the messages on the memcached server:

<32 new auto-negotiating client connection 32: Client using the ascii protocol 32 STORED 32 sending key key >32 END<32 quit <32 connection closed.

In this example, the following events are presented: client connection, key-value pair setting, reading data by key, and client disconnection.

Let's see the statistics on the server:

memstat --servers localhost Listing 1 Server Server: localhost (11211) pid: 14534 uptime: 4659 time: 1247392973 version: 1.4.0 pointer_size: 32 rusage_user: 0.0 rusage_system: 0.0 curr_items: 1 total_items: 1 bytes: 58 curr_connections: 10 total_connections : 13 connection_structures: 11 cmd_get: 1 cmd_set: 1 get_hits: 1 get_misses: 0 evictions: 0 bytes_read: 58 bytes_written: 58 limit_maxbytes: 67108864 threads: 5

The next two lines show that a value has appeared in the cache:

curr_items: 1 total_items: 1

Let's look at this value:

memcat --servers localhost key value

So, the application using memcached is ready.

PHP

First, let's install the PECL extension for PHP - memcached

pecl install memcached

At this stage, an error message of the form may appear:

ERROR: "phpize" failed

This means that the php-dev package or its equivalent is not installed. Install it and you can try again:

pecl install memcached install ok: channel: //pecl.php.net/memcached-1.0.0 You should add "extension = memcached.so" to php.ini

As we are advised, add extension = memcached.so to php.ini and restart Apache.

See information about the PHP used:

memcached support enabled Version 1.0.0 libmemcached version 0.31 Session support yes igbinary support no

Sample PHP program

You can safely use calls to memcached from PHP. As usual, consider an example:

addServer ("localhost", 11211); $ m -> set ("phpkey", "phpvalue"); var_dump ($ m -> get ("phpkey")); ?>

The result of this script:

string (8) "phpvalue"

So the PHP application using memcached is ready.

Session data caching

Memcached can also be used as a session data store for PHP. This approach is often used in real applications... Let's see what needs to be done for this.

Making changes to php.ini

; session.save_handler = files session.save_handler = memcached ; session.save_path = / var / lib / php5 session.save_path = localhost: 11211

The session.save_handler parameter specifies that the data will now be stored in memcached. The second parameter, session.save_path, specifies the memcached server (several of them can be specified, separated by commas) on which the data will be saved.

Restart Apache and you're done!

Now we need to check that now session data is actually stored not on disk, but in memcached.

Consider the work of a simple script that writes something to the session:

We run the script, it enters data into the session, and then we look at the cache

memdump --servers localhost key keyphp memc.sess.key.3ff8ccab14424082ff83a6dfbcf0941f

So - to our familiar keys from the previous examples, a key with the characteristic name memc.sess.key has been added ..

Session data storage has been moved to the caching system. More detailed information you can read about working with memcached from PHP.

Conclusion

We have covered the installation and examples of using memcached. It should be emphasized that memcached is not a data storage system, so in practice memcached is almost always used in tandem with a database. Also, attention should be paid to the timely invalidation of data in the cache and security issues. In general, the topic is interesting, and still far from being closed.

There is a lot of information on the Internet for this topic, but despite this, many bypass it. The purpose of this post is to explain on your fingers the basics of interacting with Memcached.

What is Memcache and how does it relate to PHP?

Memcache is designed to cache data that is resource intensive to generate. This kind of data can contain anything from the results of a database query to a heavyweight chunk of template. Memcached is not included in the core set of modules that come with PHP, but it is available in the pecl repository.

Installation and configuration

I decided to use Debian as the distribution in question, because it is the most commonly used when building web servers. The Memcached PHP module is available in the repository already compiled (php5-memcached), but I will describe the installation process from source code, since not all repositories are as rich as Debian's.

Installing Memcached Server

# apt-get install memcached
For a start, the following config is enough for you:
# / etc / memcached.conf
#Memcached will work like a daemon
-d
# The log will be added there
logfile / var / log /
# Allocate 256 megabytes of RAM for storage
-m 256
# This port will listen
-p 11211
# Subsequently, it is desirable to change
-u nobody
# Listen to localhost
-l 127.0.0.1

# / etc / init.d / memcached restart
Checking
# netstat -tap | grep memcached
tcp 0 0 localhost: 11211 *: * LISTEN 13036 /

Compiling and installing the PHP module

apt-get install php5-dev libmemcache-dev

Pecl download memcache
tar xzvf memcache-2.2.6.tgz
cd memcache-2.2.6 /
phpize && ./ configure --enable-memcache && make
cp modules / / usr / lib / php5 / 20060613 /

echo "extension = memcache.so" >> / etc / php5 / apache2 / php.ini
/ etc / init.d / apache2 restart


That's all! It's not difficult at all.

Examples of using

1. Basic operations

  1. // Create a new object. You can also write in a procedural style
  2. = new;
  3. -> connect ("127.0.0.1", 11211) or die ("Could not connect");
  4. // Let's try to get an object with the key our_var
  5. $ var_key = @ -> get ("our_var");
  6. if (! empty ($ var_key))
  7. // If the object is cached, display its value
  8. echo $ var_key;
  9. else
  10. // If there is no object in the cache with the key our_var, create it
  11. // our_var object will be stored for 5 seconds and will not be compressed
  12. -> set ("our_var", date ("G: i: s"), false, 5);
  13. // Output the cached data
  14. echo -> get ("our_var");
  15. -> close ();

As a result of executing this code, the time will be displayed each time with an accuracy of seconds. However, it will be updated every 5 seconds until the cache is cleared. This example illustrates the simplest operations, but in performance we would rather lose than gain. After all, every time we have to connect to the server ...

2. We increase productivity

2.1 With caching
  1. < ? php
  2. function LoadCPU ()
  3. // Function that should load the processor
  4. $ image = imagecreate (800, 600);
  5. // White background color
  6. $ color = imagecolorallocate ($ image, 255, 255, 255);
  7. //The black
  8. $ color2 = imagecolorallocate ($ image, 0, 0, 0);
  9. for ($ i = 0; $ i< 10000 ; $i++ ) {
  10. imagesetpixel ($ image, rand (0, 800), rand (0, 600), $ color2);
  11. // Discard the pointer
  12. return $ image;
  13. // Create a new Memcache object
  14. = new;
  15. // Connect to our server
  16. -> connect ("127.0.0.1", 11211) or die ("Could not connect");
  17. // Let's try to get an object with the key image
  18. $ image_bin = -> get ("image");
  19. if (empty ($ image_bin)) (
  20. // If there is no picture in the cache, generate it and cache it
  21. imagepng (LoadCPU (), getcwd (). "/ tmp.png", 9);
  22. $ image_bin = file_get_contents (getcwd (). "/ tmp.png");
  23. unlink (getcwd (). "/ tmp.png");
  24. -> set ("image", $ image_bin, false, 30);
  25. // Display the image from the cache
  26. header ("Content-type: image / png");
  27. echo $ image_bin;
  28. // Close the connection to the Memcached server
  29. -> close ();
  30. ? >

This example shows a function that creates an 800x600 image and places 10,000 points on it. Once, having generated such an image, in the future we only display it on the screen without generating it again.
2.2 No caching
  1. function LoadCPU ()
  2. // Function that should load the processor
  3. // Create an 800x600 image
  4. $ image = imagecreate (800, 600);
  5. // White background color
  6. $ color = imagecolorallocate ($ image, 255, 255, 255);
  7. //The black
  8. $ color2 = imagecolorallocate ($ image, 0, 0, 0);
  9. for ($ i = 0; $ i< 10000 ; $i ++ ) {
  10. // Arrange 10,000 points in random order
  11. imagesetpixel ($ image, rand (0, 800), rand (0, 600), $ color2);
  12. // Discard the pointer
  13. return $ image;
  14. // Display the image without caching
  15. header ("Content-type: image / png");
  16. imagepng (LoadCPU (), "", 9);

Everything is much simpler and more familiar here: we generate the image again every time.
results
I have tested both scripts for performance. One and the same machine in the first case gave 460 responses per second, and in the second only 10. Which is to be expected.

Some more useful functions

addServer - if you have several caching servers at your disposal, you can create a cluster by adding servers to the pool. You should pay attention to the parameter weight... It indicates how much memory will be available to you on a particular server.
delete - from the name it is clear that this method deletes an object with a given key from the cache.
replace - replaces the value of the object with the given key. Use in case you need to change the contents of an object before its lifetime expires.

Outcome

From my point of view, it is worth using caching only on highly loaded resources. After all, every time you connect to the Memcached server, you are wasting precious time, which most likely will not be justified. As for large projects, it is better to write more lines of code at once than to do it afterwards, with the thought that your service is lying. Also, don't forget about wasting memory! Please note that by putting 300 megabytes in the cache, you took away 300 megabytes of RAM ...
In conclusion, I want to say that this article does not reveal all the charms of technology, but I hope that it stimulates you to improve yourself. Thanks for reading, dear% username%!

UPD: Another interesting point. Memcached, there is a PHP API for libmemcached. And Memcache, a php library that doesn't use libmemcached.