064 Redis

27 Dec 2015

Redis is an in-memory datastore that persists. In this screencast we will go through some simple commands to get, set, delete integers, string, list and hashes. We will also touch on the pub/sub feature of redis ending off with redis client implemented with node.js.

Download video: mp4

Sample code: Github

Version: 2.8.19

Background on Redis

  1. homepage

Things to learn with Redis

install

  1. with brew: brew install redis
    • check version: redis-cli -v
    • start the redis server: redis-server
    • start the redis command line: redis-cli
    • get all keys in the redis cli: keys *

get, set, expire and delete keys

  1. set a key set mykey "myvalue"
    • get a key get mykey
    • get all keys keys *
    • delete all keys flushdb
    • get all keys after flushing db keys *
    • set key set travel:destination "Sydney"
    • get all keys keys *
    • delete one key del travel:destination
    • get all keys keys *
    • set a key and expire in 20 seconds set mykey 100 ex 20
    • check time to live ttl mykey
    • get the key get mykey
    • get key after expiry get mykey

integers

  1. set an integer set counter 1
    • increase by 1 incr counter
    • decrease by 1 decr counter

lists

  1. set a list: lpush mylist apple
    • get the list: lrange mylist 0 0
    • left push: lpush mylist banana
    • get the list at first index: lrange mylist 0 0
    • get the entire list: lrange mylist 0 1
    • right push: rpush mylist coconut
    • get the entire list: lrange mylist 0 2
    • pop from left lpop mylist
    • pop from right rpop mylist
    • more list commands

hashes

  1. set a hash hmset itinerary:1000 destination "New Zealand" duration 20 type nature
    • get the hash hgetall itinerary:1000
    • get specific hget itinerary:1000 destination
    • more hash commands

pubsub

  1. open one cli for subscribing

    sh $ redis-cli > subscribe hi history - open another cli for publishing

    sh $ redis-cli > publish hi bonjour - view the subscribe cli tab

with javascript

  1. install package npm i redis

    ```js var redis = require(‘redis’) var client = redis.createClient()

    client.set(‘hello’, ‘bonjour’, redis.print) ``` - check in the redis-cli: get hello - get the key in

    ```js var redis = require(‘redis’) var client = redis.createClient()

    client.set(‘hello’, ‘bonjour’, redis.print) client.get(‘hello’, function(error, reply) { console.log(reply) client.quit() }) ``` - clients in other programming languages

More Resources on Redis

  1. Redis cheatcheet

24 Ways for 2015!