Jedis操作redis

  • jedis:Java操作redis数据库的工具
  • 下载两个jar包:commons-pool2-2.9.0.jar jedis-2.9.0.jar

jedis操作String

set(key,value)

get(key)

setex(key,time,value)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
public void test(){
//获取连接
Jedis jedis = new Jedis("127.0.0.1",6379);//如果使用空参构造,默认值localhost,6379
//操作数据库
jedis.set("username","hxx");
String username = jedis.get("username");
System.out.println(username);
//可以使用setex()方法,可以指定过期时间
//将activecode value键值对存入redis,并在10秒后删除
jedis.setex("activecode",20,"hehe");
//关闭数据库
jedis.close();
}

jedis操作hash

hset(key,field,value)

hget(key,field)

hgetAll(key)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Test
public void test1(){
//获取连接
Jedis jedis = new Jedis("127.0.0.1",6379);
//操作数据库
jedis.hset("user","name","hxx");
jedis.hset("user","age","20");
//获取hash
String name = jedis.hget("user", "name");
System.out.println(name);
//获取所有数据
Map<String, String> user = jedis.hgetAll("user");
Set<String> keySet = user.keySet();
for (String field : keySet) {
System.out.println(field+":"+user.get(field));
}
//关闭数据库
jedis.close();
}

jedis操作list

lpush/rpush

lpop/rpop

lrange start end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
public void test2(){
//获取连接
Jedis jedis = new Jedis("127.0.0.1",6379);
//操作数据库
//存储 cbaabc
jedis.lpush("mylist","a","b","c");
jedis.rpush("mylist","a","b","c");
//范围获取
List<String> mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);

jedis.lpop("mylist");
jedis.rpop("mylist");

//关闭数据库
jedis.close();
}

jedis操作set

sadd

smembers:获取所有元素

1
2
3
4
5
6
7
8
9
10
11
@Test
public void test3(){
//获取连接
Jedis jedis = new Jedis("127.0.0.1",6379);
//操作数据库
jedis.sadd("myset","a","a","b");
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);
//关闭数据库
jedis.close();
}

jedis操作sortedset

zadd

zrange

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void test4(){
//获取连接
Jedis jedis = new Jedis("127.0.0.1",6379);
//操作数据库
jedis.zadd("sortedset",3,"hxx");
jedis.zadd("sortedset",4,"qzy");
Set<String> myset = jedis.zrange("sortedset",0,-1);
System.out.println(myset);
//关闭数据库
jedis.close();
}

jedis连接池

创建连接池对象JedisPool

调用方法getReasource获取Jedis连接

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void test5(){
//创建配置对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(2);//最大允许连接数

JedisPool jedisPool = new JedisPool(config,"127.0.0.1",6379);
JedisPool jedisPool1 = new JedisPool();
Jedis jedis = jedisPool.getResource();
//归还到连接池中
jedis.close();
}

jedis工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class JedisBeanUtils {
private static JedisPool jedisPool;
static {
//读取配置文件
InputStream is = JedisBeanUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties pro = new Properties();
try {
pro.load(is);//加载配置文件
} catch (IOException e) {
e.printStackTrace();
}
//创建jedis配置文件
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("port")));
}
/*获取连接方法*/
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
@Test
public void test6(){
//工具类
Jedis jedis = JedisBeanUtils.getJedis();
jedis.set("name","hxx");
jedis.close();
}