用Jedis操作redis缓存

时间:2022-08-10 17:26:40
  • 概述

         Jedis是redis官方推荐的用于访问Java客户端,在https://github.com/xetorthio/jedis下载最新的jedis。

  • 访问redis
1、访问简单的key——value,
public class RedisDemo {
public static void main(String[] args) {

Jedis jedis = new Jedis("127.0.0.1");
jedis.connect();
jedis.set("key", "123");
String value = jedis.get("key");
System.out.println(value);
}
}
2、在value中存放HashMap
package com.nomouse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

public class RedisHashMap {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1");

Map<String, String> map = new HashMap<String, String>();
map.put("name", "nomouse");
map.put("password", "123456");
map.put("sex", "male");

jedis.connect();

jedis.hmset("user", map);

List<String> list = jedis.hmget("user", "name","password");

System.out.println("name=" + list.get(0));
System.out.println("password=" + list.get(1));
}
}