引入 jedis 依赖:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<!-- Dec 15, 2019 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
简单测试:
public class TestRedis {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost", 6379);
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(value);
}
}
常用数据类型测试:
public class TestRedisCommands {
private Jedis jedis;
@Before
public void setUp() {
jedis = new Jedis("localhost", 6379);
}
@Test
public void testBasicString(){
jedis.set("name","meepo");
System.out.println(jedis.get("name")); //执行结果:meepo
jedis.append("name","dota");
System.out.println(jedis.get("name")); //执行结果:meepodota
jedis.set("name","poofu");
System.out.println(jedis.get("name")); //执行结果:poofu
jedis.del("name");
System.out.println(jedis.get("name")); //执行结果:null
jedis.mset("name","meepo","dota","poofu"); //同时设置多个字符串
System.out.println(jedis.mget("name","dota"));
}
@Test
public void testMap(){
Map<String, String> user = new HashMap<String, String>();
user.put("name", "meepo");
user.put("pwd", "password");
user.put("age", "18");
jedis.hmset("user", user);
//结果是一个泛型的List
//第一个参数是key,后面的参数是map中的对象的key,可以是多个
List<String> rsmap = jedis.hmget("user", "name");
System.out.println(rsmap); //执行结果:[meepo]
jedis.hdel("user","age");
System.out.println(jedis.hmget("user", "age")); //执行结果:null
System.out.println(jedis.hlen("user")); //执行结果:2
System.out.println(jedis.exists("user"));//执行结果:true
System.out.println(jedis.hkeys("user"));//执行结果:[pwd, name]
System.out.println(jedis.hvals("user"));//执行结果:[meepo, password]
Iterator<String> iter=jedis.hkeys("user").iterator();
while (iter.hasNext()){
String key = iter.next();
System.out.println(key+": "+jedis.hmget("user", key));
}
}
@Test
public void testList(){
System.out.println(jedis.lrange("user",0,-1));
jedis.lpush("user","spring");
jedis.lpush("user","struts");
jedis.lpush("user","hibernate");
System.out.println(jedis.lrange("user",0,-1));
}
@Test
public void testSet(){
jedis.sadd("sname","meepo");
jedis.sadd("sname","dota");
jedis.sadd("sname","poofu");
jedis.sadd("sname","noname");
jedis.srem("sname","noname");
System.out.println(jedis.smembers("sname")); //获取所有的value
System.out.println(jedis.sismember("sname", "meepo"));//判断meepo是否为集合的元素
System.out.println(jedis.srandmember("sname")); //随机获取一个元素
System.out.println(jedis.scard("sname")); //返回集合的元素数量
}
@Test
public void test() throws InterruptedException {
System.out.println(jedis.keys("*")); //返回当前库中所有的key
System.out.println(jedis.keys("*name"));//返回的匹配的key
System.out.println(jedis.del("sanmdde"));//删除key,成功返回1,失败返回0
System.out.println(jedis.ttl("sname"));//返回key的有效时间,-1表示永远有效
jedis.setex("mykey", 10, "seconds");//指定key的存活时间为10秒
Thread.sleep(5000);
System.out.println(jedis.ttl("mykey"));//剩余5秒
System.out.println(jedis.exists("key"));//检查key是否存在
System.out.println(jedis.rename("mykey","yourkey")); //重命名
System.out.println(jedis.get("mykey"));//返回为null
jedis.rpush("a","1");
jedis.lpush("a","6");
jedis.lpush("a","3");
jedis.lpush("a","9");
System.out.println(jedis.lrange("a",0,-1)); //[9, 3, 6, 1]
System.out.println(jedis.sort("a")); //[1, 3, 6, 9]
System.out.println(jedis.lrange("a",0,-1));
}
}
命令参考:http://www.redis.cn/commands.html
API参考:https://tool.oschina.net/uploads/apidocs/redis/clients/jedis/Jedis.html