Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Memcached
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Example code== ''Note that all functions described on this page are [[pseudocode]] only. Memcached calls and programming languages may vary based on the API used.'' Converting database or object creation queries to use Memcached is simple. Typically, when using straight database queries, example code would be as follows: <syntaxhighlight lang="c"> function get_foo(int userid) data = db_select("SELECT * FROM users WHERE userid = ?", userid) return data </syntaxhighlight> After conversion to Memcached, the same call might look like the following <syntaxhighlight lang="c"> function get_foo(int userid) /* first try the cache */ data = memcached_fetch("userrow:" + userid) if not data /* not found : request database */ data = db_select("SELECT * FROM users WHERE userid = ?", userid) /* then store in cache until next get */ memcached_add("userrow:" + userid, data) end return data </syntaxhighlight> The client would first check whether a Memcached value with the unique key "userrow:userid" exists, where userid is some number. If the result does not exist, it would select from the database as usual, and set the unique key using the Memcached API add function call. However, if only this API call were modified, the server would end up fetching incorrect data following any database update actions: the Memcached "view" of the data would become out of date. Therefore, in addition to creating an "add" call, an update call would also be needed using the Memcached set function. <syntaxhighlight lang="c"> function update_foo(int userid, string dbUpdateString) /* first update database */ result = db_execute(dbUpdateString) if result /* database update successful : fetch data to be stored in cache */ data = db_select("SELECT * FROM users WHERE userid = ?", userid) /* the previous line could also look like data = createDataFromDBString(dbUpdateString) */ /* then store in cache until next get */ memcached_set("userrow:" + userid, data) </syntaxhighlight> This call would update the currently cached data to match the new data in the database, assuming the database query succeeds. An alternative approach would be to invalidate the cache with the Memcached delete function, so that subsequent fetches result in a cache miss. Similar action would need to be taken when database records were deleted, to maintain either a correct or incomplete cache. An alternate cache-invalidation strategy is to store a random number in an agreed-upon cache entry and to incorporate this number into all keys that are used to store a particular kind of entry. To invalidate all such entries at once, change the random number. Existing entries (which were stored using the old number) will no longer be referenced and so will eventually expire or be recycled. <syntaxhighlight lang="c"> function store_xyz_entry(int key, string value) /* Retrieve the random number - use zero if none exists yet. * The key-name used here is arbitrary. */ seed = memcached_fetch(":xyz_seed:") if not seed seed = 0 /* Build the key used to store the entry and store it. * The key-name used here is also arbitrary. Notice that the "seed" and the user's "key" * are stored as separate parts of the constructed hashKey string: ":xyz_data:(seed):(key)." * This is not mandatory, but is recommended. */ string hashKey = sprintf(":xyz_data:%d:%d", seed, key) memcached_set(hashKey, value) /* "fetch_entry," not shown, follows identical logic to the above. */ function invalidate_xyz_cache() existing_seed = memcached_fetch(":xyz_seed:") /* Coin a different random seed */ do seed = rand() until seed != existing_seed /* Now store it in the agreed-upon place. All future requests will use this number. * Therefore, all existing entries become un-referenced and will eventually expire. */ memcached_set(":xyz_seed:", seed) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)