首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Redis(三)Redis Data Type and Specialty

2012-08-17 
Redis(3)Redis Data Type and SpecialtyRedis(3)Redis Data Type and Specialty1. Sorted Set TypeAlmost

Redis(3)Redis Data Type and Specialty
Redis(3)Redis Data Type and Specialty

1. Sorted Set Type
Almost the same as Set, but we can use a key score to order our data.
> zadd days 0 mon
(integer) 1
> zadd days 1 tue
(integer) 1
> zadd days 2 wed
(integer) 1
> zadd days 3 thu
(integer) 1
> zadd days 4 fri
(integer) 1
> zadd days 5 sat
(integer) 1
> zadd days 6 sun
(integer) 1
> zcard days
(integer) 7
> zrange days 0 6
1) "mon"
2) "tue"
3) "wed"
4) "thu"
5) "fri"
6) "sat"
7) "sun"
> zscore days sat
"5"
> zscore days mon
"0"
> zrangebyscore days 3 6
1) "thu"
2) "fri"
3) "sat"
4) "sun"

2. Hash Type
> hmset user1 username tester password 111111 age 30 gender 1
OK
> hgetall user1
1) "username"
2) "tester"
3) "password"
4) "111111"
5) "age"
6) "30"
7) "gender"
8) "1"
> hset user1 password 12345
(integer) 0
> hgetall user1
1) "username"
2) "tester"
3) "password"
4) "12345"
5) "age"
6) "30"
7) "gender"
8) "1"
> hkeys user1
1) "username"
2) "password"
3) "age"
4) "gender"
> hvals user1
1) "tester"
2) "12345"
3) "30"
4) "1"
> hdel user1 gender
(integer) 1
> hgetall user1
1) "username"
2) "tester"
3) "password"
4) "12345"
5) "age"
6) "30"

> HMSET kid-001 name Akshi age 2 sex Female
OK
> hmget kid-001 age name
1) "2"
2) "Akshi"

3. Publish/Subscribe
You can publish the data to one pipeline, others can subscribe these data.
One client can subscribe the channel
> subscribe ccav
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "ccav"
3) (integer) 1
1) "message"
2) "ccav"
3) "news1"
1) "message"
2) "ccav"
3) "news2"

The other client can publish the message
> publish ccav news1
(integer) 1
> publish ccav news2
(integer) 1

And we can subscribe a set of channels like this:
> psubscribe cc*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "cc*"
3) (integer) 1
1) "pmessage"
2) "cc*"
3) "cctv"
4) "news3"
1) "pmessage"
2) "cc*"
3) "ccav"
4) "news2"

> publish cctv news3
(integer) 1
> publish ccav news2
(integer) 1

4. Data Expiration
> set name "sillycat"
OK

ttl can check the expiration time, -1 means never.
> ttl name
(integer) -1

> exists name
(integer) 1

expire command can set the key=name with expiration time 5 seconds
> expire name 5
(integer) 1
> exists name
(integer) 0

There is also a command expireat, but I did not try that.

5. Check Suffix Command
NX will check if the key does not exist, then we will proceed the command.

> set name "karl"
OK
> setnx name "peter"
(integer) 0
> get name
"karl"

6. Other Operations
There is default 16 database in redis, we can select different databases as follow:
> select 0
OK
> set name karl
OK
> select 1
OK
>[1set name karl1
OK
>[1get name
"karl1"
>[1select 0
OK
> get name
"karl"

references:
http://blog.nosqlfan.com/html/3139.html
http://redis.io/topics/data-types



热点排行