Blog Archive » Getting Started: Redis and Python. Posted: March 22nd, 2010 | Author: Adam | Filed under: NoSQL, programming, Python, Redis | 16 Comments » So if you have been following NoSQL movement, the migration of some types of data to non-relational datastores has recently picked up speed.
For web (and other developers) this has lead to some impressive engineering resources developing some amazing tools being open sourced for the world to use. One that caught my eye recently has been Salvatore Sanfilippo’s redis which has been taken under the wing of VMWare, solidifying and validating the great work that Salavatore has done making redis an amazing tool in any developers arsenal. A very simplified explanation of redis is that it is an in memory key-value store like memcached but, it is persistent on disk, unlike memcached which is volatile. Along with being disk-persistent redis also supports some basic data structures like lists, sets, ordered sets, hashes, and of course basic key-value string storage like memcached. That’s it. EVAL. Introduction to EVAL EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter built into Redis starting from version 2.6.0.
The first argument of EVAL is a Lua 5.1 script. The script does not need to define a Lua function (and should not). It is just a Lua program that will run in the context of the Redis server. The second argument of EVAL is the number of arguments that follows the script (starting from the third argument) that represent Redis key names. All the additional arguments should not represent key names and can be accessed by Lua using the ARGV global variable, very similarly to what happens with keys (so ARGV[1], ARGV[2], ...). The following example should clarify what stated above: > eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second 1) "key1" 2) "key2" 3) "first" 4) "second" It is possible to call Redis commands from a Lua script using two different Lua functions: redis.call() redis.pcall() > eval "return redis.call('set','foo','bar')" 0 OK.
Starting with Redis tutorial. Install Download and run it: Redis Once the server is running you can use command line Redis utility to connect to it. redis-cli by default if will connect to the local Redis instance Let's build something We are going to use Redis to build a simple data model for storing user data, authenticating users, storing sessions, grouping users.
Storing user data First we probably want to have our users have unique IDs. Incr current_user_id this operation is atomic, so each client process will always get a different user id. User data can be stored in a number of ways in Redis. This approach is fine, however there are certain improvement that can be made. This approach will reduce memory overhead for keys we are storing. Hmget user 1 2 What if our user object is large? However let's consider a relatively large user object. We can store each user a separate hash.
Une courte introduction à Redis. Quand il s’agit de faire persister ses données, la seule option qui semble disponible est d’utiliser un SGBDR (une base de données relationnelle, telle que MySQL ou Oracle).
Mais on se rend parfois compte qu’un seul outil ne peut résoudre tous les problèmes. Ainsi, si on ne dispose que de marteaux, on aura tendance à voir des clous partout. Le mouvement NoSQL (comme Not Only SQL), nous propose des alternatives : bases clefs-valeurs, bases orientées document, bases orientées colonnes, bases orientées graphe ; Cassandra, MongoDB, Redis, Dynamo, Riak, Big Table, Voldemort sont souvent utilisés par les sites à gros trafic et à tendance « sociale » qui font le buzz : Google, Amazon, Facebook, Twitter, LinkedIn.
Aujourd’hui, nous allons explorer redis, ses principes, son API et ce qu’on peut en faire. Dans un prochain article, nous verrons une utilisation typique en écrivant un clone de twitter simple et sans prétention. String Listes Les listes de redis sont des listes liées (linked list).