一、Jedis介绍 首先需要保证redis server已开启及安装了java redis驱动,并在redis-cli关闭保护模式,否则可能会报错io.lettuce.core.RedisException:java.io.Exception
文章图片
jedis方法名称跟redis命令一模一样,学习难度较低。
【缓存学习总结-java操作redis】引用方法,在pom.xml中添加依赖:
redis.clients
jedis
2.9.0
二、直接上代码
package com.test;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author wanwei
* @Date 2020/3/26 9:53
* @Description
* @Reviewer
*/
public class jredisTest {
private Jedis jedis;
@Before
public void setJedis(){
jedis = new Jedis("xxxx", 6379);
jedis.auth("ssssss");
jedis.select(2);
System.out.println("连接服务器成功.........");
}/*redis操作字符串*/
@Test
public void testString(){
//添加数据
jedis.set("name", "Ok");
System.out.println("拼接前:"+ jedis.get("name"));
//拼接数据
jedis.append("name", "you");
System.out.println("拼接后:" + jedis.get("name"));
//添加多个键值对
jedis.mset("name1","sdf","name2","sdf","name3","dsf");
System.out.println(jedis.get("name1") + " " + jedis.get("name2") + " " + jedis.get("name3"));
}//hash
@Test
public void testMap(){
//添加数据
Map,String> map = new HashMap<>();
map.put("name","wanwei");
map.put("age","12");
map.put("email","784971886@qq.com");
jedis.hmset("wanwei",map);
List> list = jedis.hmget("wanwei","name","age","email");
System.out.println(list);
//删除某个键值
jedis.hdel("wanwei", "age");
System.out.println("age" + jedis.hmget("wanwei","age"));
System.out.println("wanwei中的键个数:" + jedis.hlen("wanwei"));
System.out.println("是否存在wanwei记录+" + jedis.exists("wanwei"));
System.out.println("wanwei中的所有key:" + jedis.hkeys("wanwei"));
System.out.println("wanwei中的所有value:" + jedis.hvals("wanwei"));
for (String key : jedis.hkeys("wanwei")) {
System.out.println(key + "的值为:" + jedis.hmget("wanwei", key));
}jedis.del("wanwei");
System.out.println("删除后是否存在key为wanwei的记录:" + jedis.exists("wanwei"));
}//list
@Test
public void testList(){
//移除javaFrameWork中的所有内容
jedis.del("javaFramework");
jedis.lpush("javaFrameWork","springMVC");
jedis.lpush("javaFrameWork","mybatis");
jedis.lpush("javaFrameWork","Spring");
System.out.println("长度:" + jedis.llen("javaFrameWork"));
System.out.println("所有元素:" + jedis.lrange("javaFrameWork", 0, -1));
jedis.del("javaFrameWork");
System.out.println("删除后长度:"+jedis.llen("javaFrameWork"));
System.out.println("删除后所有元素:"+jedis.lrange("javaFrameWork",0,-1));
}//set
@Test
public void testSet(){
//添加
jedis.sadd("user", "wan");
jedis.sadd("user","zhang");
jedis.sadd("user","wang");
jedis.srem("user","wan");
System.out.println("user中的members:" + jedis.smembers("user"));
System.out.println("ss是否user中的元素:" + jedis.sismember("user","ss"));
System.out.println("user中的一个随机元素:" + jedis.srandmember("user"));
System.out.println("user中的元素个数:" + jedis.scard("user"));
}/**
* 排序
*/
@Test
public void test(){
jedis.del("number");
jedis.rpush("number","4");
jedis.rpush("number","5");
jedis.rpush("number","3");
jedis.lpush("number","9");
jedis.lpush("number","1");
jedis.lpush("number","2");
System.out.println(jedis.lrange("number",0,jedis.llen("number")));
System.out.println("排序:"+jedis.sort("number"));
System.out.println(jedis.lrange("number",0,-1));
jedis.del("number");
}}