Interacting with data in Redis
Redis is an advanced kind of key-value store where the values can be of different types: string, list, set, sorted set or hash. Redis stores data in memory like memcached but it can be persisted on disk, unlike memcached, which has no such option. Redis supports fast reads and writes, in the order of 100,000 set or get operations per second.
To interact with Redis, we need to install the Redis-py
module to Python, which is available on pypi
and can be installed with pip
:
$ pip install redis
Now, we can connect to Redis via the host and port of the DB server. We assume that we have already installed a Redis server, which is running with the default host (localhost
) and port (6379
) parameters:
>>> import redis >>> r = redis.StrictRedis(host='127.0.0.1', port=6379) >>> r StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
As a first step to storing data in Redis, we need to define which kind of data...