Cache SHOW COLUMNS

本文介绍了作者在Rails项目中优化缓存的过程,包括如何利用补丁文件创建插件以提高性能,并探讨了将缓存机制从内存升级到Memcached的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天recity发布第二版本,优化了半天,终于达到一个稍微满意的速度,闲下来也写了篇bllog轻松下:)


这个东西据说在production已经cache了,是的,确实是的!

今天主要的目的是折腾 :) 于是乎google一把,看有没有现成的plugin,呵呵,俺想偷懒 :D

没找到plugins,但是找到一个不错的patch:http://dev.rubyonrails.org/attachment/ticket/9046/cache_column_names.patch

哦,原来就是这么个东西,开始动手,把它转换成plugins

先说明下,这个只是初步入门级别,没什么深入研究的可是为什么要写这篇文章:
[color=darkred]呵呵,纯属折腾。。。[/color]

新建一个plugin

ruby script/generate plugin CacheColumns


将cache_column_name.patch的代码稍作修改填到我们的lib/cache_columns里去
这里面的columns和reset_column_information是实类方法,所以我们可以使用

base.instance_eval do
block goes here
end

来覆盖原始的method

首先请先备份原始的两个method

alias old_columns columns
alias old_reset_column_information reset_column_information


新建一个全局的变量Hash来缓存columns

@@columns = {}


OK,include 到ActiveRecord::Base里去

ActiveRecord::Base.send :include, CacheColumns::ActiveRecord

打完收工 :)

完整代码如下:
/vendor/plugins/cache_columns/init.rb

ActiveRecord::Base.send :include, CacheColumns::ActiveRecord



/vendor/plugins/cache_columns/lib/cache_columns.rb

module CacheColumns
module ActiveRecord
@@columns = {}

def self.included(base)
base.instance_eval do
alias old_columns columns
alias old_reset_column_information reset_column_information

def columns
if @@columns[table_name].nil?
@@columns[table_name] = connection.columns(table_name, "#{name} Columns")
@@columns[table_name].each {|column| column.primary = column.name == primary_key}
end
@@columns[table_name]
end

# # Resets all the cached information about columns, which will cause them to be reloaded on the next request.
def reset_column_information
generated_methods.each { |name| undef_method(name) }
@column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = @inheritance_column = nil
@@columns.delete(table_name)
end

def reset_column_cache #:nodoc:
@@columns = {}
end
end
end
end



代码我放到code.google.com上面去了
[code]
svn co http://cache-columns.googlecode.com/svn/trunk
[/code]

哦,你想缓存到memcached中?好吧,我们再继续修改之,有过一次经历,这次再动手就简单多了。

require 'memcache_util'

module CacheColumns
module ActiveRecord
def self.included(base)
base.instance_eval do
alias old_columns columns
alias old_reset_column_information reset_column_information
@ttl = 60 * 30 #增加个配置选择?

def columns
record = get_columns(table_name)
unless record
record = connection.columns(table_name, "#{name} Columns")
record.each {|column| column.primary = column.name == primary_key}
cache_store record
end
record
end

# # Resets all the cached information about columns, which will cause them to be reloaded on the next request.
def reset_column_information
generated_methods.each { |name| undef_method(name) }
@column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = @inheritance_column = nil
cache_delete
end

#get columns from memcached
def get_columns(name)
start_time = Time.now
record = Cache.get cache_key_memcache
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('CacheColumns Get (%0.6f) %s' % [elapsed, cache_key_memcache])
record
end

#store columns
def cache_store(record)
start_time = Time.now
Cache.put cache_key_memcache, record, @ttl
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('CacheColumns Set (%0.6f) %s' % [elapsed, cache_key_memcache])
record
end

def cache_key_memcache
"active_record:columns:#{table_name}"
end

def cache_delete
Cache.delete cache_key_memcache
end
end
end
end
end
$(table).bootstrapTable({ url: toBanCache(url), //请求后台的URL(*) method: "get", //请求方式(*) contentType: "application/json", //toolbar: toolbar, //工具按钮用哪个容器 striped: true, //是否显示行间隔色 cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*) pagination: true, //是否显示分页(*) sortable: false, //是否启用排序 sortOrder: "asc", //排序方式 queryParamsType: "undefined",//设置为undefined可以获取pageNumber,pageSize,searchText,sortName,sortOrder//设置为limit可以获取limit, offset, search, sort, order queryParams: paramsMethod, //传递参数(*),这里应该返回一个object,即形如{param1:val1,param2:val2} sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*) pageNumber: 1, //初始化加载第一页,默认第一页 pageSize: 15, //每页的记录行数(*) pageList: [15], //可供选择的每页的行数(*) search: false, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大 strictSearch: false, showColumns: false, //是否显示所有的列 showRefresh: false, //是否显示刷新按钮 minimumCountColumns: 2, //最少允许的列数 clickToSelect: true, //是否启用点击选中行 height: tableHeight, //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度 uniqueId: idField, //每一行的唯一标识,一般为主键列 idField: idField, //指定主键列 showToggle: false, //是否显示详细视图和列表视图的切换按钮 cardView: false, //是否显示详细视图 detailView: false, //是否显示父子表 singleSelect: true,//是否启用单选 columns: createCols(columns, columnWidths, columnAligns, titles, formatters, hasCheckbox) });
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值