redis读取写入

本文介绍了如何在Java应用中使用JedisPool连接Redis进行数据的读写操作,并详细展示了Maven的配置过程,同时讲解了如何在Redis中切换数据库库。
摘要由CSDN通过智能技术生成

两个java文件

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oruit.common.utils.cache.redis;

import com.oruit.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanResult;

import java.util.*;
import java.util.Map.Entry;

@Slf4j
public class RedisUtil {


    /**
     * 成功,"OK"
     */
    private static final String SUCCESS_OK = "OK";
    /**
     * 成功,1L
     */
    private static final Long SUCCESS_STATUS_LONG = 1L;
    /**
     * 只用key不存在时才设置。Only set the key if it does not already exist
     */
    private static final String NX = "NX";
    /**
     * XX -- 只有key存在时才设置。和NX相反。Only set the key if it already exist.
     */
    private static final String XX = "XX";
    /**
     * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds
     */
    private static final String EX = "EX";
    /**
     * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds
     */
    private static final String PX = "PX";

    protected RedisUtil() {
    }

    /**
     * 成功返回true
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis();
            String statusCode = jedis.set(key, value);
            if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
                return true;
            }
        } catch (Exception e) {
        } finally {
            if (jedis != null) {
                RedisPoolUtil.returnResource(jedis);
            }
        }
        return false;
    }

    /**
     * 返回值
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis();
            return jedis.get(key);
        } catch (Exception e) {
        } finally {
            if (jedis != null) {
                RedisPoolUtil.returnResource(jedis);
            }
        }
        return null;
    }

    /**
     * 设置key值和过期时间
     *
     * @param key
     * @param value
     * @param seconds 秒数,不能小于0
     * @return
     */
    public static boolean setByTime(String key, String value, int seconds) {
        if (seconds < 0) {
            return false;
        }
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis();
            String statusCode = jedis.setex(key, seconds, value);
            if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
                return true;
            }
        } catch (Exception e) {
        } finally {
            if (jedis != null) {
                RedisPoolUtil.returnResource(jedis);
            }
        }
        return false;
    }

    /**
     * @param key
     * @param value
     * @param nxxx  NX|XX 是否存在
     *              <li>NX -- Only set the key if it does not already exist.</li>
     *              <li>XX -- Only set the key if it already exist.</li>
     * @param expx  EX|PX, expire time units ,时间单位格式,秒或毫秒
     *              <li>EX = seconds;</li>
     *              <li>PX = milliseconds</li>
     * @param time  expire time in the units of expx,时间(long型),不能小于0
     * @return
     */
    public static boolean set(String key, String value,
                              String nxxx, String expx, long time) {
        if (time < 0) {
            return false;
        }
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis();
            String statusCode = jedis.set(key, value, nxxx, expx, time);
            if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
                return true;
            }
        } catch (Exception e) {
        } finally {
            if (jedis != null) {
                RedisPoolUtil.returnResource(jedis);
            }
        }
        return false;
    }

    /**
     * 设置key
     *
     * @param key
     * @param value
     * @param nxxx  NX|XX 是否需要存在
     *              <li>NX -- Only set the key if it does not already exist.</li>
     *              <li>XX -- Only set the key if it already exist.</li>
     * @return
     */
    public static boolean set(String key, String value,
                              String nxxx) {
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis();
            String statusCode = jedis.set(key, value, nxxx);
            if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
                return true;
            }
        } catch (Exception e) {
        } finally {
            if (jedis != null) {
                RedisPoolUtil.returnResource(jedis);
            }
        }
        return false;
    }

    /**
     * 当key不存在时,设置值,成功返回true
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean setIfNotExists(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis();
            Long statusCode = jedis.setnx(key, value);
            if (Objects.equals(SUCCESS_STATUS_LONG, statusCode)) {
                return true;
            }
        } catch (Exception e) {
        } finally {
            if (jedis != null) {
                RedisPoolUtil.returnResource(jedis);
            }
        }
        return false;
    }

    /**
     * 当key不存在时,设置值,成功返回true,同setIfNotExists
     *
     * @param key
     * @param value
     * @return
     */
    public static bo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值