用Java操作Redis数据库

  • 使用步骤
  • 操作数据
  • 连接池JedisPool
  • 工具类

    一、介绍

    Jedis:一款Java操作Redis数据库的工具。

二、使用步骤

  1. 下载jedis的jar包
  2. 创建对象,获取连接 Jedis jedis = new Jedis("IP",端口号);
  3. 操作数据库 jedis.set("username","刘能");
  4. 释放资源 jedis.close();

    三、Jedis操作Redis中的不同数据结构

    1. 字符串类型 string

  • set
  • setex("key","毫秒值",value):存储可以指定过期时间的key value
  • get

    2. 哈希类型 hash : map格式

  • hset("key","skey","value")
  • hget("key","skey")
  • Map<String,String> hgetAll("key")

    3. 列表类型 list : linkedlist格式。支持重复元素

  • lpush/rpush("key","value1","value2","value3"...)
  • lpop/rpop
  • List<String> lrange start end :范围获取 0 -1为获取全部
    1
    2
    3
    4
    5
    6
    //存储
    jedis.lpush("myList","a","b","c");//左边
    jedis.rpush("myList","a","b","c");//右边
    //获取
    List<String> list = jedis.lrange("myList", 0, -1);
    System.out.println(list);//[c, b, a, a, b, c]

    4. 集合类型 set : 不允许重复元素

  • sadd("key","value1","value2"...)
  • Set<String> smembers()
    1
    2
    3
    4
    5
    //存储
    jedis.sadd("mySet","java","php","c++");
    //获取
    Set<String> set = jedis.smembers("mySet");
    System.out.println(set);//[c++, java, php]

    5. 有序集合类型 sortedset:不允许重复元素,且元素有顺序

  • zadd("key",socre,"value"):sorce为double,排序分数,再次设置可以修改
  • Set<String> zrange("key",strat,end):strat end为开始结束的索引
    1
    2
    3
    4
    5
    6
    //存储
    jedis.zadd("mySortedSet", 3.0, "盖伦");
    jedis.zadd("mySortedSet", 30.0, "安其拉");
    jedis.zadd("mySortedSet", 90.0, "猴子");
    //获取
    Set<String> set = jedis.zrange("mySortedSet", 0, -1);//[盖伦, 安其拉, 猴子]

四、连接池JedisPool

1. 使用步骤

  1. 创建JedisPool连接池对象 JedisPool(GenericObjectPoolConfig poolConfig, String host);
    • GenericObjectPoolConfig:配置对象
  2. 调用方法
    • getResource()获取Jedis连接
  3. 归还连接。
    • jedis.close();
  • 详细配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #最大活动对象数     
    redis.pool.maxTotal=1000
    #最大能够保持idel状态的对象数
    redis.pool.maxIdle=100
    #最小能够保持idel状态的对象数
    redis.pool.minIdle=50
    #当池内没有返回对象时,最大等待时间
    redis.pool.maxWaitMillis=10000
    #当调用borrow Object方法时,是否进行有效性检查
    redis.pool.testOnBorrow=true
    #当调用return Object方法时,是否进行有效性检查
    redis.pool.testOnReturn=true
    #“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.
    redis.pool.timeBetweenEvictionRunsMillis=30000
    #向调用者输出“链接”对象时,是否检测它的空闲超时;
    redis.pool.testWhileIdle=true
    # 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.
    redis.pool.numTestsPerEvictionRun=50
    #redis服务器的IP
    redis.ip=xxxxxx
    #redis服务器的Port
    redis1.port=6379
  • 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test
public void test08() {

//0.创建一个配置对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);//最大连接数
config.setMaxIdle(10);//最大空闲连接

//1.创建JedisPool连接池对象
JedisPool jedisPool = new JedisPool(config,"localhost",6379);
//2.获取连接
Jedis jedis = jedisPool.getResource();

jedis.set("name", "lisisisi");
//3.关闭 归还到连接池中
jedis.close();
}

2. 工具类

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.yangye.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* JedisPool工具类
* 加载配置文件,配置连接池的参数
* 提供获取连接的方法
*/
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();
}
}

五、注意

  1. 使用redis缓存一些不经常发生变化的数据。
  2. 数据库的数据一旦发生改变,则需要更新缓存。
  3. 数据库的表执行 增删改的相关操作,需要将redis缓存数据情况,再次存入。
  4. 在service对应的增删改方法中,将redis数据删除。

猜你喜欢

转载自blog.csdn.net/qq_45370568/article/details/106817802