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

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和SET,用来设置和获取一个key的value。
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
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的int值加1
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的int值减1
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的int值加上给定的数值
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的int值减去给定的数值
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,并返回旧值
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 MSET命令,批量get和set value
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
一个字节有8个二进制位,offset对于多个字节是连续的

比如设置k1偏移量为1的位置为1,获取到ascii码中对应的01000000的值@

BITPOS命令,查找一个bit值在一个String中第一个偏移量,比如上面的k1,0到1这两个字节之间第一个1的位置为1


BITCOUNT命令,返回字节区间内,bit的count数,比如上面的k1,有1个1


BITOP命令,两个位图进行逻辑与或的操作
