再深入了解redis阅读源代码分析:redis under the hood
http://pauladamsmith.com/articles/redis-under-the-hood.html
1、认识redis
http://redis.io/
Redis is 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.
2、下载
Redis 2.4.7 is the latest stable version.
当前版本:2.4.7 http://redis.googlecode.com/files/redis-2.4.7.tar.gz
代码还是存放在googlecode,但是建设自己的门户了,便于宣传。如果代码放自己站点,不然没有放googlecode那么好的下载性能了(这玩意儿跟我们很多人挂视频,都利用youku的站外播放器一样)。
Other downloads are available on GitHub and Google Code.
3、安装
http://redis.io/download
Installation
Download, extract and compile Redis with:
$ wget http://redis.googlecode.com/files/redis-2.4.7.tar.gz
$ tar xzf redis-2.4.7.tar.gz
$ cd redis-2.4.7
$ make
你发现原来安装这么简单,甚至都不用configure和make install,怎么这么简单呢?!
4、启动
The binaries that are now compiled are available in the src directory. Run Redis with:
You can interact with Redis using the built-in client:
Redis多周到,客户端命令行工具都给你准备了,马上就可以体验。
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
4、教程
Are you new to Redis? Try our online, interactive tutorial.
Ready for a test drive? Check this interactive tutorial that will walk you through the most important features of Redis.
教程地址: http://try.redis-db.com/
计数器(INCR累加命令:原子操作,防“Lost Update”)
There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as:
x = GET count
x = x + 1
SET count x
The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time:
Client A reads count as 10.
Client B reads count as 10.
Client A increments 10 and sets count to 11.
Client B increments 10 and sets count to 11.
备注:这个是典型的Lost Update问题。原子累加命令INCR,也是为什么redis适合应用在“计数器”的应用中,比如:PV统计计数,播放量采集。
We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation. Redis provides many of these atomic operations on different types of data.
缓存(过期时间的设置和查询)
Redis can be told that a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands.
SET resource:lock "Redis Demo"
EXPIRE resource:lock 120
This causes the key resource:lock to be deleted in 120 seconds. You can test how long a key will exist for with the TTL command. It returns the number of seconds until it will be deleted.
TTL resource:lock => 113
TTL count => -1
The -1 for the TTL of the key count means that it will never expire. Note that if you SET a key, its TTL will reset.
SNS好友列表(LIST或队列集合操作)
Redis also supports several more complex data structures. The first one we'll look at is a list. A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. You can immediately begin working with a key as a list, as long as it doesn't already exist as a different type.
RPUSH puts the new value at the end of the list.
RPUSH friends "Tom"
RPUSH friends "Bob"
LPUSH puts the new value at the start of the list.
LPUSH friends "Sam"
LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve all elements in the list.
排重(集合SET:无序,不重复)
The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
SADD adds the given value to the set.
SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"
SREM removes the given value from the set.
SREM superpowers "reflexes"
SISMEMBER tests if the given value is in the set.
SISMEMBER superpowers "flight" => true
SISMEMBER superpowers "reflexes" => false
SMEMBERS returns a list of all the members of this set.
SMEMBERS superpowers => ["flight","x-ray vision"]
SUNION combines two or more sets and returns the list of all elements.
有序集(SortedSet:LIST和SET的折中)
The last data structure which Redis supports is the sorted set. It is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.
ZADD hackers 1940 "Alan Kay"
ZADD hackers 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1912 "Alan Turing"
In these examples, the scores are years of birth and the values are the names of famous hackers.
Topics
? Data types: a summary of the different types of values that Redis supports.
? Replication: what you need to know in order to set up master-slave replication.
? Persistence: know your options when configuring Redis' durability.
? Virtual memory: when your dataset doesn't fit in RAM, you can use VM.
? Pipelining: learn how to send multiple commands at once, saving on round trip time.
? Redis Pub/Sub: Redis is a fast and stable Publish/Subscribe messaging system! Check it out.
? Memory optimization: understand how Redis uses RAM and learn some tricks to use less of it.
? High latency troubleshooting: read this document if you want to understand the possible causes of high latency in Redis.
? Redis Administration: selected administration topics.
? Benchmarks: see how fast Redis is in different platforms.
? FAQ: some common questions about Redis.
? Protocol specification: if you're implementing a client, or out of curiosity, learn how to communicate with Redis at a low level.
? Debugging Redis: in the unlikely event you are experiencing a Redis crash, you can use this guide to send the right information to the Redis Core Team.
? Internals: learn details about how Redis is implemented under the hood.
? Who's using it?