简单实现ruby数据库加载和缓存加载
2015-03-12 14
require 'active_record'
require 'redis-store'
ActiveRecord::Base.establish_connection(:adapter => "mysql2", :host => "<host>", :username => "<username>", :password => "<password>", :database => "db", :encoding => "utf8")
CACHE = ActiveSupport::Cache.lookup_store :redis_store, 'redis://127.0.0.1', {namespace: 'cache', expires_in: 300}
class RobotFilter < ActiveRecord::Base
#获取数据库链接
@robots = ActiveRecord::Base.connection.execute("select useragent, exclusion_useragent from robot_filters").collect { |e| [e[0].downcase, e[1].downcase] }
end
def eventinfo
#将查找放入缓存,如果已生成则直接读取。
return @eventinfo if self.instance_variable_defined? "@eventinfo"
@eventinfo = CACHE.fetch("Eventinfo:#{event_id}") { Eventinfo.find_by_id event_id }
end
简单实现ruby数据库加载和缓存加载
猜你喜欢
转载自schooltop.iteye.com/blog/2241631
今日推荐
周排行