ruby-on-railsrubyamazon-web-servicesrediselastic-cache

How to connect to elastic cache in rails


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