一、介绍

  1、Redis是什么

  REmote 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

---

redis-cli.exe

set name "jiangqiqiang"

get name

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

mset age 30 sex "male"

mget age sex

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

incr age (age默认加1)

incrby age 4 (age加4)

decr age (age默认减1)

decrby age 4(age 减4)

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

append name "dddd"(在后面加dddd)/STRLEN name (长度)/substr name 0,3

2.list类型

Redis能够将数据存储成一个链表,并能对这个链表进行丰富的操作:(类似队列的概念)

list

lpush studens "aaaa"

lpush students "bbb"

lpush students "ccc"

这里会形成一个链表 : aaa->bbb->ccc

llen students (链表的长度)

lrange students 0 2 列出从第0个

lpop 弹出最后插入的元素  类似栈的概念

 Redis也支持很多修改操作:

LREM¶

LREM key count value
根据参数count的值,移除列表中与参数value相等的元素。

count的值可以是以下几种:
count > 0: 从表头开始向表尾搜索,移除与value相等的元素,数量为count。
count < 0: 从表尾开始向表头搜索,移除与value相等的元素,数量为count的绝对值。
count = 0: 移除表中所有与value相等的值。

ltrim 保留区间内的元素

 3、集合(Sets)类型

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

sadd birds crows

sadd birds pigeon

sadd birds bat

sadd mamals bat

sadd mamals cat

sadd mamals dog

smembers birds

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

SREM mamals cat  删除为cat的元素

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

SINTER birds mammals(交集)

  SUNION birds mammals (并集)

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

srem  birds bat

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

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

zadd days 1 sat

zcard days

zrange days 0 6

zscore days sat(得到sat 的key值)

zrangebyscore days 3 (通过key值来得到相应的value)

zcount days 3 6 (计算days值3 到6之间的值)

  5、Hash类型

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

hmset kid name Akshi age 3 sex Female (hash设置)

hmget kid name age sex

  三、订阅信息管道

  用一个客户端订阅管道

<!--

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

-->  redis 127.0.0.1:6379> SUBSCRIBE channelone

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

  1) "subscribe"

  2) "channelone"

  3) (integer) 1
  另一个客户端往这个管道推送信息

<!--

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

-->  redis 127.0.0.1:6379> PUBLISH channelone hello

  (integer) 1

  redis 127.0.0.1:6379> PUBLISH channelone world

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

<!--

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

-->  redis 127.0.0.1:6379> 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"

订阅消息:重新开一个客户端,类似隔空喊话一样的。

  2、按一定模式批量订阅

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

<!--

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

-->  redis 127.0.0.1:6379> PSUBSCRIBE channel*

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

  1) "psubscribe"

  2) "channel*"

  3) (integer) 1
  在另一个客户端对两个推送信息

<!--

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

-->  redis 127.0.0.1:6379> PUBLISH channelone hello

  (integer) 1

  redis 127.0.0.1:6379> PUBLISH channeltwo world

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

<!--

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

-->  redis 127.0.0.1:6379> 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表示永不过期)

  四、数据过期设置

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

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

<!--

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

-->  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> TTL name

  (integer) -1
  下面命令先用EXISTS命令查看key值是否存在,然后设置了5秒的过期时间

<!--

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

-->  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> EXISTS name

  (integer) 1

  redis 127.0.0.1:6379> EXPIRE name 5

  (integer) 1
  5秒后再查看

<!--

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

-->  redis 127.0.0.1:6379> EXISTS name

  (integer) 0

  redis 127.0.0.1:6379> GET name

  (nil)
  这个值已经没有了。

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

<!--

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

-->  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> EXPIREAT name 1316805000

  (integer) 1

  redis 127.0.0.1:6379> EXISTS name

  (integer) 0
  五、事务性

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

<!--

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

-->  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> SETNX name "Dexter Morgan"

  (integer) 0

  redis 127.0.0.1:6379> GET name

  "John Doe"

  redis 127.0.0.1:6379> GETSET name "Dexter Morgan"

  "John Doe"

  redis 127.0.0.1:6379> GET name

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

<!--

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

-->  redis 127.0.0.1:6379> SET counter 0

  OK

  redis 127.0.0.1:6379> MULTI

  OK

  redis 127.0.0.1:6379> INCR counter

  QUEUED

  redis 127.0.0.1:6379> INCR counter

  QUEUED

  redis 127.0.0.1:6379> INCR counter

  QUEUED

  redis 127.0.0.1:6379> EXEC

  1) (integer) 1

  2) (integer) 2

  3) (integer) 3

  redis 127.0.0.1:6379> GET counter

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

<!--

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

-->  redis 127.0.0.1:6379> SET newcounter 0

  OK

  redis 127.0.0.1:6379> MULTI

  OK

  redis 127.0.0.1:6379> INCR newcounter

  QUEUED

  redis 127.0.0.1:6379> INCR newcounter

  QUEUED

  redis 127.0.0.1:6379> INCR newcounter

  QUEUED

  redis 127.0.0.1:6379> DISCARD

  OK

  redis 127.0.0.1:6379> GET newcounter

  "0"
  六、持久化

  Redis的所有数据都存储在内存中,但是他也提供对这些数据的持久化。

  1、数据快照

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

<!--

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

-->  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> SAVE

  OK

  redis 127.0.0.1:6379> SET name "Sheldon Cooper"

  OK

  redis 127.0.0.1:6379> 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日志文件中

<!--

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

