redis常用命令
字符串常用的操作
添加字符串 | set hello world |
---|
设置多个KV | mset AA AAA BB BBB CC CCC |
设置过期时间 | setex hello1 5 word1 |
查询字符串 | get hello |
查询多key | mget hello hello2 hello3 |
查询长度 | strlen hello |
修改字符串 | set hello world2 |
删除字符串 | del hello |
hash常用的操作
添加一个hash列表 | hset hkey1 name zhangsan |
---|
一次设置多个值 | hmset hkey3 name wangwu age 20 sex 1 |
查看数据 | hget hkey1 name |
查询key内所有的字段 | hkeys hkey3 |
查询key的数据量 | hlen hkey3 |
查询某一key内多字段的值 | hmget hkey3 name age |
修改数据 | hset hkey1 name zhangsan2 |
删除key中的数据 | hdel hkey3 sex |
list常用的操作
添加一个或多个值到列表 | lpush lkey1 zhangsan lisi |
---|
向已经存在的列表中插入数据(右边插入) | rpush lkey1 AA BB |
查询list中所有数据 (-1表示所有) | lrange lkey1 0 -1 |
通过索引查询数据 | lindex lkey1 0 |
通过索引修改数据 | lset lkey1 5 eee |
删除指定key数据 | del lkey1 |
移出并获取列表的第一个元素 | lpop lkey1 |
移出并获取列表的最后一个元素 | rpop lkey1 |
移除列表的最后一个元素,并将该元素添加到另一个列表并返回 | rpoplpush lkey1 lkey2 |
set常用的操作
向集合添加一个或多个数据 | sadd skey1 zhangsan lisi |
---|
查询集合所有数据 | smembers skey1 |
查询两个集合的差值 | sdiff skey1 skey2 |
查询两个集合交集 | sinter skey1 skey2 |
查询并集 | sunion skey1 skey2 |
将数据在一个集合移动到另一个集合 | smove skey1 skey2 zhangsan |
移除结果中的某个数据 | srem skey2 zhangsan |
对key常用的操作
查询所有符合模式的key | K****eys * |
---|
查询key对应数据的类型 | Type hkey1 |
查询剩余过期时间(秒) | ttl hkey1 |
对key进行重命名 | Rename hkey1 hkey10 |
删除存在的key | del BB |
pom
<groupId>org.example</groupId>
<artifactId>redisAPI</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
</plugins>
</build>
redisAPI 代码示例
package com.czxy;
import redis.clients.jedis.*;
public class Test01 {
public static void main(String[] args) {
test01();
test02();
config().close();
}
public static JedisPool config(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMinIdle(5);
config.setMaxIdle(10);
config.setMaxWaitMillis(10000);
return new JedisPool(config, "hadoop01", 6379);
}
public static void test01(){
Jedis jedis = config().getResource();
jedis.rpush("ltest1", "1", "2", "3", "4", "5", "6", "7", "8", "9");
jedis.lpush("ltest2", "a", "b", "c", "d", "e", "f");
jedis.linsert("ltest1", BinaryClient.LIST_POSITION.BEFORE,"3","3333");
jedis.linsert("ltest1", BinaryClient.LIST_POSITION.AFTER,"6","6666");
String ltest2 = jedis.lindex("ltest2", 3);
System.out.println("lTest2 索引为3的数据为:" + ltest2);
jedis.lset("ltest2",1,"EEE");
jedis.ltrim("ltest2", 1, 4);
jedis.rpoplpush("ltest1","ltest2");
jedis.close();
}
public static void test02(){
Jedis jedis = config().getResource();
jedis.sadd("setdemo1","aa","bb","cc","dd","ee","ff");
jedis.sadd("setdemo2","11","22","33","dd","ee","ff");
jedis.sinterstore("setdemo3","setdemo1","setdemo2");
jedis.sunionstore("setdemo4","setdemo1","setdemo2");
jedis.sdiffstore("setdemo5","setdemo2","setdemo1");
jedis.smove("setdemo2","setdemo1","11");
jedis.srem("setdemo1","bb");
jedis.close();
}
}