Chinaunix首页 | 论坛 | 博客
  • 博客访问: 235514
  • 博文数量: 14
  • 博客积分: 2416
  • 博客等级: 大尉
  • 技术积分: 768
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-28 22:45
文章分类

全部博文(14)

文章存档

2011年(13)

2008年(1)

分类: 系统运维

2011-07-19 13:31:28

There are a lot of different ways that people use memcached and PHP. The most common of which is probably your basic set and get to cache data from your database.
----------------
有许多方式使用memcache和PHP, 其中最常见方式的可能是set get 是从数据库里取数据缓存起来

  1. function get_my_data1() {
  2.         $cache_id = "mykey";
  3.         $data = $memcache_obj->get($cache_id);
  4.         if ( !$data ) {
  5.             $data = get_data_from_db_function();
  6.             $memcache_obj->set($cache_id, $data, $sec_to_cache_for);
  7.         }
  8.         return $data;
  9.     }
But what if the query that’s going to hit the database is pretty intensive and you don’t want more than one user to hit the db at a time? That’s easily handling via a semaphore lock.
----------------
但是要访问数据库的查询非常密集 并且 你不想同时有多个用户命中数据库. 可通过信号锁轻松实现.
  1. function get_my_data2() {
  2.         $cache_id = "mykey";
  3.         $data = $memcache_obj->get($cache_id);
  4.         if ( !$data ) {
  5.             // check to see if someone has already set the lock
  6.             // 检测是否有人已经设置了锁
  7.             $data_lock = $memcache_obj->get($cache_id . ‘_qry_lock’);
  8.             if ( $data_lock ) {
  9.                 $lock_counter = 0;
  10.                 // loop until you find that the lock has been released. that implies that the query has finished
  11.                 // 循环 直到你发现锁已经被释放. 这意味着查询已经完成
  12.                 do while ( $data_lock ) {
  13.                     // you may only want to wait for a specified period of time.
  14.                     // 你也许需要在一个指定的周期内等待 //
  15.                     // one second is usually sufficient since your goal is to always have sub-second response time
  16.                     // if you query takes more than 1 second, you should consider "warming" your cached data via a cron job
  17.                     // 如果查询时间超过1s, 你应该创建一个计划任务 //
  18.                     if ( $lock_counter > $max_time_to_wait ) {
  19.                         $lock_failed = true;
  20.                         break;
  21.                     }
  22.                     // you really want this to be a fraction of a second so the user waits as little as possible
  23.                     // 你可以认为这是几分之一秒, 为了让用户尽可能少的等待 //
  24.                     // for the simplicity of example, I’m using the sleep function.
  25.                     // 本例中 使用sleep方法. 等待1s再次判断锁是否存在 //
  26.                     sleep(1);
  27.                     $data_lock = $memcache_obj->get($cache_id . ‘_qry_lock’);
  28.                 }
  29.                 // if the loop is completed, that either means the user waited for too long
  30.                 // 循环结束,用户等待太久或者是锁已经被移除, 尝试再次获得, 数据应该已经在缓存中存在 //
  31.                 // or that the lock has been removed. try to get the cached data again; it should exist now
  32.                 $data = $memcache_obj->get($cache_id);
  33.                 if ( $data ) {
  34.                     return $data;
  35.                 }
  36.             }
  37.             // set a lock for 2 seconds
  38.             // 设定一个过期时间为2s的锁 //
  39.             $memcache_obj->set($cache_id . ‘_qry_lock’, true, 2);
  40.             // 从数据库获得数据存储到memcache中 //
  41.             $data = get_data_from_db_function();
  42.             $memcache_obj->set($cache_id, $data, $sec_to_cache_for);
  43.             // don’t forget to remove the lock
  44.             // 不要忘记移除锁
  45.             $memcache_obj->delete($cache_id . ‘_qry_lock’);
  46.         }
  47.         return $data;
  48.     }
Another good use of the semaphore locking is to set the expire time as part of the cached data and have an extended expire time set in memcache. This allows you to have more control over what happens when the cached data becomes stale. You can make it so that one user repopulates the cache while other users continue to get the existing cache until the first user has finished.
----------------

  1. function get_my_data3() {
  2.         $cache_id = "mykey";
  3.         $data = $memcache_obj->get($cache_id);
  4.         // if there is cached data and the expire timestamp has already expired or is within the next 2 minutes
  5.         // then we want the user to freshen up the cached data
  6.         // 如果缓存数据过期,或者时间戳在接下来的两分钟内将要过期 那么我们就要更新缓存中的数据 //
  7.         if ( $data && ($data[‘cache_expires_timestamp’] – time()) < 120 ) {
  8.             // if the semaphore lock has already been set, just return the data like you normally would.
  9.             // 如果已经设定了锁, 那么只需要返回原有数据 //
  10.             if ( $memcache_obj->get($cache_id . ‘_expire_lock’) ) {
  11.                 return $data;
  12.             }
  13.             // now we want to set the lock and have the user freshen the data.
  14.             // 现在我们要设定锁, 并且更新缓存中数据 //
  15.             $memcache_obj->set($cache_id . ‘_expire_lock’, true, 2);
  16.             // by unsetting the data it will cause the data gather logic below to execute.
  17.             // 释放$data
  18.             unset($data);
  19.         }
  20.         if ( !$data ) {
  21.             // be sure to include all of the semaphore logic from example 2
  22.             // set the _qry_lock for 2 seconds
  23.             $memcache_obj->set($cache_id . ‘_qry_lock’, true, 2);
  24.             $raw_data = get_data_from_db_function();
  25.             $data[‘cache_expires_timestamp’] = time() + $sec_to_cache_for;
  26.             $data[‘cached_data’] = $raw_data;
  27.             $memcache_obj->set($cache_id, $data, $sec_to_cache_for);
  28.             // remove the _qry_lock
  29.             $memcache_obj->delete($cache_id . ‘_qry_lock’);
  30.             // remove the _expires_lock
  31.             $memcache_obj->delete($cache_id . ‘_expires_lock’);
  32.         }
  33.         return $data;
  34.     }
When you mash these functions together, you end up with a system where only one user every freshens the cache and/or hits the database with a specific query at a time. There are a lot of other things that suddenly become available once you start thinking of memcached beyond just saving your db from hits. You have things like session handling, smarty template caching, flags for per-server processing (e.g., clearing local file cache), and even as a temporary database.
----------------

原文地址:

阅读(828) | 评论(0) | 转发(0) |
0

上一篇:关闭debian下pc喇叭

下一篇:查看debian版本

给主人留下些什么吧!~~