---
title: "[redis]【转】Redis入门教程：特性及数据类型的操作"
author: "chuqingq"
date: "2014-12-20T10:14:04Z"
tags: []
categories: ["开源软件"]
slug: "redis-redis"
draft: false
lastmod: "2015-01-12T15:52:39Z"
id: 2168802
url: "https://chuqq.iteye.com/blog/2168802"
---

http://redis.readthedocs.org/en/latest/

    帮助：

        "help &lt;tab&gt;" to get a list of possible help topics 

        "help @&lt;group&gt;" to get a list of commands in &lt;group&gt; 例如help @generic/string/list/set/sorted\_set/hash/pubsub/transactions/connection/server/scripting/

        "help &lt;command&gt;" for help on &lt;command&gt; 例如help get/set等

    generic/key: del,dump,exists,expire/EXPIREAT,KEYS,MIGRATE,move,\*object,persist,pexpire/pexpireat,pttl/ttl,randomkey,rename/renamenx,\*restore,sort(list,set,sorted\_set),

    string: set/get,mset/mget,incr/decr,incrby/decrby,append,strlen,substr(SUBSTR name 0 3),

    list: lpush,llen,lrange( LRANGE students 0 2,全闭区间),lpop,lrem(LREM students 1 "John Doe"，删除),ltrim(LTRIM students 1 3)

    set: sadd/srem,smembers,sinter(交集),sunion(并集),sdiff(补集)

    sorted\_set: 多一个score属性，添加时就按这个score进行排序: zadd,zrange,zscore（根据value查询score）,zcount（查询start到stop的score中包含的个数）,zrangebyscore

    hash: hset/hget,hmset/hmget,hkeys/hvals

    pubsub（订阅）：subscribe/pulish,psubscribe/

    hyperloglog: 

    transaction:

        NX结尾命令都是判断在这个值没有时才进行某个命令，例如setnx是在没有值时才设置string

        getset，先get再set，返回的是get的值

        multi/exec，组合执行

        discard命令来中断执行中的命令序列，例如中断multi

    script:

    connection:

    server:

        Redis支持多个DB，默认是16个，你可以设置将数据存在哪一个DB中（select），不同DB间的数据具有隔离性。也可以在多个DB间移动数据（move）

        dbsize(数据项个数，例如一个set算一个数据项)/info,flushdb/flushall

 

    数据过期设置：

        用TTL命令可以获取某个key值的过期时间(-1表示永不过期)

        先用EXISTS命令查看key值是否存在，然后用expire设置了5秒后过期（或者准确的过期时间点）

    

    持久化：

        数据快照：save/bgsave,遍历所有数据保存到rdb中

        配置appendonly yes，所有的操作都会记录在aof日志文件（appendonly.aof）中

 

==============

 

转自：http://tech.it168.com/a2012/0109/1299/000001299837\_all.shtml

虽然Redis已经很火了，相信还是有很多同学对Redis只是有所听闻或者了解并不全面，下面是一个比较系统的Redis介绍，对Redis的特性及各种数据类型及操作进行了介绍。是一个很不错的Redis入门教程。

　　**一、介绍**

　　**1、Redis是什么**

　　**RE**mote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。Redis提供了一些丰富的数据结构，包括 lists, sets, ordered sets 以及 hashes ，当然还有和Memcached一样的 strings结构.Redis当然还包括了对这些数据结构的丰富操作。

　　**2、Redis的优点**

　　性能极高 – Redis能支持超过 100K+ 每秒的读写频率。

　　丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。

　　原子 – Redis的所有操作都是原子性的，同时Redis还支持对几个操作全并后的原子性执行。

　　丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。

　　二、**数据类型**

　　**1、String类型**

　　Redis能存储二进制安全的字符串，最大长度为1GB

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; GET name

　　"John Doe"

　　String类型还支持批量的读写操作

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; MSET age **30** sex "male"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; MGET age sex

　　**1**) "**30**"

　　**2**) "male"

　　String类型其实也可以用来存储数字，并支持对数字的加减操作。

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; INCR age

　　(integer) **31**

　　redis **127.0**.**0.1**:**6379**&gt; INCRBY age **4**

　　(integer) **35**

　　redis **127.0**.**0.1**:**6379**&gt; GET age

　　"**35**"

　　redis **127.0**.**0.1**:**6379**&gt; DECR age

　　(integer) **34**

　　redis **127.0**.**0.1**:**6379**&gt; DECRBY age **4**

　　(integer) **30**

　　redis **127.0**.**0.1**:**6379**&gt; GET age

　　"**30**"

　　String类型还支持对其部分的修改和获取操作

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; APPEND name " Mr."

　　(integer) **12**

　　redis **127.0**.**0.1**:**6379**&gt; GET name

