Más contenido relacionado La actualidad más candente (20) Similar a 高性能Web应用缓存架构设计浅谈 (20) 高性能Web应用缓存架构设计浅谈3. 特殊应用场景
• 长连接
• Web chat,Web game
• 网络IO资源消耗型,非CPU消耗型
• EventMachine,Twisted, Node.js
Thursday, August 4, 11
6. JavaEye统计数据
• 3,000,000+ Rails dynamic requests per day
• 15,000,000+ HTTP requests per day
• 600+ HTTP requests/s on peak time
• 1,000,000+ full-text search per day
• 1400+ current connections/s on peak time
• 600 average Memcached get operations
• 150 average SQL queries per second
Thursday, August 4, 11
8. Web Server DB Server
CPU Peak < 60% CPU Peak < 25%
IO Wait < 5% IO Wait < 15%
Thursday, August 4, 11
16. REST架构的设计准则
• 网络上的所有事物都被抽象为资源(resource)
• resource = data + representation
• 每个资源对应⼀一个唯⼀一的资源标识(resource identifier)
• resource identifier = URL
• 通过连接器(generic connector interface)对资源进行操作
• generic connector = HTTP Procotol (GET/POST/PUT/DELETE)
• 对资源的各种操作不会改变资源标识
• 对资源的GET操作具有幂等性,不改变资源的状态
• 所有的操作都是无状态的(stateless)
Thursday, August 4, 11
21. def news
fresh_when(:last_modified => News.last.created_at, :etag => News.last)
end
@blogs = @blog_owner.last_blogs
@hash = @blogs.collect{|b| {b.id => b.post.modified_at.to_i +
b.posts_count}}.hash
if stale?(:last_modified => (@blog_owner.last_blog.post.modified_at ||
@blog_owner.last_blog.post.created_at), :etag => @hash)
render :template => "blog"
end
def board
@topics = @forum.topics.paginate...
if logged_in? || stale?(:last_modified =>
@topics[0].last_post.created_at, :etag => @topics.collect{|t| {t.id =>
t.posts_count}}.hash)
@announcements = (params[:page] || 1).to_i == 1 ?
Topic.find :all, :conditions...
render :action => 'show'
end
end
Thursday, August 4, 11
22. def show
@topic = Topic.find params[:id]
user_session.update_....... if logged_in?
Topic.increment_counter(...) if ......
@posts = @topic.post_by_page params[:page]
posts_hash = @posts.collect{|p| {p.id => p.modified_at}}.hash
topic_hash = @topic.forum_id + @topic.sys_tag_id.to_i + @topic.title.hash +
@topic.status_flag.hash
ad_hash = ... (广告的hash算法,略)
if logged_in? || stale?(:etag => [posts_hash, topic_hash, ad_hash])
render
end
end
def topic
@topic = Topic.find(params[:id])
unless logged_in?
if request.not_modified?(5.days.ago)
head :not_modified
else
response.last_modified = Time.now
end
end
end
Thursday, August 4, 11
31. 应用缓存概述
• 对象缓存
• 查询缓存
• 页面缓存
• 动态页面静态化(page cache)
• 页面片段缓存(frgment cache)
• 基于REST资源的缓存
Thursday, August 4, 11
34. 山寨cache plugin
• 基于Rails Cache的简单封装,仅60行代码
• 可以自动实现对象缓存的管理,n:1关系
的缓存,但不支持1:n集合缓存
• memcached缓存命中率超过96%
Thursday, August 4, 11
46. ip_counter = Rails.cache.increment(request.remote_ip)
if !ip_counter
Rails.cache.write(request.remote_ip, 1, :expires_in => 30.minutes)
elsif ip_counter > 2000
crawler_counter = Rails.cache.increment("crawler/
#{request.remote_ip}")
if !crawler_counter
Rails.cache.write("crawler/#{request.remote_ip}", 1, :expires_in
=> 10.minutes)
elsif crawler_counter > 50
BlackList.add(ip_sec)
render :file => "#{RAILS_ROOT}/public/403.html", :status => 403
and return false
end
render :template => 'test', :status => 401 and return false
end
Thursday, August 4, 11