Skip to content

Instantly share code, notes, and snippets.

@kehati
Last active August 29, 2015 14:04
Show Gist options
  • Save kehati/6423c5dc4b17cf1cd4b2 to your computer and use it in GitHub Desktop.
Save kehati/6423c5dc4b17cf1cd4b2 to your computer and use it in GitHub Desktop.

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 OpenShift, 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 via the OpenShift Marketpalce. On the Editions & Pricing tab, click on the desired add-on from the Add-ons section.

Binding Your Redis Cloud Service

At the Marketplace, navigate to the Purchased Products tab. Click on the 'Add to apps' button, right to your provisioned Redis Cloud service, and choose the bound app.

Once your Redis Cloud service is bound to your app, the service credentials will be stored in as an env. variable. The env. var name is identical to the provisioned service name, and its value will be in the following format:

rediscloud_xxxx={"port":"6379","hostname":"pub-redis-6379.us-east-1-2.3.ec2.garantiadata.com","your_redis_password"}

To see the exact value of your env. var, SSH into your application and run:

env | grep "rediscloud"

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 = JSON.parse(ENV['rediscloud_xxxx'])
$redis = Redis.new(:host => rediscloud.hostname, :port => rediscloud.port, :password => rediscloud.password)

Configuring Redis on Sinatra

Add this code snippet to your configure block:

configure do
    . . .
	require 'redis'
	rediscloud = JSON.parse(ENV['rediscloud_xxxx'])
$redis = Redis.new(:host => rediscloud.hostname, :port => rediscloud.port, :password => rediscloud_service.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 rediscloud = System.getenv("rediscloud_xxxx");
	if (rediscloud != null && rediscloud.length() > 0) {
		JSONObject json = (JSONObject)new JSONParser().parse(rediscloud);
		JedisPool pool = new JedisPool(new JedisPoolConfig(),
	    		json.get("hostname"),
	    		Integer.parseInt(json.get("port")),
	    		Protocol.DEFAULT_TIMEOUT,
	    		json.get("password"));				
	}			
} catch (ParseException ex) {
	// rediscloud 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 = json.loads(os.environ['rediscloud_xxxx'])
r = redis.Redis(host=rediscloud['hostname'], port=rediscloud['port'], password=rediscloud['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 = json.loads(os.environ['rediscloud_xxxx'])
CACHES = {
	'default': {
	'BACKEND': 'redis_cache.RedisCache',
	'LOCATION': '%s:%s' % (rediscloud['hostname'], rediscloud['port']),
	'OPTIONS': {
        'PASSWORD': rediscloud['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:

$rediscloud = json_decode(getenv("rediscloud_xxxx"), true);
$redis = new Predis\Client(array(
	'host' => $rediscloud["hostname"], 
	'port' => $rediscloud["port"],
	'password' => $rediscloud["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:

var rediscloud = JSON.parse(process.env.rediscloud_xxxx);
var redis = require('redis');
var client = redis.createClient(rediscloud.port, rediscloud.hostname, {no_ready_check: true});
client.auth(rediscloud.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, navigate to the Purchased Products tab at the OpenShift Marketpalce and simply click on the Redis Cloud service.

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

Support

Any Redis Cloud support issues or product feedbacks are welcome via email at support@redislabs.com.

Additional resources

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