　　"John Doe Mr."

　　redis **127.0**.**0.1**:**6379**&gt; STRLEN name

　　(integer) **12**

　　redis **127.0**.**0.1**:**6379**&gt; SUBSTR name **0** **3**

　　"John"

　　**2、List类型**

　　Redis能够将数据存储成一个链表，并能对这个链表进行丰富的操作：

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; LPUSH students "John Doe"

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; LPUSH students "Captain Kirk"

　　(integer) **2**

　　redis **127.0**.**0.1**:**6379**&gt; LPUSH students "Sheldon Cooper"

　　(integer) **3**

　　redis **127.0**.**0.1**:**6379**&gt; LLEN students

　　(integer) **3**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **2**

　　**1**) "Sheldon Cooper"

　　**2**) "Captain Kirk"

　　**3**) "John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; LPOP students

　　"Sheldon Cooper"

　　redis **127.0**.**0.1**:**6379**&gt; LLEN students

　　(integer) **2**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **1**

　　**1**) "Captain Kirk"

　　**2**) "John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; LREM students **1** "John Doe"

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; LLEN students

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **0**

　　**1**) "Captain Kirk"

　　Redis也支持很多修改操作：

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; LINSERT students BEFORE "Captain Kirk" "Dexter Morgan"

　　(integer) **3**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **2**

　　**1**) "Dexter Morgan"

　　**2**) "Captain Kirk"

　　**3**) "John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; LPUSH students "Peter Parker"

　　(integer) **4**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **3**

　　**1**) "Peter Parker"

　　**2**) "Dexter Morgan"

　　**3**) "Captain Kirk"

　　**4**) "John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; LTRIM students **1** **3**

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; LLEN students

　　(integer) **3**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **2**

　　**1**) "Dexter Morgan"

　　**2**) "Captain Kirk"

　　**3**) "John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; LREM students **1** "John Doe"

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; LLEN students

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; LRANGE students **0** **1**

　　**1**) "Captain Kirk"

 

　　**3、集合(Sets)类型**

　　Redis能够将一系列不重复的值存储成一个集合：

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SADD birds crow

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SADD birds pigeon

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SADD birds bat

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SADD mammals dog

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SADD mammals cat

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SADD mammals bat

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SMEMBERS birds

　　**1**) "bat"

　　**2**) "crow"

　　**3**) "pigeon"

　　redis **127.0**.**0.1**:**6379**&gt; SMEMBERS mammals

　　**1**) "bat"

　　**2**) "cat"

　　**3**) "dog"

　　Sets结构也支持相应的修改操作：

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SREM mammals cat

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SMEMBERS mammals

　　**1**) "bat"

　　**2**) "dog"

　　redis **127.0**.**0.1**:**6379**&gt; SADD mammals human

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SMEMBERS mammals

　　**1**) "bat"

　　**2**) "human"

　　**3**) "dog"

　　Redis还支持对集合的子交并补等操作：

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SINTER birds mammals

　　**1**) "bat"

　　redis **127.0**.**0.1**:**6379**&gt; SUNION birds mammals

　　**1**) "crow"

　　**2**) "bat"

　　**3**) "human"

　　**4**) "pigeon"

　　**5**) "dog"

　　redis **127.0**.**0.1**:**6379**&gt; SDIFF birds mammals

　　**1**) "crow"

　　**2**) "pigeon"

　　**4、有序集合(Sorted Sets)类型**

　　Sorted Sets和Sets结构相似，不同的是存在Sorted Sets中的数据会有一个score属性，并会在写入时就按这个score排好序。

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **0** mon

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **1** tue

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **2** wed

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **3** thu

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **4** fri

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **5** sat

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZADD days **6** sun

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; ZCARD days

　　(integer) **7**

　　redis **127.0**.**0.1**:**6379**&gt; ZRANGE days **0** **6**

　　**1**) "mon"

　　**2**) "tue"

　　**3**) "wed"

　　**4**) "thu"

　　**5**) "fri"

　　**6**) "sat"

　　**7**) "sun"

　　redis **127.0**.**0.1**:**6379**&gt; ZSCORE days sat

　　"**5**"

　　redis **127.0**.**0.1**:**6379**&gt; ZCOUNT days **3** **6**

　　(integer) **4**

　　redis **127.0**.**0.1**:**6379**&gt; ZRANGEBYSCORE days **3** **6**

　　**1**) "thu"

　　**2**) "fri"

　　**3**) "sat"

　　**4**) "sun"

　　**5、Hash类型**

　　Redis能够存储key对多个属性的数据(比如user1.uname user1.passwd)

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; HKEYS student

　　**1**) "name"

　　**2**) "age"

　　**3**) "sex"

