How-to connect your app to Redis
FeaturedIf you’re thinking about binding one of our supported services (like Redis, MongoDB or RabbitMQ for instance) to your application, the access information / credentials (usually: host, port, user, password) to this service will be provided in the environment variables.
Environment variables
Each application can read the environment variables, no matter what programming language and/or framework you are using. Let’s stick with Ruby for now. Accessing the environment variables in Ruby looks like this:
value = ENV['VARIABLE_NAME']
The name of the variable which contains the access information to all bound services is ‘VCAP_SERVICES’. The information in this variable is encoded in JSON.
To get the hostname for a bound Redis service you can use the following oneliner in Ruby:
JSON.parse( ENV['VCAP_SERVICES'] )['redis-2.2'].first['credentials']['hostname']
In Rails you can simply fill your redis.yml with the content of this file to get all information right.
Please sign in to leave a comment.
Comments
0 comments