Skip to content

Instantly share code, notes, and snippets.

@itamarhaber
Forked from kehati/rediscloud.html.md
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save itamarhaber/8709318 to your computer and use it in GitHub Desktop.
Save itamarhaber/8709318 to your computer and use it in GitHub Desktop.
title category
Redis Cloud
marketplace

Redis Cloud is a fully-managed cloud service for hosting and running your Redis dataset in a highly-available and scalable manner, with predictable and stable top performance. You can quickly and easily get your apps up and running with Redis Cloud through its add-on for Cloud Foundry, just tell us how much memory you need and get started instantly with your first Redis database.

Redis Cloud offers true high-availability with its in-memory dataset replication and instant auto-failover mechanism, combined with its fast storage engine. You can easily import an existing dataset to any of your Redis Cloud databases, from your S3 account or from any other Redis server. Daily backups are performed automatically and in addition, you can backup your dataset manually at any given time. The service guarantees high performance, as if you were running the strongest cloud instances.

Creating A Redis Cloud Service

Create a Redis Cloud service with the following command:

cf create-service rediscloud

and the desired plan.

Binding Your Redis Cloud Service

Bind your Redis Cloud service to your app, using the following command:

cf bind-service <rediscloud_service_name> <app name>

Once your Redis Cloud service is bound to your app, the service credentials will be stored in the VCAP_SERVICES env. variable in the following format:

{
  rediscloud-n/a: [
    {
      name: "rediscloud-42",
      label: "rediscloud-n/a",
      plan: "20mb",
      credentials: {
        port: "6379",
        hostname: "pub-redis-6379.us-east-1-2.3.ec2.garantiadata.com",
        password: "your_redis_password"
      }
    }
  ]
}

Using Redis from Ruby

The redis-rb is a very stable and mature Redis client and is probably the easiest way to access Redis from Ruby.

Install redis-rb:

sudo gem install redis

Configuring Redis from Rails

For Rails 2.3.3 up to Rails 3.0, update the config/environment.rb to include the redis gem:

config.gem 'redis' 

For Rails 3.0 and above, update the Gemfile:

gem 'redis'  

And then install the gem via Bundler:

bundle install

Lastly, create a new redis.rb initializer in config/initializers/ and add the following code snippet:

rediscloud_service = JSON.parse(ENV['VCAP_SERVICES'])["rediscloud-n/a"]
credentials = rediscloud_service.first["credentials"]
	$redis = Redis.new(:host => credentials.hostname, :port => credentials.port, :password => credentials.password)

Configuring Redis on Sinatra

Add this code snippet to your configure block:

configure do
    . . .
	require 'redis'
	rediscloud_service = JSON.parse(ENV['VCAP_SERVICES'])["rediscloud-n/a"]
	credentials = rediscloud_service.first["credentials"]
	$redis = Redis.new(:host => credentials.hostname, :port => credentials.port, :password => credentials.password)
    . . .
end

Using Redis on Unicorn

No special setup is required when using Redis Cloud with a Unicorn server. For Rails apps on Unicorn, follow the instructions in the Configuring Redis from Rails section and for Sinatra apps on Unicorn see Configuring Redis on Sinatra section.

Testing (Ruby)

$redis.set("foo", "bar")
# => "OK"
$redis.get("foo")
# => "bar"

Using Redis from Java

Jedis is a blazingly small, sane and easy to use Redis java client. You can download the latest build from github or use it as a maven dependency:

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.0.0</version>
	<type>jar</type>
	<scope>compile</scope>
</dependency>

Configure the connection to your Redis Cloud service using the VCAP_SERVICES environment variable and the following code snippet:

try {
	String vcap_services = System.getenv("VCAP_SERVICES");
	if (vcap_services != null && vcap_services.length() > 0) {
		// parsing rediscloud credentials
		JsonRootNode root = new JdomParser().parse(vcap_services);
		JsonNode rediscloudNode = root.getNode("rediscloud-n/a");
		JsonNode credentials = rediscloudNode.getNode(0).getNode("credentials");
		
		JedisPool pool = new JedisPool(new JedisPoolConfig(),
	    		credentials.getStringValue("hostname"),
	    		Integer.parseInt(credentials.getNumberValue("port")),
	    		Protocol.DEFAULT_TIMEOUT,
	    		credentials.getStringValue("password"));				
	}			
} catch (InvalidSyntaxException ex) {
	// vcap_services could not be parsed.
} 