　　redis **127.0**.**0.1**:**6379**&gt; HVALS student

　　**1**) "Ganesh"

　　**2**) "**30**"

　　**3**) "Male"

　　redis **127.0**.**0.1**:**6379**&gt; HGETALL student

　　**1**) "name"

　　**2**) "Ganesh"

　　**3**) "age"

　　**4**) "**30**"

　　**5**) "sex"

　　**6**) "Male"

　　redis **127.0**.**0.1**:**6379**&gt; HDEL student sex

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; HGETALL student

　　**1**) "name"

　　**2**) "Ganesh"

　　**3**) "age"

　　**4**) "**30**"

　　Hash数据结构能够批量修改和获取

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; HMSET kid name Akshi age **2** sex Female

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; HMGET kid name age sex

　　**1**) "Akshi"

　　**2**) "**2**"

　　**3**) "Female"

　　**3**.Publish/Subscribe

　　Redis支持这样一种特性，你可以将数据推到某个信息管道中，然后其它人可以通过订阅这些管道来获取推送过来的信息。

 

　　**三、订阅信息管道**

　　用一个客户端订阅管道

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SUBSCRIBE channelone

　　Reading messages... (press Ctrl-C to quit)

　　**1**) "subscribe"

　　**2**) "channelone"

　　**3**) (integer) **1**

　　另一个客户端往这个管道推送信息

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; PUBLISH channelone hello

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; PUBLISH channelone world

　　(integer) **1**

　　然后第一个客户端就能获取到推送的信息

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis 127.0.0.1:6379&gt; SUBSCRIBE channelone

　　Reading messages... (press Ctrl-C to quit)

　　1) "subscribe"

　　2) "channelone"

　　3) (integer) 1

　　1) "message"

　　2) "channelone"

　　3) "hello"

　　1) "message"

　　2) "channelone"

　　3) "world"

　　**2、按一定模式批量订阅**

　　用下面的命令订阅所有channel开头的信息通道

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; PSUBSCRIBE channel*

　　Reading messages... (press Ctrl-C to quit)

　　**1**) "psubscribe"

　　**2**) "channel\*"

　　**3**) (integer) **1**

　　在另一个客户端对两个推送信息

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; PUBLISH channelone hello

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; PUBLISH channeltwo world

　　(integer) **1**

　　然后在第一个客户端就能收到推送的信息

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; PSUBSCRIBE channel*

　　Reading messages... (press Ctrl-C to quit)

　　**1**) "psubscribe"

　　**2**) "channel\*"

　　**3**) (integer) **1**

　　**1**) "pmessage"

　　**2**) "channel\*"

　　**3**) "channelone"

　　**4**) "hello"

　　**1**) "pmessage"

　　**2**) "channel\*"

　　**3**) "channeltwo"

　　**4**) "world"

 

　　**四、数据过期设置**

　　Redis支持按key设置过期时间，过期后值将被删除(在客户端看来是补删除了的)

　　用TTL命令可以获取某个key值的过期时间(-1表示永不过期)

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; TTL name

　　(integer) -**1**

　　下面命令先用EXISTS命令查看key值是否存在，然后设置了5秒的过期时间

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; EXISTS name

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; EXPIRE name **5**

　　(integer) **1**

　　5秒后再查看

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; EXISTS name

　　(integer) **0**

　　redis **127.0**.**0.1**:**6379**&gt; GET name

　　(nil)

　　这个值已经没有了。

　　上在是直接设置多少秒后过期，你也可以设置在某个时间点过期，下面例子是设置2011-09-24 00:40:00过期。

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; EXPIREAT name **1316805000**

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; EXISTS name

　　(integer) **0**

　　**五、事务性**

　　Redis本身支持一些简单的组合型的命令，比如以NX结尾命令都是判断在这个值没有时才进行某个命令。

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; SETNX name "Dexter Morgan"

　　(integer) **0**

　　redis **127.0**.**0.1**:**6379**&gt; GET name

　　"John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; GETSET name "Dexter Morgan"

　　"John Doe"

　　redis **127.0**.**0.1**:**6379**&gt; GET name

　　"Dexter Morgan"

　　当然，Redis还支持自定义的命令组合，通过MULTI和EXEC，将几个命令组合起来执行

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET counter **0**

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; MULTI

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; INCR counter

　　QUEUED

　　redis **127.0**.**0.1**:**6379**&gt; INCR counter

　　QUEUED

　　redis **127.0**.**0.1**:**6379**&gt; INCR counter

　　QUEUED

　　redis **127.0**.**0.1**:**6379**&gt; EXEC

　　**1**) (integer) **1**

　　**2**) (integer) **2**

　　**3**) (integer) **3**

