`
eyesmore
  • 浏览: 376873 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

redis入门介绍

阅读更多

Redis入门简介
深入了解redis请阅读官方文档:redis documentation
http://redis.io/documentation 

再深入了解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:

经过上面的安装后,二进制的可执行文件就生成了,就放在src子目录下(src: script脚本的意思),这点又很简单,并没有放到/usr/local/bin之类的目录下,就感觉不需要懂别的,光用redis就可以了。

$ src/redis-server
注意:这种启动是在前台启动的,不是后台。

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/ 

顺便说下,目前redis还不支持集群,但是支持主从复制,redis开发人员说预计在redis2.6版本将支持集群。

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.


    SET resource:lock "Redis Demo 1"
    EXPIRE resource:lock 120
    TTL resource:lock => 119
    SET resource:lock "Redis Demo 2"
TTL resource:lock => -1

摘要:
1、    redis默认的set是不过期的;
2、    过期时间单位是:秒,不是毫秒,这点跟memcached一样,很容易理解“毫秒”没有意义,毕竟redis是网络操作的。
3、    Redis的过期时间不仅可以设置,还可以查询什么时候会过期。而且,redis的过期时间,可以随时更新。
4、    Redis的过期时间检测采用两种技术:Lazy Expiration,既读取的时候判断是否过期;和利用监控线程,周期性的对keys进行抽样检测,发现过期的,就删除。这么说来,如果你要捕获过期事件,是没有严格意义的。

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.


    LRANGE friends 0 -1 => ["Sam","Tom","Bob"]
    LRANGE friends 0 1 => ["Sam","Tom"]
    LRANGE friends 1 2 => ["Tom","Bob"]

摘要:
1、    LIST提供“左边插入”,“左边移除”,“右边插入”,“右边移除”的操作;
2、    LIST还提供“子集查询”,通过LRANGE命令可以任意查看LIST的任意连续子序列。编号从0开始,-1表示结尾。区间是:双闭区间。
3、    LIST的应用场景:SNS中的好友列表。比如新浪微博的“粉丝”就可以用redis的LIST来CACHE,以减少对DB的压力。在Redis出现前,我们要实现列表CACHE是很麻烦的,比如memcached如果缓存了用户 uid的好友列表fids={f1,f2,…,fn};如果某个时候用户uid删除了某个好友,这个时候我们需要更新列表,而memcached的key-value的value数据结构太笨了,或者应该说value是无结构的,要删除其中的fids={f1,f2,…,fn}是很费劲的。
4、    后面我们还会继续谈到:redis基于集合,还有:交集,并集,差集运算。这样新浪微博的“共同好友”就太简单了,就是两个用户好友列表的交集运算。

排重(集合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.


    SADD birdpowers "pecking"
    SADD birdpowers "flight"
    SUNION superpowers birdpowers => ["flight","x-ray vision","pecking"]
   
有序集(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.


ZRANGE hackers 2 4 => ["Alan Kay","Richard Stallman","Yukihiro Matsumoto"]

Redis官方文档
http://redis.io/documentation

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?

附录:Redis命令大全
http://redis.io/commands

分享到:
评论

相关推荐

    Redis 入门指南.pdf

    本指南将帮助初学者理解 Redis 的基本概念、安装与配置、数据类型以及常用命令,从而快速入门 Redis。 1. **Redis 简介** - Redis 源自意大利程序员 Salvatore Sanfilippo,最初是为了提高网站性能而设计的。 - ...

    REDIS 入门指南pdf

    以下是对 Redis 入门指南的一些关键知识点的详细说明: 1. **Redis 数据类型** - **字符串(String)**:Redis 最基本的数据类型,可以存储字符串、数字等,支持自增、自减操作。 - **哈希(Hash)**:用于存储...

    Redis入门指南(第2版)

    本书是一本Redis入门指导书籍,以通俗易懂的方式介绍了Redis基础与实践方面的知识,包括历史特性、在开发和生产环境中部署运行Redis、数据类型与命令、使用Redis实现队列、事务、复制、管道、持久化、优化Redis存储...

    Redis入门到精通最新教学视频

    Redis入门到精通最新教学视频!!!!!!!!!!!!!!!!!!!

    Redis入门指南(第2版)电子书

    《Redis入门指南(第2版)》是一本Redis的入门指导书籍,以通俗易懂的方式介绍了Redis基础与实践方面的知识,包括历史与特性、在开发和生产环境中部署运行Redis、数据类型与命令、使用Redis实现队列、事务、复制、...

    redis入门指南

    《Redis入门指南》是一本Redis的入门指导书籍,以通俗易懂的方式介绍了Redis基础与实践方面的知识,包括历史与特性、在开发和生产环境中部署运行Redis、数据类型与命令、使用Redis实现队列、事务、复制、管道、持久...

    Redis入门指南

    《Redis入门指南》是一本Redis的入门指导书籍,以通俗易懂的方式介绍了Redis基础与实践方面的知识,包括历史与特性、在开发和生产环境中部署运行Redis、数据类型与命令、使用Redis实现队列、事务、复制、管道、持久...

    redis入门指南2

    《Redis入门指南(第2版)》是一本Redis的入门指导书籍,以通俗易懂的方式介绍了Redis基础与实践方面的知识,包括历史与特性、在开发和生产环境中部署运行Redis、数据类型与命令、使用Redis实现队列、事务、复制、...

    Redis 入门指南

    Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南Redis 入门指南

    redis入门指南 高清PDF

    redis 入门教程,高清扫描版。免费下载、

    Redis入门简单实例

    Redis入门简单实例,附带详细说明,代码可直接运行,欢迎交流。

    Redis入门指南 第2版 高清PDF完整扫描版.pdf 【带书签】

    Redis入门指南 第2版 高清PDF完整扫描版.pdf 。搜集整理自互联网,仅供个人学习,严禁用于商业用途,如有版权问题,请联系删除。

    Redis入门指南 - 李子骅.mobi

    《Redis入门指南》是一本Redis的入门指导书籍,以通俗易懂的方式介绍了Redis基础与实践方面的知识,包括历史与特性、在开发和生产环境中部署运行Redis、数据 ...

Global site tag (gtag.js) - Google Analytics