-->  redis 127.0.0.1:6379> GET name

  (nil)

  redis 127.0.0.1:6379> SET name "Ganesh Gunasegaran"

  OK

  redis 127.0.0.1:6379> 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间移动数据。

<!--

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

-->  redis 127.0.0.1:6379> SELECT 0

  OK

  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> SELECT 1

  OK

  redis 127.0.0.1:6379[1]> GET name

  (nil)

  redis 127.0.0.1:6379[1]> SELECT 0

  OK

  redis 127.0.0.1:6379> MOVE name 1

  (integer) 1

  redis 127.0.0.1:6379> SELECT 1

  OK

  redis 127.0.0.1:6379[1]> GET name

  "John Doe"
  Redis还能进行一些如下操作,获取一些运行信息

<!--

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

-->  redis 127.0.0.1:6379[1]> DBSIZE

  (integer) 1

  redis 127.0.0.1:6379[1]> INFO

  redis_version:2.2.13

  redis_git_sha1:00000000

  redis_git_dirty:0

  arch_bits:64

  multiplexing_api:kqueue

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

<!--

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

-->  redis 127.0.0.1:6379> SET name "John Doe"

  OK

  redis 127.0.0.1:6379> DBSIZE

  (integer) 1

  redis 127.0.0.1:6379> SELECT 1

  OK

  redis 127.0.0.1:6379[1]> SET name "Sheldon Cooper"

  OK

  redis 127.0.0.1:6379[1]> DBSIZE

  (integer) 1

  redis 127.0.0.1:6379[1]> SELECT 0

  OK

  redis 127.0.0.1:6379> FLUSHDB

  OK

  redis 127.0.0.1:6379> DBSIZE

  (integer) 0

  redis 127.0.0.1:6379> SELECT 1

  OK

  redis 127.0.0.1:6379[1]> DBSIZE

  (integer) 1

  redis 127.0.0.1:6379[1]> FLUSHALL

  OK

  redis 127.0.0.1:6379[1]> DBSIZE

  (integer) 0

  八、客户端

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

redis常见命令的更多相关文章

  1. redis常见命令使用

    这篇经验主要介绍了Redis常见用的一些操作命令.这篇例子是在windows上操作的.linux类似.写的一些基础,大神就别看了. 工具/原料   redis windows 方法/步骤   1 可以 ...

  2. Redis 常见命令

    0. 5种数据类型 String(字符串) List(列表) Hash(字典) Set(集合) Sorted Set(有序集合) 1. String 字符串 set key value 设置key=v ...

  3. redis常见错误处理

    --1]当内存不足引起  redis出错 先尝试下列语句,指定redis使用内存 redis-server.exe redis.windows.conf --maxheap 200mredis-ser ...

  4. redis(2)---redis基本数据类型及常见命令

    Redis的魅力 缓存大致可以分为两类,一种是应用内缓存,比如Map(简单的数据结构),以及EH Cache(Java第三方库),另一种就是缓存组件,比如Memached,Redis:Redis(re ...

  5. [Linux基础]Linux基础知识入门及常见命令.

    前言:最近刚安装了Linux系统, 所以学了一些最基本的操作, 在这里把自己总结的笔记记录在这里. 1,V8:192.168.40.10V1:192.168.40.11Linux ip:192.168 ...

  6. Redis常见七种使用场景(PHP实战)

    edis 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 本篇文章,主要介绍利用Redis常见应用场景下PHP实战. ...

  7. Redis学习笔记(3)——Redis的命令大全

    Redis是一种nosql数据库,常被称作数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted se ...

  8. Redis常见使用说明

    1 概述Remote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写. ...

  9. Redis常用命令详细介绍

    一.字符串 字符串键是Redis最基本的键值对类型,将一个单独的键和一个单独的值关联起来.通过字符串键,不仅可以存储和读取字符串,如果输入能被解释为整数和浮点数,还能执行自增或自减操作. 1.SET: ...

随机推荐

  1. Spring JDBC 随笔

    Spring 框架,借助 JdbcTemplate 类来简化 java 访问 database. 完成一个增查改删(CRUD)的基本功能,借助下面 5 个角色来共同来完成. 1. object cla ...

  2. 如何给DropDownList控件设置样式(ASP.NET MVC)

    前话: 应学校领导要求,要给后台管理系统添加一个搜索功能,提供可选择选项.我选择使用DropDownList去实现,熟悉.net控件的都知道,DropDownList的样子非常丑,不论是边框长宽还是里 ...

  3. JavaScript中判断对象类型的种种方法

    我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...

  4. spring mvc事务注解

    @Transactional(noRollbackFor=RuntimeException.class)方法事务说明@Transactional(RollbackFor=Exception.class ...

  5. 深入分析C++引用

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 关于引用和指针的差别的文章非常多非 ...

  6. JuiceSSh破解分析

    JuiceSSH是一款免费的远程ssh客户端,感觉是一款挺优秀的软件,里边有一些高级功能需要购买高级版才能使用,这里便对其对高级功能的破解进行分析. 本文仅用于学习交流使用,请尊重作者,勿在网上肆意发 ...

  7. java socker编程

    转自http://haohaoxuexi.iteye.com/blog/1979837 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户 ...

  8. Chapter 7. Dependency Management Basics 依赖管理基础

    This chapter introduces some of the basics of dependency management in Gradle. 7.1. What is dependen ...

  9. linux常见设备类型及文件系统

    As you can see in  Table   14.3   , all disk device names end with the letter a. That is because it ...

  10. Windows系统下安装Python的SSH模块教程

    Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是 ...