　　redis **127.0**.**0.1**:**6379**&gt; GET counter

　　"**3**"

　　你还可以用DICARD命令来中断执行中的命令序列

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET newcounter **0**

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; MULTI

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; INCR newcounter

　　QUEUED

　　redis **127.0**.**0.1**:**6379**&gt; INCR newcounter

　　QUEUED

　　redis **127.0**.**0.1**:**6379**&gt; INCR newcounter

　　QUEUED

　　redis **127.0**.**0.1**:**6379**&gt; DISCARD

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; GET newcounter

　　"**0**"

　　**六、持久化**

　　Redis的所有数据都存储在[内存](http://product.it168.com/list/b/0205_1.shtml "内存")中，但是他也提供对这些数据的持久化。

　　**1、数据快照**

　　数据快照的原理是将整个Redis中存的所有数据遍历一遍存到一个扩展名为rdb的数据文件中。通过SAVE命令可以调用这个过程。

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; SAVE

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; SET name "Sheldon Cooper"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; BGSAVE

　　Background saving started

　　如果你是使用的brew在Mac OSX上安全的Redis，那么rdb文件会存在如下路径

　　/usr/local/var/db/redis/dump.rdb

　　6.2 Append-Only File(追加式的操作日志记录)

　　Redis还支持一种追加式的操作日志记录，叫append only file，其日志文件以aof结局，我们一般各为aof文件。要开启aof日志的记录，你需要在配置文件中进行如下设置：

　　appendonly yes

　　这时候你所有的操作都会记录在aof日志文件中

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; GET name

　　(nil)

　　redis **127.0**.**0.1**:**6379**&gt; SET name "Ganesh Gunasegaran"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; EXIT

　　→ cat /usr/local/var/db/redis/appendonly.aof

　　\***2**

　　$**6**

　　SELECT

　　$**1**

　　**0**

　　\***3**

　　$**3**

　　SET

　　$**4**

　　name

　　$**18**

　　Ganesh Gunasegaran

　　**七、管理命令**

　　Redis支持多个DB，默认是16个，你可以设置将数据存在哪一个DB中，不同DB间的数据具有隔离性。也可以在多个DB间移动数据。

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SELECT **0**

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; SELECT **1**

　　OK

　　redis **127.0**.**0.1**:**6379**\[1]&gt; GET name

　　(nil)

　　redis **127.0**.**0.1**:**6379**\[1]&gt; SELECT **0**

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; MOVE name **1**

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SELECT **1**

　　OK

　　redis **127.0**.**0.1**:**6379**\[1]&gt; GET name

　　"John Doe"

　　Redis还能进行一些如下操作，获取一些运行信息

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**\[1]&gt; DBSIZE

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**\[1]&gt; INFO

　　redis\_version:**2.2**.**13**

　　redis\_git\_sha1:**00000000**

　　redis\_git\_dirty:**0**

　　arch\_bits:**64**

　　multiplexing\_api:kqueue

　　......

　　Redis还支持对某个DB数据进行清除(当然清空所有数据的操作也是支持的)

&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)  
http://www.CodeHighlighter.com/

--&gt;　　redis **127.0**.**0.1**:**6379**&gt; SET name "John Doe"

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; DBSIZE

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**&gt; SELECT **1**

　　OK

　　redis **127.0**.**0.1**:**6379**\[1]&gt; SET name "Sheldon Cooper"

　　OK

　　redis **127.0**.**0.1**:**6379**\[1]&gt; DBSIZE

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**\[1]&gt; SELECT **0**

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; FLUSHDB

　　OK

　　redis **127.0**.**0.1**:**6379**&gt; DBSIZE

　　(integer) **0**

　　redis **127.0**.**0.1**:**6379**&gt; SELECT **1**

　　OK

　　redis **127.0**.**0.1**:**6379**\[1]&gt; DBSIZE

　　(integer) **1**

　　redis **127.0**.**0.1**:**6379**\[1]&gt; FLUSHALL

　　OK

　　redis **127.0**.**0.1**:**6379**\[1]&gt; DBSIZE

　　(integer) **0**

　　**八、客户端**

　　Redis的客户端很丰富，几乎所有流行的语言都有其客户端，这里就不再赘述，有兴趣的同学可以上Redis的Clients页面去查找。

　　**九、资料引用**

　　Redis documentation：http://redis.io/documentation

　　Simon Willison – Redis tutorial：http://simonwillison.net/static/2010/redis-tutorial/

　　Michael J. Russo – Redis from ground up：http://blog.mjrusso.com/2010/10/17/redis-from-the-ground-up.html

　　**10.总结**

![数据过期设置、事务性及持久化](http://image20.it168.com/201201_500x375/918/df128a0295dbff0f.png)