日期:2014-05-16  浏览次数:20499 次

Redis(2)Introduction for Redis and its concept String List Set
Redis(2)Introduction for Redis and its concept String List Set

1. Introduction
Redis --- REmote DIctionary Server is a key value store system.

Benefit of Redis:
High Performance ----> 100k per second
Support many types ----> Strings, Lists, Hashes, Sets and Ordered Sets

2. Data Type
String Type
>SET name "sillycat"
OK
>GET name
"sillycat"

>mset age 30 sex "male"
OK
>mget age sex name
1) "sillycat"
2) "male"
3) "30"
>get age
30

We can store number here, and we can do operation to it.
> incr age
(integer) 31
> get age
"31"
> incrby age 3
(integer) 34
> decr age
(integer) 33
> decrby age 2
(integer) 31

We can also do some operation on string
> APPEND name " Mr."
(integer) 12
(0.53s)
> get name
"sillycat Mr."
> strlen name
(integer) 12
> substr name 0 3
"sill"
> get name
"sillycat Mr."

List Type
> LPUSH students "kiko"
(integer) 1
> LPUSH students "karl"
(integer) 2
> LPUSH students "peter"
(integer) 3
> LLEN students
(integer) 3
> LRANGE students 0 2
1) "kiko"
2) "karl"
3) "peter"
> LPOP students
"peter"
> LLEN students
(integer) 2
> LRANGE students 0 1
1) "kiko"
2) "karl"
> LREM students 1 "kiko"   
# remove the element, key=1 > 0, so, that means remove from the left and remove 1 element matched.
# key=0, means remove all
# key<0 = -2, means remove from the right and remove 2 elements matched
(integer) 1
> LLEN students
(integer) 1
> LRANGE students 0 0
1) "karl"

> lpush jobs "java"
(integer) 1
> lpush jobs "python"
(integer) 2
> lpush jobs "groovy"
(integer) 3
> lrange jobs 0 2
1) "groovy"
2) "python"
3) "java"
> linsert jobs before "python" "scala"
(integer) 4
> lrange jobs 0 3
1) "groovy"
2) "scala"
3) "python"
4) "java"
> ltrim jobs 1 3
OK
> llen jobs
(integer) 3
> lrange jobs 0 2
1) "scala"
2) "python"
3) "java"

Set Type
> sadd lans java
(integer) 1
> sadd lans python
(integer) 1
> sadd lans groovy
(integer) 1
> sadd lans ruby
(integer) 1

> sadd coders daxiong
(integer) 1
> sadd coders sillycat
(integer) 1
> sadd coders gongda
(integer) 1
> sadd coders gongshao
(integer) 1

> smembers lans
1) "ruby"
2) "groovy"
3) "python"
4) "java"
> smembers coders
1) "gongda"
2) "sillycat"
3) "gongshao"
4) "daxiong"

We can operate on these sets. For example, remove one.
> srem lans java
(integer) 1
> smembers lans
1) "ruby"
2) "groovy"
3) "python"

Operation on Collection
> smembers coders
1) "gongda"
2) "sillycat"
3) "gongshao"
4) "daxiong"
> smembers youngguys
1) "peter"
2) "gongshao"

> sinter coders youngguys
1) "gongshao"
> sunion coders youngguys
1) "sillycat"
2) "daxiong"
3) "gongshao"
4) "gongda"
5) "peter"
> sdiff coders youngguys
1) "gongda"
2) "sillycat"
3) "daxiong"


references:
http://krams915.blogspot.com/2012/02/spring-mvc-31-implement-crud-with_6764.html
http://redis.io/documentation
http://redis.io/topics/data-types-intro
http://redis.io/topics/data-types
http://blog.nosqlfan.com/html/3139.html
http://ma