redis

官方tutorial:try redis

命令:

SET
GET
DEL
INCR(原子操作,避免出现两个clinet分别加1,但数据只加1的情况,INCR保证了原子性)
SETNX(SET-if-not-exists)

设置生存时间:

SET resource:lock "Redis Demo"
EXPIRE resource:lock 120//120s后死亡

//用TTL检测
TTL resource:lock => 113
// after 113s
TTL resource:lock => -2

//-2代表不存在,-1代表永远不会过期,-1如下

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.list
相关操作:

RPUSH(push to right)
LPUSH(push to left)
LLEN(length)
LRANGE(sublist)
LPOP
RPOP

2.sets

SADD
SREM
SISMEMBER(if or not is a member,1 for true,0 for false)
SMEMBERS(return a list of all members)
SUNION(union two or more sets and return the list of all members)

3.sorted sets

传统的sets没有order,sorted sets每个value都有一个对应的score,通过score来进行排序,相对于java中的comparator吧。

ZADD hackers 1940 "Alan Kay"
ZADD hackers 1906 "Grace Hopper"
ZADD hackers 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1957 "Sophie Wilson"
ZADD hackers 1912 "Alan Turing"
ZRANGE hackers 2 4 => 1) "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman"

4.Hashes

HSET user:1000 name "John Smith"
HSET user:1000 email "john.smith@example.com"
HSET user:1000 password "s3cret"

//get all
HGETALL user:1000

//get one field
HGET user:1001 name => "Mary Jones"

更多关于hashes的命令hashes

tutorial太简单,更多参考:
1.redis doc
2.关于redis的data types
3.关于命令commands

先来看看FAQ

1.redis的不同

1.Redis is a different evolution path in the key-value DBs where values can contain more complex data types, with atomic operations defined on those data types.可以在复杂对象上进行原子操作。

2.Redis is an in-memory but persistent on disk database, so it represents a different trade off where very high write and read speed is achieved with the limitation of data sets that can’t be larger than memory.redis是一种内存数据库,并不是硬盘数据库,这样可以实现高速。

2.那么数据在内存中到底大小如何呢?

An empty instance uses ~ 1MB of memory.
1 Million small Keys -> String Value pairs use ~ 100MB of memory.
1 Million Keys -> Hash value, representing an object with 5 fields, use ~ 200 MB of memory.

因为是内存数据库,所以如果要存储的内容超过内存大小,那么肯定没有办法咯。

3.Is using Redis together with an on-disk database a good idea?

Yes, a common design pattern involves taking very write-heavy small data in Redis (and data you need the Redis data structures to model your problem in an efficient way), and big blobs of data into an SQL or eventually consistent on-disk database.

通常的做法就是将频繁项放在redis中,其他数据放在sql,这不就是缓存么,呵呵。

4.Redis is single threaded. How can I exploit multiple CPU / cores?

It’s very unlikely that CPU becomes your bottleneck with Redis, as usually Redis is either memory or network bound. 通常CPU不会成为瓶颈,内存和网络才是。

关于data types:

Redis is not a plain key-value store, it is actually a data structures server, supporting different kinds of values. What this means is that, while in traditional key-value stores you associated string keys to string values, in Redis the value is not limited to a simple string, but can also hold more complex data structures.几种数据结构:

1.Strings
2.Lists
3.Sets
4.Hashes
5.Sorted sets
6.Bitmaps and HyperLogLogs

都是很简单的数据结构,怎样使用看具体的commands reference就行了。

1.Redis Mass Insertion

1.Use the protocol, Luke

Using a normal Redis client to perform mass insertion is not a good idea for a few reasons: the naive approach of sending one command after the other is slow because you have to pay for the round trip time for every command.

For all this reasons the preferred way to mass import data into Redis is to generate a text file containing the Redis protocol, in raw format, in order to call the commands needed to insert the required data.

将需要insert的命令写入data.txt,比如:

SET Key0 Value0
SET Key1 Value1
...
SET KeyN ValueN

键入命令:cat data.txt | redis-cli --pipe

2.Using Redis as an LRU cache

最大内存配置:

It is possible to set the configuration directive using the 1.redis.conf file, or later using the 2.CONFIG SET command at runtime.在conf file中写入:maxmemory 100mb

驱逐策略:

当达到最大内存时,使用何种策略来清除旧数据。

1.noeviction
2.allkeys-lru
3.volatile-lru
4.allkeys-random
5.volatile-random
6.volatile-ttl

运行步骤:

It is important to understand that the eviction process works like this:
1.A client runs a new command, resulting in more data added.
2.Redis checks the memory usage, and if it is greater than the maxmemory limit , it evicts keys according to the policy.
3.A new command is executed, and so forth.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值