memcached

Posted on 2008-07-25 16:31 Love 阅读(19) 评论(0)  编辑 收藏 网摘

 

Example code

Converting a database or object creation queries to use memcached is simple. Typically, when using straight database queries, example code would be as follows:

function get_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) {
result = db_select("SELECT * FROM users WHERE userid = ?", userid);
memcached_add("userrow:" + userid,  result);
}
return result;
}

The server 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 be also needed, 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);
}
}
Tag标签: fackbook,memcached

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:


相关搜索:
fackbook memcached

相关链接:
 

posts - 11, comments - 6, trackbacks - 0, articles - 0

Copyright © Love