Redis has a rich ecosystem of clients for a large range of programing languages. To illustrate using these clients, we'll focus on two languages, Python and Javascript through node.js.
Python is popular scripting language that is used in a wide range of industries. We will be demonstrating how to use Redis in a Python application with the focus not becoming an expert in Python, but how to use Redis in a programming context with simple scripting support.
$ sudo apt-get install Python3.4 $ wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.11.6.tar.gz $ tar xvf virtualenv-1.11.6.tar.gz $ python3 virtualenv-1.11.6/virtualenv.py redis-test-env $ source redis-test-env/bin/activate (redis-test-env) $ pip install redis
$ source redis-py-env/bin/activate (redis-py-env) $ python Python 3.4.0 (v3.4.0:04f714765c13, Mar 15 2014, 23:02:41) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>> local_redis = redis.StrictRedis() >>> local_redis.dbsize()
We'll now use Python to create a small Twitter clone based upon the tutorial, Tutorial: Design and implementation of a simple Twitter clone using PHP and the Redis key-value store
The Redis Key structure for this Twitter Clone
Key | Description | Redis Data Type |
---|---|---|
next_user_id | user id counter | String |
User:{ user_id } | Each user key includes a user id based on the next_user_id and stores information related to the user, including username subkey. | Hash |
Users | Contains a mapping between each User's username and user_id | Hash |
followers:{ user_id } | Stores user ids for all of the users that following a particular user along with a timestamp score | Sorted set |
following:{ user_id } | Stores user ids for all of the users that a particular user is following along with a timestamp score | Sorted set |
next_post_id | post id counter | String |
post:{ post_id } | Message, timestamp, and creating User for a single post | Hash |
posts:{ user_id } | Posts from users that the User is following | List |
>>> def add_user(username): user_id = local_redis.incr("next_user_id") user_key = "User:{}".format(user_id) local_redis.hset(user_key, "username", username) local_redis.hset("Users", username, user_id) return user_key >>> first_user_key = add_user("dwallace") >>> second_user_key = add_user("bbarker") >>> print(first_user_key)
>>> def add_follower(user, follower): follower_key = "followers:{}".format(local_redis.hget('Users', user).decode()) local_redis.zadd(follower_key, time.time(), local_redis.hget('Users', follower)) >>> add_follower('dwallace', 'bbarker')
def add_following(user, following_user): following_key = "following:{}".format(local_redis.hget('Users', user).decode()) local_redis.zadd(following_key, time.time(), local_redis.hget('Users', following_user))
def create_post(username, body): post_id = local_redis.incr("next_post_id") post_key = "post:{}".format(post_id) local_redis.hmset(post_key, {"user_id": local_redis.hget("Users", username), "time": time.time(), "body": body}) followers = local_redis.zrange("followers:{}".format( local_redis.hget("Users", username).decode()), 0,-1) for follower_id in followers: posts_key = "posts:{}".format(follower_id.decode()) local_redis.lpush(posts_key, post_id) return post_key
def get_my_posts(username): user_id = local_redis.hget("Users", username) posts_id = "posts:{}".format(user_id.decode()) posts = local_redis.lrange(posts_id, 0, -1) for post_id in posts: post_key = "post:{}".format(post_id.decode()) print("""{}:\n{}""".format(local_redis.hget(post_key, "user_id"), local_redis.hget(post_key, "body")))
The server-side Javascript framework, node.js (although there was recent major fork called io.js)
The first steps running node.js, is to download and install node.js and then install the recommended node.js client. We will also two other node modules, express and swig to build a simple website using Redis.
$ sudo apt-get install node.js $ npm install hiredis redis $ npm install express $ npm install swig --save
Second, change directory to the node
example directory and
run the node.js application
$ cd introduction-to-redis/programming-redis $ node server.js Application Started on http://localhost:1337/
To see an existing Node.js project that uses Redis, checkout Amir Rajan's Todo Application at https://github.com/amirrajan/nodejs-todo.
$ git clone https://github.com/amirrajan/nodejs-todo.git $ cd nodejs-todo $ npm install