14.3 Redis工具

14.3.1 持久化

  • redis是一个内存数据库,当redis服务器重启,或者电脑重启,数据会丢失,我们可以将redis内存中的数据持久化保存到硬盘的文件中。

  • redis持久化机制:

    • RDB:默认方式,不需要进行配置,默认就使用这种机制

      在一定的间隔时间中,检测key的变化情况,然后持久化数据

      1. 编辑redis.windwos.conf文件
        1
        2
        3
        4
        5
        6
        #   after 900 sec (15 min) if at least 1 key changed
        save 900 1
        # after 300 sec (5 min) if at least 10 keys changed
        save 300 10
        # after 60 sec if at least 10000 keys changed
        save 60 10000
      2. 重新启动redis服务器,并指定配置文件名称
        1
        D:\JavaWeb\redis\windows-64\redis-2.8.9>redis-server.exe redis.windows.conf
    • AOF:日志记录的方式,可以记录每一条命令的操作。可以每一次命令操作后,持久化数据

      1. 编辑redis.windwos.conf文件
        1
        2
        3
        4
        appendonly no(关闭aof) --> appendonly yes (开启aof)
        # appendfsync always : 每一次操作都进行持久化
        appendfsync everysec : 每隔一秒进行一次持久化
        # appendfsync no : 不进行持久化

14.3.2 Java客户端 Jedis

  • Jedis: 一款java操作redis数据库的工具.

使用步骤

  1. 下载jedis的jar包
  2. 使用
    1
    2
    3
    4
    5
    6
    //1. 获取连接
    Jedis jedis = new Jedis("localhost",6379);
    //2. 操作
    jedis.set("username","zhangsan");
    //3. 关闭连接
    jedis.close();

Jedis操作各种redis中的数据结构

● 字符串类型 string
  • set
  • get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//1. 获取连接
//如果使用空参构造,默认值 "localhost",6379端口
Jedis jedis = new Jedis();
//2. 操作
//存储
jedis.set("username","zhangsan");
//获取
String username = jedis.get("username");
System.out.println(username);
//可以使用setex()方法存储可以指定过期时间的 key value
//将activecode:hehe键值对存入redis,并且20秒后自动删除该键值对
jedis.setex("activecode",20,"hehe");
//3. 关闭连接
jedis.close();
● 哈希类型 hash

map格式

  • hset
  • hget
  • hgetAll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//1. 获取连接
Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// 存储hash
jedis.hset("user","name","lisi");
jedis.hset("user","age","23");
jedis.hset("user","gender","female");
// 获取hash
String name = jedis.hget("user", "name");
System.out.println(name);
// 获取hash的所有map中的数据
Map<String, String> user = jedis.hgetAll("user");
// keyset
Set<String> keySet = user.keySet();
for (String key : keySet) {
//获取value
String value = user.get(key);
System.out.println(key + ":" + value);
}
//3. 关闭连接
jedis.close();
● 列表类型 list

linkedlist格式。支持重复元素

  • 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
19
//1. 获取连接
Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// list 存储
jedis.lpush("mylist","a","b","c");//从左边存
jedis.rpush("mylist","a","b","c");//从右边存
// list 范围获取
List<String> mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);
// list 弹出
String element1 = jedis.lpop("mylist");//c
System.out.println(element1);
String element2 = jedis.rpop("mylist");//c
System.out.println(element2);
// list 范围获取
List<String> mylist2 = jedis.lrange("mylist", 0, -1);
System.out.println(mylist2);
//3. 关闭连接
jedis.close();
● 集合类型 set

不允许重复元素

  • sadd
  • smembers:获取所有元素
1
2
3
4
5
6
7
8
9
10
//1. 获取连接
Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// set 存储
jedis.sadd("myset","java","php","c++");
// set 获取
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);
//3. 关闭连接
jedis.close();
● 有序集合类型 sortedset

不允许重复元素,且元素有顺序

  • zadd
  • zrange
1
2
3
4
5
6
7
8
9
10
11
12
//1. 获取连接
Jedis jedis = new Jedis();//如果使用空参构造,默认值 "localhost",6379端口
//2. 操作
// sortedset 存储
jedis.zadd("mysortedset",3,"亚瑟");
jedis.zadd("mysortedset",30,"后裔");
jedis.zadd("mysortedset",55,"孙悟空");
// sortedset 获取
Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1);
System.out.println(mysortedset);
//3. 关闭连接
jedis.close();

jedis连接池: JedisPool

使用

  1. 创建JedisPool连接池对象

  2. 调用方法 getResource()方法获取Jedis连接

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    //0.创建一个配置对象
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(50);
    config.setMaxIdle(10);
    //1.创建Jedis连接池对象
    JedisPool jedisPool = new JedisPool(config,"localhost",6379);
    //2.获取连接
    Jedis jedis = jedisPool.getResource();
    //3. 使用
    jedis.set("hehe","heihei");
    //4. 关闭 归还到连接池中
    jedis.close();

连接池工具类

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
public class JedisPoolUtils {
private static JedisPool jedisPool;
static{
//读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties pro = new Properties();
//关联文件
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取数据,设置到JedisPoolConfig中
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();
}
}