Java Redis安装引用及常用操作工具类

1、Redis安装引用

1)安装

WindowsWindows上安装配置Redis

LinuxLinux上下载安装配置Redis

2)引用

Maven地址https://mvnrepository.com/artifact/redis.clients/jedis/3.0.0

Maven项目依赖:

<dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>3.0.0</version></dependency>

2、Redis工具类完整代码

1)基类BaseCache

package com.wonhero.utils.cache;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import java.net.URI;import java.util.Set;/** * 缓存基类 */public class BaseCache {    private static JedisPoolConfig jedisPoolConfig;    protected static Jedis getJedis() {        JedisPool jedisPool = null;        if (jedisPoolConfig == null) {            jedisPoolConfig = new JedisPoolConfig();            jedisPoolConfig.setMaxTotal(1024);            jedisPoolConfig.setMaxIdle(100);            jedisPoolConfig.setMaxWaitMillis(100);            jedisPoolConfig.setTestOnBorrow(false);//jedis 第一次启动时,会报错            jedisPoolConfig.setTestOnReturn(true);        }        String ip = "192.168.31.22";        int port = 6379;        int timeout = 2000;        String password = "";        // 初始化JedisPool        if (password == null || "".equals(password))            jedisPool = new JedisPool(jedisPoolConfig, ip, port, timeout);        else            jedisPool = new JedisPool(jedisPoolConfig, ip, port, timeout, password);        return jedisPool.getResource();    }    /**     * 删除数据     *     * @param pattern     */    public static void removeData(String pattern) {        Jedis redis = null;        try {            redis = getJedis();            Set<String> keys = redis.keys("*" + pattern);            if (keys == null || keys.size() == 0) {                return;            }            for (String key : keys) {                redis.del(key);            }        } finally {            if (redis != null) {                redis.close();            }        }    }}

2)实现类

package com.wonhero.utils.cache;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import redis.clients.jedis.Jedis;public class CommonCache extends BaseCache {    private final static Logger logger = LoggerFactory.getLogger(CommonCache.class);    /**     * 获取值(用户ID)     *     * @returne     */    public static String getValue(String key) {        Jedis redis = null;        //long startTime = DateUtil.getCurrentDate().getTime();        try {            redis = getJedis();            return redis.get(key);        } finally {            if (redis != null) {                redis.close();            }            //logger.debug( key + " redis 耗时:" + (DateUtil.getCurrentDate().getTime() - startTime));        }    }    /**     * 获取值(用户ID)     *     * @returne     */    public static String getAndRemoveValue(String key) {        Jedis redis = null;        String value = "";        //long startTime = DateUtil.getCurrentDate().getTime();        try {            redis = getJedis();            value = redis.get(key);            removeValue(key);        } finally {            if (redis != null) {                redis.close();            }            //logger.debug( key + "getAndRemoveValue redis 耗时:" + (DateUtil.getCurrentDate().getTime() - startTime));        }        return value;    }    /**     * 保存数据     *     * @param key     */    public static void setValue(String key, String value, int expireTime) {        Jedis redis = null;        try {            redis = getJedis();            redis.set(key, value);            redis.expire(key, expireTime);        } finally {            if (redis != null) {                redis.close();            }        }    }    /**     * 保存数据     *     * @param key     */    public static void setValue(String key, String value) {        Jedis redis = null;        try {            redis = getJedis();            redis.set(key, value);        } finally {            if (redis != null) {                redis.close();            }        }    }
public static byte[] getValue(byte[] key) { Jedis redis = null; //long startTime = DateUtil.getCurrentDate().getTime(); try { redis = getJedis(); return redis.get(key); } finally { if (redis != null) { redis.close(); } //logger.debug( key + " redis 耗时:" + (DateUtil.getCurrentDate().getTime() - startTime)); } }
/** * 保存数据 * * @param key */ public static void setValue(byte[] key, byte[] value) { Jedis redis = null; try { redis = getJedis(); redis.set(key, value); } finally { if (redis != null) { redis.close(); } } }
/** * 删除记录 * * @param key */ public static void removeValue(String key) { Jedis redis = null; try { redis = getJedis(); redis.del(key); } finally { if (redis != null) { redis.close(); } } } /** * 重设超时间 * * @param jdi * @param expireTime */ public static void resetExpireTime(String jdi, int expireTime) { Jedis redis = null; try { redis = getJedis(); redis.expire(jdi, expireTime); } finally { if (redis != null) { redis.close(); } } }}

免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除

咨询热线(9:00 - 18:00)
0755 - 29812418
微信公众号二维码
微信公众号二维码
微信公众号
返回顶部