Testing (Java)

jedis jedis = pool.getResource();
jedis.set("foo", "bar");
String value = jedis.get("foo");
// return the instance to the pool when you're done
pool.returnResource(jedis);

(example taken from Jedis docs).

Using Redis from Python

redis-py is the most commonly-used client for accessing Redis from Python.

Use pip to install it:

sudo pip install redis

Configure the connection to your Redis Cloud service using VCAP_SERVICES environment variable and the following code snippet:

import os
import urlparse
import redis
import json

rediscloud_service = json.loads(os.environ['VCAP_SERVICES'])['rediscloud-n/a'][0]
credentials = rediscloud_service['credentials']
r = redis.Redis(host=credentials['hostname'], port=credentials['port'], password=credentials['password'])

Testing (Python):

>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'

Redis can be used as the back-end cache for Django. To do so, install django-redis-cache:

pip install django-redis-cache

Next, configure your CACHES in the settings.py file:

import os
import urlparse
import json

rediscloud_service = json.loads(os.environ['VCAP_SERVICES'])['rediscloud-n/a'][0]
credentials = rediscloud_service['credentials']
CACHES = {
	'default': {
	'BACKEND': 'redis_cache.RedisCache',
	'LOCATION': '%s:%s' % (credentials['hostname'], credentials['port']),
	'OPTIONS': {
        'PASSWORD': credentials['password'],
		'DB': 0,
    }
  }
}

Testing (Django)

from django.core.cache import cache
cache.set("foo", "bar")
print cache.get("foo")

Using Redis from PHP

Predis is a flexible and feature-complete PHP client library for Redis.

Instructions for installing the Predis library can be found here.

Loading the library to your project should be straightforward:

<?php
// prepend a base path if Predis is not present in your "include_path".
require 'Predis/Autoloader.php';
Predis\Autoloader::register();

Configure connection to your Redis Cloud service using VCAP_SERVICES environment variable and the following code snippet:

// parsing rediscloud credentials
$vcap_services = getenv("VCAP_SERVICES");
$rediscloud_service = json_decode($vcap_services, true)["rediscloud-n/a"][0]
$credentials = $rediscloud_service["credentials"]

$redis = new Predis\Client(array(
	'host' => $credentials["hostname"], 
	'port' => $credentials["port"],
	'password' => $credentials["password"], 
));

Testing (PHP)

$redis->set('foo', 'bar');
$value = $redis->get('foo');

Using Redis from Node.js

node_redis is a complete Redis client for node.js.

You can install it with:

npm install redis

Configure the connection to your Redis Cloud service using VCAP_SERVICES environment variable and the following code snippet:

// parsing rediscloud credentials
var vcap_services = process.env.VCAP_SERVICES;
var rediscloud_service = JSON.parse(vcap_services)["rediscloud-n/a"][0]
var credentials = rediscloud_service.credentials;

var redis = require('redis');
var client = redis.createClient(credentials.port, credentials.hostname, {no_ready_check: true});
client.auth(credentials.password);

###Testing (Node.js)

client.set('foo', 'bar');
client.get('foo', function (err, reply) {
    console.log(reply.toString()); // Will print `bar`
});

Dashboard

Our dashboard presents all performance and usage metrics of your Redis Cloud service on a single screen, as shown below:

Dashboard

To access your Redis Cloud dashboard, simply click the 'Manage' button next to the Redis Cloud service on your app space console.

You can then find your dashboard under the MY DATABASES menu.

Managing Services

You can continue managing your services from the command line.

Support

Any Redis Cloud support issues or product feedbacks are welcome via email at support@redislabs.com. Please make sure you are familiar with the CloudFoundry method of contacting service providers for support.

Additional resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment