dqef7931 2010-11-21 15:17
浏览 43

Memcached为具有大量选择的小型数据库带来好处

I am building a web app in php with mysql as database.

I am using memcached to store sessions and I am thinking if I should also store (in another memcached instance), database records.

The db is going to be pretty small and updated once in a while. There are going to be a lot of selects though, as we expect high traffic.

For a small database would I benefit if I store records in memcached or should I leave it to the mysql cache and tune the db for lots of connections (would appreciate some pointers for optimizing mysql)?

Thanks

  • 写回答

1条回答 默认 最新

  • dqnf28092 2010-11-21 15:55
    关注

    Why dont you run some benchmarks and then spread that load !!

    http://www.php.net/manual/en/intro.apc.php

    Page generated in 0.003202 secs using 2 DB calls and 0 DBCache hits (users ttl=5 countries ttl=5 and both items have expired)
    
    Page generated in 0.002728 secs using 1 DB calls and 1 DBCache hits (users ttl=5 countries ttl=immortal and users has expired)
    
    Page generated in 0.000067 secs using 0 DB calls and 2 DBCache hits (users ttl=5 countries ttl=immortal and both are fetched from cache)
    
    how fast - 0.000067 ouch !! 
    

    Sample PHP Script using APC

    <?php
    
    require_once "CacheDB.php";
    
    ob_start(); 
    
    echo "<h3>APC info:</h3>";
    
    print_r(apc_sma_info());
    
    try{
    
        //I assume you've already have a db connection available for the cacheDB to use
    
        $db = @new mysqli("127.0.0.1","foo_dbo","pass","foo_db",3306);
    
        if ($db->connect_errno)
            throw new Exception("Could not connect: " . $db->connect_error);
    
        //start the demo...
    
        $startTime = microtime(true);
    
        $cacheDB = new CacheDB($db);
    
        $rows = $cacheDB->Query("call list_users()", CacheDB::TTL_5); //5 second Time To Live (TTL) (30 secs might be more realistic)
        if($rows){
            echo "<h3>Users:</h3><ul>";
            foreach($rows as $row) echo sprintf("<li>%s</li>", $row["username"]);
            echo "</ul>";
        }
    
        $rows = $cacheDB->Query("call list_countries()", CacheDB::TTL_IMMORTAL); //never expires
    
        if($rows){
            echo "<h3>Countries:</h3><ul>";
            foreach($rows as $row) echo sprintf("<li>%s</li>", $row["name"]);
            echo "</ul>";
        }
    
        echo sprintf("<p><b>Page generated in %s secs using %d DB calls and %d DBCache hits</b></p><p>Refresh me !!</p>",
                number_format(microtime(true) - $startTime, 6, ".", ""), 
                $cacheDB->GetDBHits(), $cacheDB->GetCacheHits());
    
        $db->close();
    }
    catch(Exception $ex)
    { 
        ob_clean(); 
        echo sprintf("zomg borked - %s", $ex->getMessage());
    }
    
    //finally
    
    ob_end_flush();
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集