1、加入相关maven依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 下面仅为了为了测试加入的依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
2、Jedis操作
package org.example;
import com.alibaba.fastjson.JSONArray;
import org.junit.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RunWith(JUnit4.class)
public class AppTest {
private static Jedis jedis;
@BeforeClass
public static void beforeClass() {
JedisShardInfo info = new JedisShardInfo("192.168.154.5", 6379);
info.setPassword("123456");
jedis = new Jedis(info);
Assert.assertEquals("PONG", jedis.ping());
}
@AfterClass
public static void afterClass() {
jedis.close();
}
@Before
public void before() {
jedis.del("name");
jedis.del("name1");
jedis.del("name2");
jedis.del("user");
jedis.del("user2");
jedis.del("language");
jedis.del("positions");
jedis.del("hll");
jedis.del("languageSet");
}
@Test
public void string() throws InterruptedException {
jedis.set("name", "wanger");
Assert.assertEquals("wanger", jedis.get("name"));
jedis.mset("name1", "wanger1", "name2", "wanger2");
Assert.assertEquals("wanger1", jedis.get("name1"));
Assert.assertEquals("wanger2", jedis.get("name2"));
jedis.del("lock");
Assert.assertEquals(1L, jedis.setnx("lock", "2").longValue());
Assert.assertEquals(0L, jedis.setnx("lock", "2").longValue());
jedis.del("lock");
Assert.assertEquals(1L, jedis.setnx("lock", "2").longValue());
jedis.expire("lock", 2);
Assert.assertEquals(0L, jedis.setnx("lock", "2").longValue());
Thread.sleep(2001L);
Assert.assertEquals(1L, jedis.setnx("lock", "2").longValue());
}
@Test
public void hash() throws InterruptedException {
jedis.hset("user", "foo", "bar");
Assert.assertEquals("bar", jedis.hget("user", "foo"));
Map<String, String> temp = new HashMap<>();
temp.put("foo", "bar");
temp.put("foo2", "bar2");
jedis.hmset("user2", temp);
Assert.assertEquals("{foo2=bar2, foo=bar}", jedis.hgetAll("user2").toString());
Assert.assertEquals("bar", jedis.hget("user2", "foo"));
Assert.assertEquals("bar2", jedis.hget("user2", "foo2"));
}
@Test
public void set() {
jedis.sadd("languageSet", "java");
jedis.sadd("languageSet", "python");
jedis.sadd("languageSet", "python");
jedis.sadd("languageSet", "python");
jedis.sadd("languageSet", "c++");
jedis.sadd("languageSet", "vb");
Set<String> sList = jedis.smembers("languageSet");
System.out.println(sList);
Assert.assertEquals(4, sList.size());
}
@Test
public void list() {
jedis.lpush("language", "java");
jedis.lpush("language", "python");
jedis.lpush("language", "python");
jedis.lpush("language", "c++");
jedis.lpush("language", "vb");
List<String> lList = jedis.lrange("language", 0, 100);
String result = JSONArray.toJSONString(lList);
Assert.assertEquals("[\"vb\",\"c++\",\"python\",\"python\",\"java\"]", result);
}
@Test
public void sortSet() {
jedis.zadd("language", 0, "java");
jedis.zadd("language", 1, "python");
jedis.zadd("language", 1, "python");
jedis.zadd("language", 10, "python");
jedis.zadd("language", 1, "c++");
jedis.zadd("language", 4, "vb");
Set<String> sList = jedis.zrangeByScore("language", 0, 10);
Assert.assertEquals("[\"java\",\"c++\",\"vb\",\"python\"]", JSONArray.toJSONString(sList));
}
@Test
public void hyperLogLog() {
float size = 100000;
for (int i = 0; i < size; i++) {
jedis.pfadd("hll", "hll-" + i);
}
long total = jedis.pfcount("hll");
System.out.println(total);
System.out.println(total / size);
System.out.println(1 - (total / size));
}
@Test
public void geo() {
Map<String, GeoCoordinate> geoMap = new HashMap<>();
GeoCoordinate localCoordinate = new GeoCoordinate(112.881953, 28.238426);
GeoCoordinate CompanyCoordinate = new GeoCoordinate(78.45681, 64.15516);
geoMap.put("local", localCoordinate);
geoMap.put("company", CompanyCoordinate);
jedis.geoadd("positions", geoMap);
Assert.assertEquals("[(112.88195461034775,28.238424808101946)]", jedis.geopos("positions", "local").toString());
Assert.assertEquals("[(78.45681041479111,64.15516002493432)]", jedis.geopos("positions", "company").toString());
}
}