[+/-]
libmemcachedA number of interfaces from different languages exist for interacting with memcached servers and storing and retrieving information. Interfaces for the most common language platforms including Perl, PHP, Python, Ruby, C and Java.
Data stored into a memcached server is referred to by a single string (the key), with storage into the cache and retrieval from the cache using the key as the reference. The cache therefore operates like a large associative array or hash. It is not possible to structure or otherwise organize the information stored in the cache. If you want to store information in a structured way, you must use 'formatted' keys.
The following tips may be useful to you when using memcached:
The general sequence for using memcached in any language as a caching solution is as follows:
Request the item from the cache.
If the item exists, use the item data.
If the item does not exist, load the data from MySQL, and store the value into the cache. This means the value will be available to the next client that requests it from the cache.
For a flow diagram of this sequence, see Figure 14.8, “Typical memcached Application Flowchart”.
The interface to memcached supports the following methods for storing and retrieving information in the cache, and these are consistent across all the different APIs, even though the language specific mechanics may be different:
          get(key) — retrieves information
          from the cache. Returns the value if it exists, or
          NULL, nil, or
          undefined or the closest equivalent in the
          corresponding language, if the specified key does not exist.
        
          set(key, value [, expiry]) — sets
          the key in the cache to the specified value. Note that this
          will either update an existing key if it already exists, or
          add a new key/value pair if the key doesn't exist. If the
          expiry time is specified, then the key will expire (be
          deleted) when the expiry time is reached. The time should be
          specified in seconds, and is taken as a relative time if the
          value is less than 30 days (30*24*60*60), or an absolute time
          (epoch) if larger than this value.
        
          add(key, value [, expiry]) — adds
          the key to the cache, if the specified key doesn't already
          exist.
        
          replace(key, value [, expiry]) —
          replace the value of the specified
          key, only if the key already exists.
        
          delete(key [, time]) — Deletes the
          key from the cache. If you supply a
          time, then adding a value with the
          specified key is blocked for the specified
          period.
        
          incr(key [, value]) — Increment the
          specified key by one or the specified
          value.
        
          decr(key [, value]) — Decrement the
          specified key by one or the specified
          value.
        
          flush_all — invalidates (or
          expires) all the current items in the cache. Technically they
          will still exist (they are not deleted), but they will be
          silently destroyed the next time you try to access them.
        
In all implementations, most or all of these functions are duplicated through the corresponding native language interface.
For all languages and interfaces, you should use memcached to store full items, rather than simply caching single rows of information from the database. For example, when displaying a record about an object (invoice, user history, or blog post), all the data for the associated entry should be loaded from the database, and compiled into the internal structure that would normally be required by the application. You then save the complete object into the cache.
      Data cannot be stored directly, it needs to be serialized, and
      most interfaces will serialize the data for you. Perl uses
      Storable, PHP uses
      serialize, Python uses
      cPickle (or Pickle) and Java
      uses the Serializable interface. In most cases,
      the serialization interface used is customizable. If you want to
      share data stored in memcached instances
      between different language interfaces, consider using a common
      serialization solution such as JSON (Javascript Object Notation).
    

User Comments
Add your own comment.