redis pipeline
2013年9月27日 18:50
在redis的一个集合中有上万的元素,这些元素对应对象属性有几个键值,原来通过逐个读取的方式访问,获取集合中所有元素的信息需要300多秒,严重影响性能,google后发现,redis提供pipeline来获取大量数据,改用pipeline实现后缩短到20秒
$redis = Redis.new keys = $redis.smembers("userlist") res_ids = $redis.pipelined do keys.each do |client_id| $redis.get("account:#{client_id}:id") end end res = $redis.pipelined do res_ids.each do |id| x = "account:"+id lastlogin = $redis.hmget("#{x}:lastlogin","ip","time") firstlogin = $redis.hmget("#{x}:firstlogin","ip","time") multi_value = $redis.mget(x+":available",x+":network_type",x+":traffic_used",x+":traffic_wifi_used", x+":tasks_today",x+":max_traffic",x+":tasks_wifi_today",x+":tasks_3g_today", x+":tasks_wifi_finished",x+":tasks_3g_finished",x+":tasks_wifi_arrive", x+":tasks_3g_arrive",x+":tasks_wifi_success",x+":tasks_3g_success", x+":month_3g_traffic",x+":month_3g_traffic_yestoday",x+":month_2g_traffic", x+":month_2g_traffic_yestoday",x+":month_wifi_traffic", x+":month_wifi_traffic_yestoday",x+":system_info",x+":province_name", x+":city_name",x+":isp_name",x+":software_version", x+":task:send",x+":task:arrive",x+":task:finish",x+":task:pcapempty", x+":tasks_3g_yestoday",x+":tasks_wifi_yestoday") [lastlogin , firstlogin , multi_value ] end end