Memcached (o Memcache) incorpora un sistema de estadísticas interno que nos permite conocer el estado del demonio, su capacidad restante de almacenamiento, uso, conextiones, etcétera.

Para acceder a él a través del cliente de Ruby, por ejemplo, basta con cargar una consola en el entorno deseado e invocar al método stats del objeto de caché.

Por ejemplo, si nuestra configuración es la siguiente:


   CACHE = MemCache.new 'localhost:11211', :namespace => 'wadus_fragments'
 

Podemos utilizar el objeto CACHE de la siguiente manera:


   pp CACHE.stats
   {"bytes"=>746532339,
    "pid"=>10303,
    "connection_structures"=>19,
    "time"=>1200349556,
    "limit_maxbytes"=>1073741824,
    "cmd_get"=>689486,
    "version"=>"1.1.12",
    "bytes_written"=>3296488315,
    "cmd_set"=>265986,
    "get_misses"=>265168,
    "total_connections"=>1238,
    "curr_connections"=>17,
    "curr_items"=>66977,
    "uptime"=>56490,
    "get_hits"=>424318,
    "total_items"=>265986,
    "rusage_system"=>95.012555,
    "rusage_user"=>17.16339,
    "bytes_read"=>3226552602}
 

La interpretación de todos estos valores se puede encontrar en la definición del protocolo:

   Name              Type     Meaning
   ----------------------------------
   pid               32u      Process id of this server process
   uptime            32u      Number of seconds this server has been running
   time              32u      current UNIX time according to the server
   version           string   Version string of this server
   pointer_size      32       Default size of pointers on the host OS
                              (generally 32 or 64)
   rusage_user       32u:32u  Accumulated user time for this process 
                              (seconds:microseconds)
   rusage_system     32u:32u  Accumulated system time for this process 
                              (seconds:microseconds)
   curr_items        32u      Current number of items stored by the server
   total_items       32u      Total number of items stored by this server 
                              ever since it started
   bytes             64u      Current number of bytes used by this server 
                              to store items
   curr_connections  32u      Number of open connections
   total_connections 32u      Total number of connections opened since 
                              the server started running
   connection_structures 32u  Number of connection structures allocated 
                              by the server
   cmd_get           64u      Cumulative number of retrieval requests
   cmd_set           64u      Cumulative number of storage requests
   get_hits          64u      Number of keys that have been requested and 
                              found present
   get_misses        64u      Number of items that have been requested 
                              and not found
   evictions         64u      Number of valid items removed from cache                                                                           
                              to free memory for new items                                                                                       
   bytes_read        64u      Total number of bytes read by this server 
                              from network
   bytes_written     64u      Total number of bytes sent by this server to 
                              network
   limit_maxbytes    32u      Number of bytes this server is allowed to
                              use for storage. 
   threads           32u      Number of worker threads requested.
                              (see doc/threads.txt)
 

En concreto cuatro parámetros me han parecido interesantes de observar:

  • el parámetro bytes junto con el de limit_maxbytes: el primero representa el total de bytes ocupados actualmente, frente el máximo.
  • el número de fallos de caché que refleja el valor del parámetro get_misses junto con el número de aciertos get_hits

Los primeros nos permiten saber si hemos llegado al límite el almacenamiento que necesita nuestra aplicación y el segundo nos permite saber si nuestra política de caché es adecuada: diviendo hits entre misses obtenemos la proporción de fragmentos encontrados frente a fragmentos que ya no existían. Un valor de 1 indica que por cada dos búsquedas, una tiene éxito y otra no.

Por supuesto un valor inferior a uno muestra que la caché no sirve de nada (indicaría por ejemplo que necesitaríamos ampliar el Memcache o que tenemos una política de borrado "exagerada").