What is Memcached

W

Memcached is an open source distributed caching system that can be used on Windows, MacOS, Linux, and Unix, with a key-value-based architecture. The key cannot exceed 255 characters and the value is limited to 1 MB.

All information is stored on servers in the RAM. We should not forget that the speed of reading in virtual memory is much faster. For example, reading takes place in RAM at 800 MB/s while a hard disk can only achieve speeds up to 100 MB/s. Some companies prefer to store all information in RAM and not on HDD for this reason. Obviously, the backup data is stored on HDDs because they are permanent storage procedures, but actual requests occur in RAM.

Installing and configuring Memcached is quite simple. For use in a typical scenario (Linux, Apache, MySQL, PHP server) two software packages are required:

1. Memcached (Memcached service)

2. PHP-pecl-Memcache (the PHP extension that facilitates communication with the Memcached service).

Installation:

yum install memcached php-pecl-memcache

Configuration:

For a CentOS server, the configuration file is located at /etc/sysconfig/Memcached

PORT = “11111”
USER = “memcached”
MAXCONN = “1024”
Cachesize = “512”
OPTIONS = “- l 127.0.0.1”

Starting service:

To automatically start the service when loading the operating system:

chkconfig Memcached on
service Memcached start

If you want to start the service as a daemon, do this by using the command:
Memcached -d -m 512 -u Memcached -l 127.0.0.1

View saved items

memcached-tool 127.0.0.1:11211

Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
 1 96B 4426s 1 2 no 0 0 0
21 8.7K 3675s 1 2 no 0 0 0
29 51.7K 3963s 1 1 no 0 0 0
33 126.3K 3893s 1 1 no 0 0 0

View statistics

memcached-tool 127.0.0.1:11211 stats

# 127.0.0.1: 11211 Field Value

         accepting_conns 1
               auth_cmds 0
             auth_errors 0
                   bytes 8601
              bytes_read 8560
           bytes_written 2810
              cas_badval 0
                cas_hits 0
              cas_misses 0
               cmd_flush 0
                 cmd_get 4
                 cmd_set 3
             conn_yields 0
   connection_structures 14
        curr_connections 12
              curr_items 3
               decr_hits 0
             decr_misses 0
             delete_hits 0
           delete_misses 0
               evictions 0
                get_hits 2
              get_misses 2
               incr_hits 0
             incr_misses 0
          limit_maxbytes 134217728
     listen_disabled_num 0
                     pid 13095
            pointer_size 64
           rusage_system 0.056991
             rusage_user 0.023996
                 threads 4
                    time 1440147170
       total_connections 890
             total_items 3
                  uptime 2273
                 version 1.4.4

Use:

memcache = new Memcached ();

memcache_available = $ memcache-> connect (‘127.0.0.1’, 11211);

$ query = “SELECT FROM FROM WHERE first_name = ‘john’ AND last_name = ‘Doe’;”;
$ mc_key = md5 ($ query);
$ mc_result = $ memcache-> get ($ mc_key);
if ($ mc_result) {
    $ first_name = $ mc_result [‘first_name’];
    $ last_name = $ mc_result [‘last_name’];
}
else {
    // Run the query in the database
    $ result = mysql_query ($ query);
    $ row = mysql_fetch_array ($ result);
    $ memcache-> set ($ mc_key, $ row, false, 60 * 60); // stores the result for one hour (3600 sec)
}
To check if the Memcache class is installed in PHP, before creating the $ memcache object, you can use the sequence:
if (class_exists ( ‘Memcache’)) {
    // Memcache is enabled.
}

Conclusion

It’s not difficult to implement Memcached in your system, but you have to think carefully about this topic as you can not cache things that are not there. Typically, it is intended to cache information that is difficult to update or if it makes frequent requests such as a database.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories