Introduction to Redis

Introduction to Redis

Redis is a key-value database that has very nice features which every computer scientist love it.

Why I love Redis?

  • Redis give the data structure like all computer scientist learn in the school such as Set, sorted set, List, and Hash. So you don’t need to think in SQL anymore.
  • Redis is very fast it can gives you the performance about 50,000 to 100,000 operation per second in basic basic computers (2GHz CPU with 2GB RAM).
  • you can install Redis in 2 minutes. (Linux and Windows)
  • It’s written in ANSI-C so you can easily pick the source code (Yes, it’s open source!) and modify the code if you want.
  • There are native clients for almost every language (include PythonRuby and PHP) and Yes, you don’t need Thrift.
  • You can setup master-slave replication of Redis in 2 minutes.
  • Redis has great community lead by Salvatore Sanfilippo. (also check Redis Google group)
  • Redis is great for large scale web programming without need of MySQL. (Check Retwis )
  • Redis is very easy to learn.

Where should I start the basic?

  • First download the source code from Github or Google code.
  • Second compile the source code (basically just type ./make)
  • Download and install your language client if you prefer to work with Redis from your language here is list all language.
  • Run Redis server — $ ./redis-server — (Redis use port # 6379 by default)
  • Run Redis client  – $ ./redis-cli – and enter follow commands to see “Hello World!”

1- > set foo bar
2- > get foo
3- > lpush myList world!
4- > lpush myList Hello
5- > lpop myList
6- > lpop myList

In line 1, we set the key name “foo” to the value “bar”

In line 2, we get the value for the key name “foo” so the return value is “bar”

In line 3, we (left) push the value “world!” to the list “myList”

In line 4, we (left) push the value “Hello” to the list “myList”

In line 5, we (left) pop the value from “myList” and the return value is “Hello”

In line 6, we (left) pop the value from “myList” and the return value is “world!”

Now, you get the idea how Redis works, so lets do one real project in Python and Redis.

Can I see Redis in real project?

Yes, In this project I want to design a program that get the RSS from source, then parse the RSS and send the title of each news to Twitter.

What you need to do before start to code?

0- Python 2.5 or higher

1- Redis and py-redis

2- Twitter python library (included in package)

3- Yahoo social python

Next:    Enter the following code in your favorite text editor and save the file.

import redis,twitter
import yahoo.yql

def getandset():
	''' Get the weather condition for WOID=12778445 and save in the database'''

	q = yahoo.yql.YQLQuery().execute('select * from rss where url="http://weather.yahooapis.com/forecastrss?w=12778445"')
	temp = q["query"]["results"]["item"]["condition"]["temp"]
	text = q["query"]["results"]["item"]["condition"]["text"]
	date = q["query"]["results"]["item"]["condition"]["date"]
	#forecast
	high = q["query"]["results"]["item"]["forecast"][0]['high']
	low  = q["query"]["results"]["item"]["forecast"][0]['low']

	r = redis.Redis('localhost')
	r.set('weather:current:temp', temp)
	r.set('weather:current:cond', text.lower())
	r.set('weather:tomorrow:high', high)
	r.set('weather:tomorrow:low', low)

def post():
	API = twitter.Api(username = "YOUR TWITTER USERNAME", password = 'YOUR TWITTER PASSWORD')
	r = redis.Redis('localhost')
	temp = r.get('weather:current:temp')
	cond = r.get('weather:current:cond')
	tom_high = r.get('weather:tomorrow:high')
	tom_low = r.get('weather:tomorrow:low')
	if (temp or cond or tom_high or tom_low):
	tweet = 'Currently %s F and %s at %s. Tomorrow: high %s F, low: %s F'
	name = 'West Lafayette'
	tweet = tweet % (temp, cond, name, tom_high, tom_low)
	print tweet
	print "Posting...."
	API.PostUpdate(tweet)
	print "Done!"

if __name__ == '__main__':
getandset()
post()

Summery of code:
In this code we first use YQL (the service form Yahoo!) to get the RSS (in this example we get the weather condition for West Lafayette, IN)
Then we parse the RSS to get our desired field which is basically temperatures and forecast. After that we save these values to our database using ‘SET’ function. Please make attention to the key name style I use “‘weather:current:temp’” which is basically tell you in this key we want to have the today’s temperature. In the post function, I basically retrieve the value we inserted to database and post it to your Twitter account.
You can download the source code for this tutorial from here.
Now that you should learnt the SET data structure, next time I will explain LIST which is one of the most important structure in Redis.
Further Reading:

comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>