Java 中使用 Redis

时间:2022-07-26
本文章向大家介绍Java 中使用 Redis,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.1 Jedis 快速使用

  Jedis 是 Redis 官方推荐的 Java 连接开发工具。我们需要通过 Jedis 在 Java 中操作 Redis。

1.1.1 基本使用

  Jedis 的使用相当简单,只需要在创建对象时指定 host、port、password 就行了,没有设置密码的可以省略 password。Jedis 对象创建之后,底层会开启一条 Socket 通道和 Redis 服务进行连接。在使用完 Jedis 对象之后,需要调用 close() 方法把连接关闭,否则会占用系统资源。

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 使用
 */
public class JedisTest {

    @Test
    public void text() {
    	// 创建连接
        Jedis jedis = new Jedis("localhost", 6379);
		// 操作 Redis
        jedis.set("time", new Date().toString());
		// 释放资源
        jedis.close();
    }
}

1.1.2 操作 string

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 操作 string
 */
public class JedisTest {

    @Test
    public void textString() {
    	// 创建连接
        Jedis jedis = new Jedis("localhost", 6379);
        
		// 操作 string
        jedis.set("time", new Date().toString());
        // 设置过期时间,60s
        jedis.setex("key", 60, "value");
        
        // 获取数据
        String time = jedis.get("time");
        System.out.println(time);
	
		// 释放资源
        jedis.close();
    }
}

1.1.3 操作 hash

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 操作 hash
 */
public class JedisTest {

    @Test
    public void textHash() {
        // 创建连接
        Jedis jedis = new Jedis("localhost", 6379);

        // 操作 hash
        jedis.hset("hash", "name", "张三");
        jedis.hset("hash", "age", "23");
        jedis.hset("hash", "sex", "0");

        // 获取数据
        String name = jedis.hget("hash", "name");
        System.out.println(name);

        // 删除数据
        jedis.hdel("hash", "age");

        // 获取所有数据
        System.out.println();
        Map<String, String> hash = jedis.hgetAll("hash");
        hash.keySet().forEach(s -> System.out.println(hash.get(s)));

        // 释放资源
        jedis.close();
    }
}

1.1.4 操作 list

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 操作 list
 */
public class JedisTest {

    @Test
    public void textList() {
        // 创建连接, 空参默认 "localhost", 6379
        Jedis jedis = new Jedis();

        // list 添加数据
        // 1. 从左侧(头部)存
        jedis.lpush("list", "1", "2", "3");
        // 2. 从右侧(尾部)存
        jedis.rpush("list", "a", "b", "c");

        // 获取数据
        List<String> list = jedis.lrange("list", 0, -1);
        System.out.println(list);

        // 删除数据
        // 1. 从左侧删除
        jedis.lpop("list");
        // 2. 从右侧删除
        jedis.rpop("list");


        // 释放资源
        jedis.close();
    }
}

1.1.5 操作 set

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 操作 set
 */
public class JedisTest {

    @Test
    public void textSet() {
        // 创建连接, 空参默认 "localhost", 6379
        Jedis jedis = new Jedis();

        // set 添加数据
        jedis.sadd("set", "a", "b", "c");
        jedis.sadd("set", "1", "b", "3");

        // 获取数据
        Set<String> set = jedis.smembers("set");
        System.out.println(set);

        // 删除数据
        jedis.srem("set", "1", "C");


        // 释放资源
        jedis.close();
    }
}

1.1.6 操作 sortedset

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 操作 sortedset
 */
public class JedisTest {

    @Test
    public void textZset() {
        // 创建连接, 空参默认 "localhost", 6379
        Jedis jedis = new Jedis();

        // set 添加数据
        jedis.zadd("zset", 60, "1");
        jedis.zadd("zset", 70, "3");
        jedis.zadd("zset", 80, "2");
        jedis.zadd("zset", 40, "a");

        // 获取数据
        Set<String> zset = jedis.zrange("zset", 0, -1);
        System.out.println(zset);

        // 删除数据
        jedis.zrem("zset", "1");


        // 释放资源
        jedis.close();
    }
}

1.2 Jedis 连接池

1.2.1 获取 Jedis 连接池

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/9/28
 * @description Jedis 连接池
 */
public class JedisTest {

    @Test
    public void textPool() {
        // 配置 Jedis 连接池
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

        // 创建 Jedis 连接池
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,"localhost", 6379);

        // 获取连接
        Jedis jedis = jedisPool.getResource();

        System.out.println(jedis);

        jedis.close();
    }
}

1.2.2 连接池配置

# 最大活动对象数    
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