PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to connect to elastic cache in rails

 September 05, 2022     amazon-web-services, elastic-cache, redis, ruby, ruby-on-rails     No comments   

Issue

I am looking for a way to connect the rails application to elastic cache for storing the cache. For testing purpose I try to test with redis-cli which is working fine. Is there any library or documentation available to connect to elastic cache. I already try to search but no luck

redis-cli -h primary-endpoint --tls -p 6379 -a "token"

I tried with redis-rails gem with the following code but it didn't work

REDIS_CONFIG = { 
    "url"=>"primary_endpoint",
     "port"=>6379
 }

@redis_token_store ||= Redis.new(REDIS_CONFIG)


@redis_token_store.set(1, "2")

I get the following error

/usr/local/bundle/gems/redis-4.0.1/lib/redis/client.rb:344:in `rescue in establish_connection': Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)
/usr/local/lib/ruby/3.0.0/socket.rb:1214:in `__connect_nonblock': Connection refused - connect(2) for 127.0.0.1:6379 (Errno::ECONNREFUSED)
/usr/local/lib/ruby/3.0.0/socket.rb:1214:in `__connect_nonbloc: Operation now in progress - connect(2) would block (IO::EINPROGRESSWaitWritable)

Also, not sure how to pass the token in the configuration


Solution

I have found the solution if someone encountered the same he can take reference

The gem which I have used is redis-rails

Redis.new(url: "rediss://primary_endpoint:port_number", password: "") 

Notice the "ss" in the rediss that is mandatory otherwise the connection will not get established from elastic cache

For reference

The authentication token(which is fetched from AWS Secret Manager) can be passed into the password



Answered By - Aniket Tiwari
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing