From Academic Kids

The title of this article is incorrect because of technical limitations. The correct title is memcached.

memcached is a distributed memory caching system that was originally developed by Danga Interactive for LiveJournal. It is used to speed up dynamic database-driven websites by caching data and objects in memory to reduce the amount the database needs to be read. memcached is open source and released under a BSD license. It uses libevent.

Memcached lacks authentication and security features, meaning it should only be used on servers with a firewall set up appropriately.

MediaWiki is one example of software that supports memcached. Slashdot uses memcached in some parts of its site.

memcached acts as a giant hash table, caching data as it is requested. Originally designed to speed up only the queries themselves, memcached is now often used in object caching as well. Any task which is time intensive can be improved by using memcached to store the data, rather than regenerate it every time, so long as the cached data has either a definite expiry time or a process through which the cached data can be marked as deletable.

Converting your database or object creation queries to use memcached is simple. Typically, if you are using straight database queries, you might have code that looks like the following:

function foo (int userid) {
   result = db_select("SELECT * FROM users WHERE userid = " + userid);
   return result;

After conversion to memcached, the same call might look like the following:

function get_foo (int userid) {
    result = memcached_fetch("userrow:" + userid);
    if (result) {
        return result;
    result = db_select("SELECT * FROM users WHERE userid = " + userid);
    memcached_add("userrow:" + userid,  result);
    return result;

As you can see, you would first check whether a memcached value with the unique key "userrow:userid" exists, where userid is some number. If the memcached_fetch call returns the data, you immediately return - and never need to touch the database or slow disks. If the result does not exist, you would select from the database as usual, and set the unique key using the memcached API add function call.

However, if you only modified this API call, and no others, you would eventually end up fetching incorrect data (assuming your entire database doesn't fit into the memory caching you have available). In addition to creating a "add" call, you would also need to implement an update call, using the memcached set function.

function update_foo(int userid, string dbUpdateString) {
    result = db_execute(dbUpdateString);
    if (result) {
        data = createUserDataFromDBString(dbUpdateString);
        memcached_set("userrow:"+userid, data);

This call would update the currently cached data to match the new data in the database, assuming the database query succeeds.

Note that all functions described on this page are pseudocode only. Memcached calls and programming languages may vary based on the API you use.

External links


Academic Kids Menu

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools