Redis用法
在Linux中安装Redis命令
$ wget http://redis.googlecode.com/files/redis-2.6.6.tar.gz
$ tar xzf redis-2.6.6.tar.gz
$ cd redis-2.6.6
$ make
安装完后提示:
make[1]: Leaving directory `/root/redis-2.6.6/src'
cd到/root/redis-2.6.6/src然后运行一下将启动服务
$ ./redis-server
得到成功提示:
[
2140] 30 Nov 20:42:24.395 * The server is now ready to accept connections on port 6379
将Redis作为 Linux 服务随机启动
vi /etc/rc.local
在文件中添加:
/root/redis-2.6.6/src/redis-server
更多Redis工具:
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
停止Redis服务命令:
src/redis-cli shutdown
配置Redis:
源码编译完成后,在redis-2.2.12目录下有一个redis.conf文件,
这个文件即是Redis的配置文件,你可以自行修改配置文件里面的参数,用配置文件来启动Redis的方法如下:
[root@localhost redis-2.2.12]# src/redis-server redis.conf
Sidekiq在Rails中的用法
1. 在Gemfile中引入sidekiq
gem 'sidekiq'
然后运行:bundle install
会发现安装了以下几个相关gem:
Installing facter (1.6.15)
Installing timers (1.0.1)
Installing celluloid (0.12.3)
Installing redis (3.0.2)
Installing redis-namespace (1.2.1)
Installing sidekiq (2.5.4)
Installing connection_pool (0.9.2)
2. 创建Worker: app/workers/baidu_top_worker.rb
class BaiduTopWorker
include Sidekiq::Worker
def perform
puts 'backgroud job start'
Site.creat(:name => "a#{rand}", :value => Time.now)
system "rake baidu_top:forager"
puts 'bj end'
end
end
3. 在Controller或者Model中,任何需要调用后台worker的地方加入一下代码:
BaiduTopWorker.perform_async
4. 一切都准备好了,最后启动sidekiq
在app跟目录运行:
bundle exec sidekiq
5. 如何添加或修改了Worker,记得一定要重启sidekiq哦
6. 安装redmon监控redis
gem install redmon
$ redmon >> Thin web server (v1.3.1 codename Triple Espresso) >> Maximum connections set to 1024 >> Listening on 0.0.0.0:4567, CTRL+C to stop [12-03-10 15:49:40] listening on http#0.0.0.0:4567
$ redmon -h Usage: /Users/sean/codez/steelThread/redmon/vendor/ruby/1.9.1/bin/redmon (options) -a, --address ADDRESS The thin bind address for the app (default: 0.0.0.0) -n, --namespace NAMESPACE The root Redis namespace (default: redmon) -l, --lifespan MINUTES Lifespan(in minutes) for polled data (default: 30) -i, --interval SECS Poll interval in secs for the worker (default: 10) -p, --port PORT The thin bind port for the app (default: 4567) -r, --redis URL The Redis url for monitor (default: redis://127.0.0.1:6379) -s, --secure CREDENTIALS Use basic auth. Colon separated credentials, eg admin:admin. --no-app Do not run the web app to present stats --no-worker Do not run a worker to collect the stats