ruby系统使用memcache缓存
http://guides.rubyonrails.org/
仔细读一下手册里的关于cache的相关部分,注意一句话:
This flag is normally set in the corresponding config/environments/*.rb and caching is disabled by default for development and test, and enabled for production.
默认的开发环境是没有开启缓存的,如果我们要测试缓存,需要手动开启,把默认的false改为true。
$vim config/environments/development.rb
config.action_controller.perform_caching = true
准备活动做完了正式开始啦。
(1)首先在Gemfile里增加对应的gem,增加完运行 $bundle install 安装一下。
(2)在对应的interface文件夹里新增相应的cache语句,从第4行到第七行,中间删除的语句请忽略~
(3)在config/application.rb里增加cache存储的namespace
(4)最后要在config/settings.rb里增加namespace
======================
(5)查一下是否有生成对应的key
$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
stats
STAT pid 1024
STAT uptime 99957
STAT time 1428992525
STAT version 1.4.13
STAT libevent 2.0.16-stable
STAT pointer_size 64
STAT rusage_user 1.024064
STAT rusage_system 0.816051
STAT curr_connections 7
STAT total_connections 9
STAT connection_structures 8
STAT reserved_fds 20
STAT cmd_get 15
STAT cmd_set 2
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 15
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 1720
STAT bytes_written 633
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 157
STAT curr_items 1
STAT total_items 2
STAT evictions 0
STAT reclaimed 0
END
ERROR
ERROR
ERROR
stats items
STAT items:4:number 1
STAT items:4:age 936
STAT items:4:evicted 0
STAT items:4:evicted_nonzero 0
STAT items:4:evicted_time 0
STAT items:4:outofmemory 0
STAT items:4:tailrepairs 0
STAT items:4:reclaimed 0
STAT items:4:expired_unfetched 0
STAT items:4:evicted_unfetched 0
END
stats cachedump 4 100
ITEM ott-youku-cms-development:views/ce5f4eac26ac2773a497d1d557255964.json [22 b; 1428992202 s]
END
===============
在log里查看是第一次请求的时候写cache,第二次请求只读取cache,不用读表
Started GET "/interface/system_control/xx_blacklists.json" for 127.0.0.1 at 2015-04-14 14:23:19 +0800
14:23:19 INFO: Processing by Interface::SystemControlController#xx_blacklists as JSON
14:23:19 DEBUG: Cache read: views/ce5f4eac26ac2773a497d1d557255964.json ({:expires_in=>600})
14:23:19 INFO: Read fragment views/ce5f4eac26ac2773a497d1d557255964.json (0.4ms)
14:23:19 DEBUG: SQL (0.1ms) SELECT COUNT(*) FROM `cms_tv_pidlists` WHERE `cms_tv_pidlists`.`state` = 1 AND (pid = ‘‘ and pid_type = 1)
14:23:19 DEBUG: SQL (0.1ms) SELECT COUNT(*) FROM `cms_client` WHERE `cms_client`.`pid_stat` = 1 AND `cms_client`.`partner_mode` = 12 AND (pid = ‘‘)
14:23:19 DEBUG: SQL (0.1ms) SELECT COUNT(*) FROM `cms_client` WHERE `cms_client`.`pid_stat` = 1 AND `cms_client`.`partner_mode` = 17 AND (pid = ‘‘)
14:23:19 DEBUG: SQL (0.1ms) SELECT COUNT(*) FROM `cms_tv_pidlists` WHERE `cms_tv_pidlists`.`state` = 1 AND (pid = ‘‘ and pid_type = 2)
14:23:19 DEBUG: SQL (0.1ms) SELECT COUNT(*) FROM `cms_tv_blacklists` WHERE `cms_tv_blacklists`.`state` = 1 AND `cms_tv_blacklists`.`blacklist_type` = ‘UA‘ AND (code = ‘‘)
14:23:19 DEBUG: Cache write: views/ce5f4eac26ac2773a497d1d557255964.json ({:expires_in=>600})
14:23:19 INFO: Write fragment views/ce5f4eac26ac2773a497d1d557255964.json (0.4ms)
14:23:19 INFO: Completed 200 OK in 74ms (Views: 3.0ms | ActiveRecord: 0.5ms)
14:24:18 INFO:
Started GET "/interface/system_control/xx_blacklists.json" for 127.0.0.1 at 2015-04-14 14:24:18 +0800
14:24:18 INFO: Processing by Interface::SystemControlController#youku_blacklists as JSON
14:24:18 DEBUG: Cache read: views/ce5f4eac26ac2773a497d1d557255964.json ({:expires_in=>600})
14:24:18 INFO: Read fragment views/ce5f4eac26ac2773a497d1d557255964.json (0.4ms)
14:24:18 INFO: Completed 200 OK in 42ms
14:24:18 INFO:
按住 ctrl + ]
telnet>
输入quit 就可以推出了
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。