This is a short introduction to NoSQL REDIS (key, value store) database I wrote a couple of months ago.
Includes the main commands REDIS offers plus how to set up a storage for Products in a similar way as data table.
Decarbonising Buildings: Making a net-zero built environment a reality
Redis
1. Redis
An open source, advanced key-value store. It is
often referred to as a data structure server since
keys can contain
strings, hashes, lists, sets and sorted sets.
http://redis.io
2. Redis – get it from
• Debian based Linux distros
– apt-get install…
• redis-server
• redis-doc
• Other Linux ditros
– Tar from http://redis.io
• Windows
– Many sources, though not official support
(google-it).
3. Redis
• Redis is running
$ ps -ef |grep redis
redis 1081 1 0 May11 ? 00:01:01 /usr/bin/redis-server /etc/redis/redis.conf
• Redis client / console
$ redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> ?
redis-cli 2.2.11
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
redis 127.0.0.1:6379> quit
$
4. Redis
• Redis commands
$ redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> SET server:name “test”
OK
redis 127.0.0.1:6379> GET server:name
“test”
redis 127.0.0.1:6379> DEL server:name
(integer) 1
redis 127.0.0.1:6379> GET server:name
(nil)
redis 127.0.0.1:6379>
$
5. Redis
• Redis more interesting commands
– INCR
– EXPIRE
– TTL
• Redis support for Lists:
– RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP
• Redis support for Sets (and Sorted sets):
– SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
• More at http://redis.io/documentation
6. Redis and Python
• Need to add the redis-py module
$ easy_install redis
• On the python console
>>> import redis
>>> r_server = redis.Redis("localhost")
>>> r_server.set(“whatever", “myTest")
True
>>> r_server.get(“whatever”)
'myTest'
7. Redis and Python
• Simple method to add an object to redis:
def save_to_redis(self):
"""Saves a product to the local redis database"""
r = redis.Redis("localhost")
if r.sadd("products", self.pId):
r.set("product:%s:category" % self.pId, self.cat)
r.set("product:%s:brand" % self.pId, self.brand)
r.set("product:%s:description" % self.pId, self.desc)
r.set("product:%s:price" % self.pId, self.price)
r.set("product:%s:url" % self.pId, self.url)
return True
else
return False
8. Redis and Python
• Simple method to read an object to redis:
@staticmethod
def get_product(param_id):
r = redis.Redis("localhost")
if r.sismember("products",param_id):
cat = r.get("product:%s:category" % param_id)
brand = r.get("product:%s:brand" % param_id).decode('utf-8')
desc = r.get("product:%s:description" % param_id).decode('utf-8')
price = r.get("product:%s:price" % param_id)
url = r.get("product:%s:url" % param_id).decode('utf-8')
return Product(param_id,cat,brand,desc,price,url)
9. Redis
• Back at the redis-cli :
$ redis-cli
redis 127.0.0.1:6379> SCARD products
(integer) 3
redis 127.0.0.1:6379> SMEMBERS products
1) "140274“
2) "115165“
3) "114278“
redis 127.0.0.1:6379> SISMEMBER products 115165
(integer) 1
redis 127.0.0.1:6379> keys product:140274:*
1) "product:140274:brand"
2) "product:140274:description"
3) "product:140274:url"
4) "product:140274:category"
5) "product:140274:price"
redis 127.0.0.1:6379> GET product:140274:description
"212 Glam Men Eau De Toilette Spray 100ml/3.4oz"