redis笔记(String)

由于memcache的局限性,诞生了redis,相比较memcache中的value,redis中的value有了数据类型的区分,redis对每种类型都有响应的操作方法,使用起来更加灵活。目前redis是最流行的内存nosql数据库。

数据类型

Image for post

String

Redis中支持的String包含字符,数值,bitmaps。可以通过OBJECT命令查看encoding。

127.0.0.1:6379> OBJECT help
1) OBJECT <subcommand> arg arg ... arg. Subcommands are:
2) ENCODING <key> -- Return the kind of internal representation used in order to store the value associated with a key.
3) FREQ <key> -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key.
4) IDLETIME <key> -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key.
5) REFCOUNT <key> -- Return the number of references of the value associated with the specified key.
127.0.0.1:6379> set key1 1
OK
127.0.0.1:6379> object encoding key1
"int"
GET key
summary: Get the value of a key
since: 1.0.0

SET key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
127.0.0.1:6379> set rediskey 'hello world'
OK
127.0.0.1:6379> get rediskey
"hello world"
APPEND key value
summary: Append a value to a key
since: 2.0.0
127.0.0.1:6379> append rediskey 'heihei'
(integer) 17
127.0.0.1:6379> get rediskey
"hello worldheihei"
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
127.0.0.1:6379> set intvalue 1
OK
127.0.0.1:6379> incr intvalue
(integer) 2
127.0.0.1:6379> get intvalue
"2"
DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0
127.0.0.1:6379> get intvalue
"2"
127.0.0.1:6379> decr intvalue
(integer) 1
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
127.0.0.1:6379> get intvalue
"1"
127.0.0.1:6379> incrby intvalue 2
(integer) 3
127.0.0.1:6379> get intvalue
"3"
DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0
127.0.0.1:6379> get intvalue
"3"
127.0.0.1:6379> decrby intvalue 2
(integer) 1
127.0.0.1:6379> get intvalue
"1"
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0
127.0.0.1:6379> get rediskey
"hello worldheihei"
127.0.0.1:6379> getset rediskey 'hello redis'
"hello worldheihei"
127.0.0.1:6379> get rediskey
"hello redis"
MGET key [key ...]
summary: Get the values of all the given keys
since: 1.0.0

MSET key value [key value ...]
summary: Set multiple keys to multiple values
since: 1.0.1
127.0.0.1:6379> MSET key1 'key1' key2 'key2' key3 'key3'
OK
127.0.0.1:6379> MGET key1 key2 key3
1) "key1"
2) "key2"
3) "key3"

位图bitmap

设置一个位图使用SETBIT命令,参数为key,offset(二进制位的偏移量),value

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store