簡體   English   中英

如何使用Rails設置Azure Redis緩存

[英]How to setup Azure Redis cache with Rails

我有一個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客戶端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官方頁面以獲取命令 ,但是某些命令不能在Azure Redis緩存上使用,請參閱Azure Redis Cache中不支持的Redis命令

希望能幫助到你。 如有任何疑問,請隨時告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM