memcached-logo

 

In part I of this blog we saw how to install and test Memcached.
In this part, we will see how to use this amazing software in command line.

memcached-tool

We can use the default script memcached-tool

/usr/share/memcached/scripts/memcached-tool 
Usage: memcached-tool <host[:port] | /path/to/socket> [mode]
       memcached-tool 10.0.0.5:11211 display    # shows slabs
       memcached-tool 10.0.0.5:11211            # same.  (default is display)
       memcached-tool 10.0.0.5:11211 stats      # shows general stats
       memcached-tool 10.0.0.5:11211 dump       # dumps keys and values

display

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 display
  #  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM
  3     152B        11s       1       1      no        0        0    0

stats

/usr/share/memcached/scripts/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         146
              bytes_read        9363
           bytes_written       22531
              cas_badval           0
                cas_hit's           0
              cas_misses           0
               cmd_flush           0
                 cmd_get          92
                 cmd_set          82
               cmd_touch           0
             conn_yields           0
   connection_structures           7
        curr_connections           5
              curr_items           1
               decr_hit's           0
             decr_misses           0
             delete_hit's           1
           delete_misses           0
       evicted_unfetched           0
               evictions           0
       expired_unfetched           0
                get_hit's          87
              get_misses           5
              hash_bytes      524288
       hash_is_expanding           0
        hash_power_level          16
               incr_hit's           0
             incr_misses           0
                libevent 2.0.19-stable
          limit_maxbytes    67108864
     listen_disabled_num           0
                     pid       23989
            pointer_size          64
               reclaimed          17
            reserved_fds          20
           rusage_system   74.119732
             rusage_user   94.034704
                 threads           4
                    time  1433928279
       total_connections          52
             total_items          82
              touch_hit's           0
            touch_misses           0
                  uptime     2940441
                 version      1.4.13

dump

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 dump
Dumping memcache contents
  Number of buckets: 1
  Number of items  : 1
Dumping bucket 3 - 1 total items
add key3 1 1430987838 76
O:8:"stdClass":2:{s:8:"str_attr";s:13:"never_expires";s:8:"int_attr";i:333;}

Making a connection with telnet

To make a connection to Memcached, use the following command:

[[email protected]]$ telnet localhost 11211
 Trying 127.0.0.1...
 Connected to localhost.
 Escape character is '^]'.

Accessing statistics

Stats command allows you to have an overview showing important Memcached statistics:

stats
STAT pid 2004
STAT uptime 14043
STAT time 1479306474
STAT version 1.4.13
STAT libevent 2.0.19-stable
STAT pointer_size 64
STAT rusage_user 1.046840
STAT rusage_system 0.649901
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hit's 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hit's 0
STAT incr_misses 0
STAT incr_hit's 0
STAT decr_misses 0
STAT decr_hit's 0
STAT cas_misses 0
STAT cas_hit's 0
STAT cas_badval 0
STAT touch_hit's 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END

We can see some important informations :Some useful information is returned, such as :

  • the server uptime,
  • the server version,
  • the total number of items,
  • the current client connections to the server.
Related concept:   How to list running vhosts on Apache

Accessing slabs

Slabs are a chunks of memory allocated by Memcached for internal use. This mecanism enhances the optimization of memory use.

To list the slabs, use the stats slabs command:

stats slabs
 STAT 1:chunk_size 88
 STAT 1:chunks_per_page 11915
 STAT 1:total_pages 1
 STAT 1:total_chunks 11915
 STAT 1:used_chunks 11915
 STAT 1:free_chunks 0
 STAT 1:free_chunks_end 11914
 STAT 6:chunk_size 296
 STAT 6:chunks_per_page 3542
 STAT 6:total_pages 1
 STAT 6:total_chunks 3542
 STAT 6:used_chunks 3541
 STAT 6:free_chunks 1
 STAT 6:free_chunks_end 3541
 STAT 7:chunk_size 376
 STAT 7:chunks_per_page 2788
 STAT 7:total_pages 1
 STAT 7:total_chunks 2788
 STAT 7:used_chunks 2788
 STAT 7:free_chunks 0
 STAT 7:free_chunks_end 2787
 STAT 8:chunk_size 472
 STAT 8:chunks_per_page 2221
 STAT 8:total_pages 1
 STAT 8:total_chunks 2221
 STAT 8:used_chunks 2220
 STAT 8:free_chunks 1
 STAT 8:free_chunks_end 2218
 STAT active_slabs 4
 STAT total_malloced 4193552
 END

Each slabs contains a list of items. To have these items you can use:

stats items
STAT items:3:number 3
STAT items:3:age 38
STAT items:3:evicted 0
STAT items:3:evicted_nonzero 0
STAT items:3:evicted_time 0
STAT items:3:outofmemory 0
STAT items:3:tailrepairs 0
STAT items:3:reclaimed 0
STAT items:3:expired_unfetched 0
STAT items:3:evicted_unfetched 0
END

How to manipulate data ?

Setting key:

set <key> <flags> <expire-time> <bytes> <value>

Example:

set mykey 0 1000 4 myValue
STORED

Getting key:

get mykey
VALUE mykey 0 4 myValue
END

Based on the php script used in part I we can use key3 which never expire:

get key3
VALUE key3 1 76
O:8:"stdClass":2:{s:8:"str_attr";s:13:"never_expires";s:8:"int_attr";i:333;}
END

Deleting key:

delete key3
 DELETED

Hope this blog helped you to understand memcached which is really very powerful and widely used by big companies like Facebook or Twitter. Feel free to read our other blogs and give us your feedbacks. Till next time.

Related concept:   Linux TOP command explained