How to setup Azure Redis cache with Rails

混江龙づ霸主 提交于 2020-01-05 09:11:18

问题


I have a Rails app, and I want to use Azure Redis cache. So far as I got information from internet, I have created a Redis cache on Azure and I have installed Redis gem and I configured in following in redis.rb

$redis = Redis.new(:host => 'xxxxx.redis.cache.windows.net', :port => 6380, :db => 10, :password => "xxxxxxxxxxxxxxxxxxxxxxx", :use_ssl => true)

and after this I don't know how to map it with my database and how to use it.


回答1:


Based on my understanding, it sounds like you want to know how to use Azure Redis Cache via Ruby redis client redis-rb. According to your code, it seems that you had known how to install the redis client library for Ruby and get the connection information from Azure portal, but the code is incorrect.

Here is my sample code for using Ruby to connect Azure Redis Cache.

  1. Installing redis-rb via gem install redis.
  2. My code as below.

    # 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
    

More commands, please see Redis Offical page for Commands, but some commands can not be used on Azure Redis Cache, please see Redis commands not supported in Azure Redis Cache.

Hope it helps. Any concern, please feel free to let me know.



来源:https://stackoverflow.com/questions/41761958/how-to-setup-azure-redis-cache-with-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!