Redis基础 Jedis
Posted 杀智勇双全杀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis基础 Jedis相关的知识,希望对你有一定的参考价值。
概述
当然是不可能手动一条一条命令操作Redis的,类似JDBC方式的做法就是Jedis。虽然Redis是C语言写的,但是借助Jedis的封装,Java依然可用很方便地操作Redis。
Talk is cheap, show me the code。
Maven依赖
在pom.xml填写:
<dependencies>
<!-- Jedis 依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!-- JUnit 4 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</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>
</configuration>
</plugin>
</plugins>
</build>
套路
构建连接
Jedis jedis = null;
@Before
public void getConnection() {
//方式一:直接构建一个连接对象
//实例化一个客户端连接对象,指定服务端地址
// jedis = new Jedis("node1",6379);
//方式二:通过连接池来构建连接
//构建连接池,并且配置连接池属性
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(20);//最大连接数
jedisPoolConfig.setMaxIdle(10);//最大空闲连接
jedisPoolConfig.setMaxWaitMillis(1500);//最大等待时间
//构建连接池:连接池配置对象,主机名,端口
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "node1", 6379);
//从连接池中获取连接对象
jedis = jedisPool.getResource();
}
释放连接
@After
public void close() {
jedis.close();
}
操作测试
模仿JDBC的套路,在中间插入测试代码即可。。。
String操作
@Test
public void stringTest(){
//set/get/incr/setex/expire/ttl/exists
// jedis.set("s1","hadoop");
// String s1 = jedis.get("s1");
// System.out.println(s1);
// jedis.set("s2","2");
// jedis.incr("s2");
// String s2 = jedis.get("s2");
// System.out.println(s2);
// jedis.expire("s2",20);
// while(true){
// System.out.println(jedis.ttl("s2"));
// }
// System.out.println(jedis.exists("s1"));
// System.out.println(jedis.exists("s2"));
jedis.setex("s2",10,"hive");
}
Hash操作
@Test
public void hashTest(){
//hset/hmset/hget/hgetall/hdel/hlen/hexists
// jedis.hset("m1","name","zhangsan");
// String name = jedis.hget("m1", "name");
// System.out.println(name);
// System.out.println("==========================");
// Map<String,String> maps = new HashMap<>();
// maps.put("age","20");
// maps.put("sex","male");
// jedis.hmset("m1",maps);
// List<String> hmget = jedis.hmget("m1", "name", "age", "sex");
// System.out.println(hmget);
Map<String, String> m1 = jedis.hgetAll("m1");
for(Map.Entry map1 : m1.entrySet()){
System.out.println(map1.getKey()+"\\t"+map1.getValue());
}
jedis.hdel("m1","name");
Long m11 = jedis.hlen("m1");
System.out.println(m11);
System.out.println(jedis.hexists("m1","name"));
System.out.println(jedis.hexists("m1","age"));
}
List操作
@Test
public void listTest(){
//lpush/rpush/lrange/llen/lpop/rpop
jedis.lpush("list1","1","2","3","4");
jedis.rpush("list1","5","6","7","8");
List<String> list1 = jedis.lrange("list1", 0, -1);
System.out.println(list1);
System.out.println(jedis.llen("list1"));
jedis.lpop("list1");
jedis.rpop("list1");
System.out.println(jedis.lrange("list1",0,-1));
}
Set操作
@Test
public void setTest(){
//sadd/smembers/sismember/scard/srem
// jedis.sadd("set1","1","4","5","4");
// jedis.sadd("set1","1","1","6","6");
// Set<String> set1 = jedis.smembers("set1");
// System.out.println(set1);
System.out.println(jedis.sismember("set1","2"));
System.out.println(jedis.sismember("set1","1"));
System.out.println(jedis.scard("set1"));
jedis.srem("set1","1");
System.out.println(jedis.sismember("set1","1"));
}
Zset操作
@Test
public void zsetTest(){
//zadd/zrange/zrevrange/zcard/zrem
// jedis.zadd("zset1",20,"yuwen");
// jedis.zadd("zset1",99,"yingyu");
// jedis.zadd("zset1",30,"shengwu");
// jedis.zadd("zset1",55,"wuli");
Set<String> zset1 = jedis.zrange("zset1", 0, -1);
// Set<Tuple> zset1 = jedis.zrevrangeWithScores("zset1", 0, -1);
// System.out.println(zset1);
System.out.println(jedis.zcard("zset1"));
jedis.zrem("zset1","shengwu");
System.out.println(jedis.zrevrangeWithScores("zset1", 0, -1));
}
以上是关于Redis基础 Jedis的主要内容,如果未能解决你的问题,请参考以下文章
Redis 基础 -- Redis的Java客户端 (JedisSpringDataRedis)两种序列化方式(RedisTemplateStringRedisTemplate)