Redis 安装
Debian 安装 Redis:
$ sudo apt-get install redis-server
以守护进程方式启动服务端:
$ redis-server --daemonize yes
测试客户端:
$ redis-cli
127.0.0.1:6379> ping
PONG
检查服务端进程:
$ ps -ef | grep redis-server
barwe 18165 1 0 14:26 ? 00:00:00 redis-server *:6379
barwe 23228 11728 0 14:43 pts/6 00:00:00 grep redis-server
设置密码:
$ redis-cli
127.0.0.1:6379> config set reqiurepass 123456
127.0.0.1:6379> auth 123456
Rails 中集成 Redis
添加 Gem 包:
gem "redis"
新建 config/initializers/redis.rb 文件:
$redis = Redis.new(
:host => "127.0.0.1",
:port => 6379,
:password => "123456",
)
测试:
$ rails c
irb(main):001:0> $redis.set(:age, 23)
=> OK
irb(main):003:0> $redis.get(:age)
=> "123"
Rails 中使用 Redis
redis gem doc: https://www.rubydoc.info/gems/redis/4.6.0
API doc: https://www.rubydoc.info/gems/redis/4.6.0/Redis/Commands
评论区