ruby-on-rails - 如何使用 Rails 设置 Azure Redis 缓存

标签 ruby-on-rails azure-redis-cache

我有一个 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 缓存的示例代码。

  1. 通过 gem install redis 安装 redis-rb
  2. 我的代码如下。

    # 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/

相关文章:

ruby-on-rails - 如何使用 simple_form 解决 undefined method `to_key' 错误

javascript - rails : Include External JavaScript

jquery - 使用 Rails/Jquery 创建动态更新的进度条?

azure - 如何在REDIS缓存中添加全局变量

ruby-on-rails - 如何为脚本或批处理文件设置 rails_env

mysql - 将表数据迁移到其他表的最佳方法

Azure Redis 缓存服务丢失了所有数据

redis - Azure Redis 缓存是否在到期时刷新项目

azure - Redis 缓存 maxmemory-reserved 设置是 Azure 独有的吗?

asp.net - 使用 varyByCustom 时如何使用 : HttpResponse. RemoveOutputCacheItem