Try Redis


redis 官网,   github上的 网址
redis 命令参考,  github 地址
redis3.0 源码注释

以下摘自维基百科

          Redis是一个开源、支持网络、基于内存、键值对存储数据库,使用ANSI C编写。从2013年5月开始,Redis的开发由Pivotal赞助。在这之前,其开发由VMware赞助。根据月度排行网站DB-Engines.com的数据显示,Redis是最流行的键值对存储数据库.

  • 支持的语言

许多语言都包含Redis支持,包括:


  • 数据模型

Redis的外围由一个键、值映射的字典构成。与其他非关系型数据库主要不同在于:Redis中值的类型不仅限于字符串,还支持如下抽象数据类型:

值的类型决定了值本身支持的操作。Redis支持不同无序、有序的列表,无序、有序的集合间的交集、并集等高级服务器端原子操作。

  • 持久化

Redis通常将全部的数据存储在内存中。2.4版本后可配置为使用虚拟内存,一部分数据集存储在硬盘上,但这个特性废弃了。

目前通过两种方式实现持久化

  • 使用快照,一种半持久耐用模式。不时的将数据集以异步方式从内存以RDB格式写入硬盘。
  • 1.1版本开始使用更安全的AOF格式替代,一种只能追加的日志类型。将数据集修改操作记录起来。Redis能够在后台对只可追加的记录作修改来避免無限增长的日志。
  • 同步

Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。从盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

  • 性能
当数据依赖不再需要,Redis这种基于内存的性质,与在执行一个事务时将每个变化都写入硬盘的数据库系统相比就显得执行效率非常高。写与读操作速度没有明显差别。

  • 使用redis的公司

try redis 网址

  • Use the command SET to store the value "fido" at key "server:name"
> SET server:name "fido"
OK
> GET server:name
"fido"
  • Use the command DET to delete a given key and associated value, INCR to atomically increment a number stored at a given key
> set connection 10
OK
> incr connection
(integer) 11
> incr connection
(integer) 12
> del connection
(integer) 1
> incr connection
(integer) 1
  • Redis can be told that a key should only exit for a certain length of time. This is accomplished with the EXPIRE and TTL commands.
> SET resource:lock "Redis Demo"
OK
> EXPIRE resource:lock 120
(integer) 1
> TTL resource:lock
(integer) 25
> TTL resource:lock
(integer) -2
  • RPUSH puts the new value at the end of the list, LPUSH puts the new value at the start of the list, LRANGE gives a subset of the list.
> RPUSH friends "Alice"
(integer) 1
> RPUSH friends "Bob"
(integer) 2
> LPUSH friends "Sam"
(integer) 3
> LRANGE friends 0 -1
1) "Sam"
2) "Alice"
3) "Bob"
> LRANGE friends 0 1
1) "Sam"
2) "Alice"
> LRANGE friends 1 2
1) "Alice"
2) "Bob"
  • LLEN returns the current length of the list, LPOP removes the first element from the list and returns it, RPOP removes the last element from the list and returns it
> LLEN friends
(integer) 3
> LPOP friends
"Sam"
> RPOP friends
"Bob"
> LLEN friends
(integer) 1
> LRANGE friends 0 -1
1) "Alice"
  • SADD adds the given value to the set, SREM removes the given value from the set
> SADD superpowers "flight"
(integer) 1
> SADD superpowers "x-ray vision"
(integer) 1
> SADD superpowers "reflexes"
(integer) 1
> SREM superpowers "reflexes"
1

  • SISMEMBER tests if the given value is in the set, SMEMBERS returns a list of all the members of this set, SUNION combines two or more sets and returns the list of all members.
> SISMEMBER superpowers "flight"
(integer) 1
> SISMEMBER superpowers "reflexes"
(integer) 0
> SMEMBERS superpowers
1) "x-ray vision"
2) "flight"
> SADD birdpowers "pecking"
(integer) 1
> SADD birdpowers "flight"
(integer) 1
> SUNION superpowers birdpowers
1) "pecking"
2) "flight"
3) "x-ray vision"
  • A sorted set 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"
(integer) 1
> ZADD hackers 1906 "Grace Hopper"
(integer) 1
> ZADD hackers 1953 "Richard Stallman"
(integer) 1
> ZADD hackers 1969 "Linus Torvalds"
(integer) 1
> ZADD hackers 1965 "Yukihiro Matsumoto"
(integer) 1
> ZADD hackers 1916 "Claude Shannon"
(integer) 1
> ZADD hackers 1957 "Sophie Wilson"
(integer) 1
> ZADD hackers 1912 "Alan Turing"
(integer) 1
> ZRANGE hackers 2 4
1) "Claude Shannon"
2) "Alan kay"
3) "Richard Stallman"
  • Hashes are maps between string fields and string values, so they are theperfect data type to represent objects (eg: A User with a number of fields likename, surname, age, and so forth)
> HSET user:1000 name "John Smith"
(integer) 1
> HSET user:1000 email "john.smith@example.com"
> HSET user:1000 password "s3cret"
(integer) 1
(integer) 1
> HGETALL user:1000
1) "name"
2) "John Smith"
3) "email"
4) "john.smith@example.com"
5) "password"
6) "s3cret"
> HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"
OK
> HGET user:1001 name
"Mary Jones"
Numerical values in hash fields are handled exactly the same as in simple stringsand there are operations to increment this value in an atomic way.
> HSET user:1000 visits 10
(integer) 1
> HINCRBY user:1000 visits 1
(integer) 11
> HINCRBY user:1000 visits 10
(integer) 21
> HDEL user:1000 visits
(integer) 1
> HINCRBY user:1000 visits 1
(integer) 1



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值