The husband Tao Gong has been
清风
through the ages, and who is there to dare to call him a man?
Article Directory
Introduction
The string type is the most basic data type in Redis. It can store any form of string, including binary data. The maximum capacity of a string type key to store data is 512MB.
1. Basic commands
command | Explanation |
---|---|
SET key value | Set field value |
GET key | Get field value |
INCR key | Increment number |
1. Assignment and Value
Assignment: SET key value
value: GET key
127.0.0.1:6379> set name breez
OK
127.0.0.1:6379> get name
"breez"
127.0.0.1:6379> get other
(nil)
127.0.0.1:6379>
Note: When the key does not exist, it will return empty (nil)
practice:Use Java to operate Redis database:SET
Import maven dependencies:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.5.1</version>
</dependency>
@Test
public void testSTRING(){
Jedis jedis = new Jedis("127.0.0.1");
String s = jedis.set("name", "清风");
System.out.println("返回值:"+s);
String name = jedis.get("name");
System.out.println("name:"+name);
}
返回值:OK
name:清风
2. Increment the number
INCR key
Increase the current key value and return the incremented value.
Example:
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
If the key value is not an integer, an error will be prompted
127.0.0.1:6379> set n str
OK
127.0.0.1:6379> incr n
(error) ERR value is not an integer or out of range
127.0.0.1:6379> set n2 1
OK
127.0.0.1:6379> incr n2
(integer) 2
127.0.0.1:6379> incr n2
(integer) 3
127.0.0.1:6379> incr n2
(integer) 4
127.0.0.1:6379>
From the abovenNot an integer, but a string,n2Is an integer
practice:Use Java to operate Redis database:INCR
@Test
public void testINCR(){
Jedis jedis = new Jedis("127.0.0.1");
Long num1 = jedis.incr("num");
Long num2 = jedis.incr("num");
Long num3 = jedis.incr("num");
System.out.println("执行三次后的值:"+num3);
}
执行三次后的值:3
Second, advanced commands
command | Explanation |
---|---|
INCRBY key increment | Increase the specified integer |
DECR key | Decrease integer |
DECRBY key decrement | Decrease the specified integer |
INCRBYFLOAT key increment | Increase the specified floating point number |
APPEND key value | Append value to the end |
STRLEN key | Get string length |
MGET key [key …] | Set multiple key values at the same time |
MSET key value [key value …] | Get the value of multiple keys at the same time |
GETBIT key offest | Get the binary number (0 or 1) corresponding to the bit number specified by the key |
SETBIT key offest value | Set the binary number (0 or 1) of the bit number specified by the key |
BITCOUNT key [start][end] | Count the specified byte range |
BITOP operation destkey key [key …] | Perform bit operations on multiple string type keys |
1. Increase the specified integer
INCRBY key increment
The INCRBY command is the same as the INCR command. The difference is that the former can be used to increment
specify the value to be increased at a time.
Example:
127.0.0.1:6379> incrby num 2
(integer) 2
127.0.0.1:6379> incrby num 2
(integer) 4
127.0.0.1:6379> incrby num 2
(integer) 6
127.0.0.1:6379>
practice:Use Java to operate Redis database:INCRBY
@Test
public void testINCRBY(){
Jedis jedis = new Jedis("127.0.0.1");
jedis.incrBy("num",2);
jedis.incrBy("num",2);
Long num = jedis.incrBy("num", 2);
System.out.println("执行三次后的结果:"+num);
}
执行三次后的结果:6
2. Decrease the specified integer
DECR key
DECR key increment
The usage is the same as the INCR command, except that the key value is decremented.
Example:
DECR
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> decr num
(integer) 9
127.0.0.1:6379> decr num
(integer) 8
127.0.0.1:6379> decr num
(integer) 7
practice:Use Java to operate Redis database:DECR
@Test
public void testDECR(){
Jedis jedis = new Jedis("127.0.0.1");
jedis.set("num","10");
System.out.println("初始值:"+jedis.get("num"));
jedis.decr("num");
jedis.decr("num");
Long num = jedis.decr("num");
System.out.println("递减3次后的值:"+num);
}
初始值:10
递减3次后的值:7
DECRBY
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> decrby num 3
(integer) 7
127.0.0.1:6379> decrby num 3
(integer) 4
127.0.0.1:6379> decrby num 3
(integer) 1
127.0.0.1:6379> decrby num 3
(integer) -2
practice:Use Java to operate Redis database:DECRBY
@Test
public void testDECRBY(){
Jedis jedis = new Jedis("127.0.0.1");
jedis.set("num","10");
System.out.println("初始值:"+jedis.get("num"));
jedis.decrBy("num",3);
jedis.decrBy("num",3);
Long num = jedis.decrBy("num", 3);
System.out.println("递减3次后的值:"+num);
}
初始值:10
递减3次后的值:1
3. Increase the specified floating point number
INCRBYFLOAT key increment
Increment a double-precision floating point number
127.0.0.1:6379> incrbyfloat num 2.6
"2.6"
127.0.0.1:6379> incrbyfloat num 2.6
"5.2"
127.0.0.1:6379> incrbyfloat num 2.6
"7.800000000000001"
127.0.0.1:6379> incrbyfloat num 2.6
"10.4"
practice:Use Java to operate Redis database:INCRBYFLOAT
@Test
public void testINCRBYFLOAT(){
Jedis jedis = new Jedis("127.0.0.1");
jedis.incrByFloat("num",2.6);
jedis.incrByFloat("num",2.6);
jedis.incrByFloat("num",2.6);
Double num = jedis.incrByFloat("num", 2.6);
System.out.println("递增4次后的结果:"+num);
}
递增4次后的结果:10.4
4. Append value to the end
APPEND key value
The function of this command is to append a value to the end of a key. If the key does not exist, it is set to the default value:, value
which is equivalent to set key value, and the return value is the total length of the appended string.
example:
127.0.0.1:6379> set key hello
OK
127.0.0.1:6379> append key world
(integer) 10
127.0.0.1:6379> get key
"helloworld"
127.0.0.1:6379> append key " breez"
(integer) 17
127.0.0.1:6379> get key
"helloworld breez"
注意
: If you need to add spaces, you need to add double quotes to the string
practice:Use Java to operate Redis database:APPEND
@Test
public void testAPPEND(){
Jedis jedis = new Jedis("127.0.0.1");
jedis.set("key","hello");
System.out.println("追加前:"+jedis.get("key"));
jedis.append("key","world");
System.out.println("追加后:"+jedis.get("key"));
}
追加前:hello
追加后:helloworld
5. Get the string length
STRLEN key
The STRLEN command returns the length of the key, or 0 if the key does not exist.
Example:
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> strlen key
(integer) 0
127.0.0.1:6379> set key breez
OK
127.0.0.1:6379> strlen key
(integer) 5
practice:Use Java to operate Redis database:STRLEN
@Test
public void testSTRLEN(){
Jedis jedis = new Jedis("127.0.0.1");
Set<String> keys = jedis.keys("*");
System.out.println("缓存中所有的键:");
System.out.println(keys);
jedis.set("key","breez");
String key = jedis.get("key");
Long strlen = jedis.strlen("key");
System.out.println(key+"的长度为:"+strlen);
}
缓存中所有的键:
[]
breez的长度为:5
6. Get/set multiple key values at the same time
MGET
MGET key [ key …]
MSET
MSET key value [key value …]
注意
: MGET/MSET is similar to GET/SET, but the former can set the key value of multiple keys.
Example:
127.0.0.1:6379> mset name breez age 22 sex male
OK
127.0.0.1:6379> keys *
1) "sex"
2) "name"
3) "age"
127.0.0.1:6379> mget name age sex
1) "breez"
2) "22"
3) "male"
practice:Use Java to operate Redis database:MSETAndMGET
@Test
public void testMSETAndMGET(){
Jedis jedis = new Jedis("127.0.0.1");
jedis.mset(new String[]{
"name","breez","age","22","sex","male"});
List<String> mget = jedis.mget(new String[]{
"name","age","sex"});
System.out.println(mget);
}
[breez, 22, male]
7. Bit manipulation
GETBIT key offset
SETBIT key offset value
BITCOUNT key [start][end]
BITOP operation destkey key [key …]
Redis Chinese official website: http://www.redis.cn
未完待续,持续更新中...