zntw3a
Last Updated: February 25, 2016
·
676
· devl

Raketask for connecting to Redis on Heroku

Assuming redis-cli is installed on your computer and you're using OpenRedis. If not, replace OPENREDIS_URL with the correct Heroku config variable that contains a Redis connection string.

desc 'Connect to Redis (production)'
task :redis do
  redis_uri = `heroku config:get OPENREDIS_URL`
  pattern = /redis:\/\/:(\S+)@(\S+):(\d+)/
  redis_uri[pattern]
  exec "redis-cli -a #{$1} -h #{$2} -p #{$3}"
end

Execute by running rake redis

The regex pattern will capture the password, the host and the port into $1, $2 and $3 respectively. exec will replace the currently running process with that of the command being executed, in this case redis-cli.

Perhaps a bit Perl-ish code, but it gets the job done. :)

Update: OpenRedis is using a proxy these days. For details, see http://gist.io/4198977