// 1.获取连接
Jedis jedis = new Jedis("localhost",6379);
// 2.操作
jedis.set("name","zhangsan");
// 3.关闭连接
jedis.close();
/**
* string操作
*/
@Test
public void test2(){
// 1.获取连接
Jedis jedis = new Jedis(); // 空参数的时候默认local:6379
// 2.操作
jedis.set("name","zhangsan");
String name = jedis.get("name");
System.out.println(name);
// 可以使用setex()方法,存储可以指定过期时间的key value
jedis.setex("activecode",20,"haha"); // 键值对存入redis,20秒后自动删除
// 3.关闭连接
jedis.close();
}
/**
* 哈希
*/
@Test
public void test3(){
// 1.获取连接
Jedis jedis = new Jedis("localhost",6379);
// 2.操作
jedis.hset("user","name","ls");
jedis.hset("user","age","23");
jedis.hset("user","gender","male");
// 获取
String name = jedis.hget("user", "name");
System.out.println(name);
Map<String, String> user = jedis.hgetAll("user");
Set<String> keySet = user.keySet();
for (String key :keySet){
System.out.print(key+": "+user.get(key)+" ");
System.out.println(jedis.hget("user",key));
}
System.out.println(user);
// 3.关闭连接
jedis.close();
}
/**
* 链表
*/
@Test
public void test4(){
// 1.获取连接
Jedis jedis = new Jedis("localhost",6379);
// 2.操作
while (jedis.rpop("queue")!=null){
jedis.rpop("queue");
}
jedis.lpush("queue","E","C","A");
jedis.rpush("queue","M","V","P");
List<String> queue = jedis.lrange("queue", 0, -1);
System.out.println(queue);
// 3.关闭连接
jedis.close();
}
/**
* 集合
*/
@Test
public void test5(){
// 1.获取连接
Jedis jedis = new Jedis("localhost",6379);
// 2.操作
jedis.sadd("classmates","a");
jedis.sadd("classmates","b");
System.out.println(jedis.smembers("classmates"));
jedis.zrem("classmates","b");
System.out.println(jedis.smembers("classmates"));
// 3.关闭连接
jedis.close();
}
/**
* 有序链表
*/
@Test
public void test6(){
// 1.获取连接
Jedis jedis = new Jedis("localhost",6379);
// 2.操作
jedis.zadd("set",90,"ys");
jedis.zadd("set",70,"xsr");
System.out.println(jedis.zrange("set",0,-1));
// 3.关闭连接
jedis.close();
}
使用:
代码:
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
// 读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties pro = new Properties();
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
// 初始化jedisPool
jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("post")));
}
/**
* 获取连接方法
*/
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
/**
* 使用redis缓存
*/
public String findAllJson(){
// 1.先从redis中查询数据
// 1.1获取redis客户端连接
Jedis jedis = JedisPoolUtils.getJedis();
String province_json = jedis.get("province");
// 2.判读一下province是否为null
if(province_json == null|| province_json.length()==0){
// redis没有数据,需要查询数据库,并且更新缓存
// 2.1从数据库查询
List<Province> ps = dao.findAll();
// 2.2将list序列化到json
ObjectMapper mapper = new ObjectMapper();
try{
province_json = mapper.writeValueAsString(ps);
}catch(JsonProcessingException e){
e.printStacktrace();
}
// 2.3 将json数据存入redis中
jedis.set("province",province_json);
// 释放连接,省略了try/catch和finally确认最终关闭的操作
jedis.close();
}else{
// redis中有数据,查询缓存
}
return province_json;
}

groupId:组织名
atifactId:模块名称
version:版本号
<groupId>redis.clientsgroupId> 每一层代表一个目录,如果里面包含了多个点,每个点就是一个目录
<artifactId>jedisartifactId>
<version>2.7.0version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
plugin>plugins>
build>
pom文件里面,对于添加的依赖还可以进行依赖范围的配置
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope> #默认值为compile,指编译环境有效
dependency>
依赖范围:我配置的jar包对哪些环境有效,默认值为compile