王筝的博客
ruby学习

memcache默认端口是11211,如何换成自定义端口咩

如果是在开发环境不要忘记开启缓存
$ vim  config/environments/development.rb
config.action_controller.perform_caching = true

启动:
$memcached -d -m 10 -u root -l 127.0.0.1 -p 11215 -c 256 -P /tmp/memcached.pid

停用:
$kill `cat /tmp/memcached.pid`

配置config/settings.yml 文件
memcached:
namespace: xx-cms-development
servers:
– 127.0.0.1:11215

注意这里不要写外网的ip,刚才在测试服务器用cache失败是因为servers这里配置了10.100.xx.xx,
而启动的时候用的是127.0.0.1

  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)来看一下是否生效

$ 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 xx-cms-development:views/ce5f4eac26ac2773a497d1d557255964.json [22 b; 1428992202 s]

END

 

 

===============

(6)来看一下log里是否第一次请求有写cache,第二次直接读取,无需写cache和读数据库

Started GET “/interface/system_control/xxx_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 就可以推出了