【问题标题】:How to setup Azure Redis cache with Rails如何使用 Rails 设置 Azure Redis 缓存
【发布时间】:2017-02-10 15:52:09
【问题描述】:

我有一个 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-on-rails azure-redis-cache


【解决方案1】:

根据我的理解,听起来您想知道如何通过 Ruby redis 客户端 redis-rb 使用 Azure Redis 缓存。根据您的代码,您似乎知道如何安装 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

希望对您有所帮助。有任何问题,请随时告诉我。

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 2014-07-14
    • 2022-01-10
    • 2021-05-09
    • 2015-04-16
    • 1970-01-01
    • 2015-12-08
    • 2021-06-06
    • 2018-10-05
    相关资源
    最近更新 更多