zoukankan      html  css  js  c++  java
  • memcached

    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);
    }
    }
    
  • 相关阅读:
    SpringMVC数据绑定
    SpringMVC概述
    应用Spring和Hibernate(C3P0数据池)写数据库交互项目
    Spring的AOP特性
    Sping框架的IOC特性
    MD5加密字符串
    重力感应 视频横竖屏切换
    自定义View(三)实现简单的可拖动、可缩放的ImageView
    自定义View(二)增加View的属性
    自定义View的学习(一) 自绘制控件
  • 原文地址:https://www.cnblogs.com/Love/p/1251470.html
Copyright © 2011-2022 走看看