我有一个 Rails 应用程序,我想使用 Azure Redis 缓存。就我从互联网上获得的信息而言,我已经在Azure上创建了Redis缓存,并且已经安装了Redis gem,并在redis.rb中进行了以下配置
$redis = Redis.new(:host => 'xxxxx.redis.cache.windows.net', :port => 6380, :db => 10, :password => "xxxxxxxxxxxxxxxxxxxxxxx", :use_ssl => true)
此后我不知道如何将它与我的数据库映射以及如何使用它。
最佳答案
根据我的理解,听起来您想知道如何通过 Ruby redis 客户端使用 Azure Redis 缓存 redis-rb
。根据您的代码,您似乎已经知道如何安装Ruby的redis客户端库并从Azure门户获取连接信息,但代码不正确。
这是我使用 Ruby 连接 Azure Redis 缓存的示例代码。
- 通过
gem install redis
安装redis-rb
。 我的代码如下。
# Import the redis library for Ruby require "redis" # Create a redis client instance for connecting Azure Redis Cache # At here, for enabling SSL, set the `:ssl` symbol with the # symbol value `:true`, see https://github.com/redis/redis-rb#ssltls-support redis = Redis.new( :host => '<azure redis cache name>.redis.cache.windows.net', :port => 6380, :db => <the db index you selected like 10>, :password => "<access key>", :ssl => :true) # Then, set key `foo` with value `bar` and return `OK` status = redis.set('foo', 'bar') puts status # => OK # Get the value of key `foo` foo = redis.get('foo') puts foo # => bar
更多命令请参见Redis官方页面Commands ,但有些命令无法在Azure Redis Cache上使用,请参见Redis commands not supported in Azure Redis Cache .
希望有帮助。如有任何疑问,请随时告诉我。
关于ruby-on-rails - 如何使用 Rails 设置 Azure Redis 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761958/