Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1858178
  • 博文数量: 211
  • 博客积分: 464
  • 博客等级: 下士
  • 技术积分: 3794
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-24 18:25
个人简介

阿弥陀佛

文章分类

全部博文(211)

文章存档

2020年(2)

2019年(3)

2018年(5)

2017年(6)

2016年(10)

2015年(9)

2014年(73)

2013年(90)

2012年(13)

分类: 服务器与存储

2013-11-30 19:44:51

memcached允许用户设置数据的过期时间,那么memcached是如何管理过期数据的呢?
原理很简单:首先通过hash表查找该数据,然后判断该数据是否过期。如果过期,那么就将数据删除,否则将数据交给用户。
系统中有一个定时器事件。

点击(此处)折叠或打开

  1. /* libevent uses a monotonic clock when available for event scheduling. Aside
  2.  * from jitter, simply ticking our internal timer here is accurate enough.
  3.  * Note that users who are setting explicit dates for expiration times *must*
  4.  * ensure their clocks are correct before starting memcached. */
  5. static void clock_handler(const int fd, const short which, void *arg) {
  6.     struct timeval t = {.tv_sec = 1, .tv_usec = 0};
  7.     static bool initialized = false;

  8.     evtimer_set(&clockevent, clock_handler, 0);
  9.     event_base_set(main_base, &clockevent);
  10.     evtimer_add(&clockevent, &t);

  11. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
  12.     if (monotonic) {
  13.         struct timespec ts;
  14.         if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
  15.             return;
  16.         current_time = (rel_time_t) (ts.tv_sec - monotonic_start);
  17.         return;
  18.     }
  19. #endif
  20.     {
  21.         struct timeval tv;
  22.         gettimeofday(&tv, NULL);
  23.         current_time = (rel_time_t) (tv.tv_sec - process_started);
  24.     }
  25. }
可以看到事件驱动模型的魅力,大大简化了网络编程的难度。
事件驱动libevent库编程模型需如下几步:
1.为事件绑定处理函数
2.将事件绑定到event_base中(一般一个thread对应一个event_base,可以向event_base上绑定很多事件)
3.event_add(base,&timeout) 设置回调函数的最晚执行事件。

阅读(5605) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

shoeshop12014-11-12 11:57:16

很好高仿香奈儿1113系列包包 http://chanelbagscn.com/chanle-big-2.html
高仿香奈儿1115系列包包 http://chanelbagscn.com/chanle-big-3.html
高仿香奈儿1116系列包包 http://chanelbagscn.com/chanle-big-4.html