Memcached

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

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools