Java封装读取Redis数据库数据的工具类【杭州多测师_王sir】

package cn.duoceshi.springbootdemo.utils;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisUtils {

    private static JedisPool jedisPool;

    static {
        Map props = PropertyUtils.getAll(RedisUtils.class.getResourceAsStream("/application.properties"));
        String ip = props.get("redis.host");
        int port = Integer.parseInt(props.get("redis.port"));
        int timeout = 60000;
        String password = props.get("redis.password");

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxActive(10);
        config.setMaxIdle(5);
        config.setMaxWait(5000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        config.setTestWhileIdle(true);
        config.setMinEvictableIdleTimeMillis(60000L);
        config.setTimeBetweenEvictionRunsMillis(3000L);
        config.setNumTestsPerEvictionRun(-1);
        jedisPool = new JedisPool(config, ip, port, timeout, password);
    }


    public static void close(Jedis jedis) {
        try {
            jedisPool.returnResource(jedis);
        } catch (Exception e) {
            if (jedis.isConnected()) {
                jedis.quit();
                jedis.disconnect();
            }
        }
    }


    public static String get(String key) {

        String value = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } finally {
            //返还到连接池
            close(jedis);
        }

        return value;
    }

    public static void set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    public static Set keys(String keyFormat) {

        Jedis jedis = null;
        Set set = new HashSet<>();
        try {
            jedis = jedisPool.getResource();
            set = jedis.keys(keyFormat);
        } finally {
            //返还到连接池
            close(jedis);
        }
        return set;
    }


    public static void hset(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hset(key, field, value);
        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    /**
     * 获取数据
     *
     * @param key
     * @return
     */
    public static String hget(String key, String field) {

        String value = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hget(key, field);
        } finally {
            //返还到连接池
            close(jedis);
        }

        return value;
    }


    /**
     * 发布消息
     *
     * @param channel
     * @param message
     */
    public static void publish(String channel, String message) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.publish(channel, message);
        } finally {
            //返还到连接池
            close(jedis);
        }
    }



    /**
     * 向名为key的list尾部添加一个值为value的元素
     *
     * @param key
     * @param value
     */
    public static void rpush(String key, String value) {
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.rpush(key, value);

        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    /**
     * 向名为key的list尾部添加一个值为value的元素
     * @param key
     * @param valueList
     */
    public static void rpush(String key,List valueList){
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            String[] valueArray = new String[valueList.size()];
            valueList.toArray(valueArray);
            jedis.rpush(key, valueArray);

        } catch (Exception e) {

            //释放redis对象
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);

        }
    }



    public static String lindex(String key, Integer index) {
        Jedis jedis = null;
        String value = null;
        try {

            jedis = jedisPool.getResource();
            value = jedis.lindex(key, index);

        } finally {

            //返还到连接池
            close(jedis);

        }
        return value;
    }


    public static void hmset(Object key, Map hash) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key.toString(), hash);
        } finally {
            //返还到连接池
            close(jedis);

        }
    }

    public static void hmset(Object key, Map hash, int time) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key.toString(), hash);
            jedis.expire(key.toString(), time);
        } finally {
            //返还到连接池
            close(jedis);

        }
    }

    public static List hmget(Object key, String... fields) {
        List result = null;
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            result = jedis.hmget(key.toString(), fields);

        } finally {
            //返还到连接池
            close(jedis);
        }
        return result;
    }

    public static Set hkeys(String key) {
        Set result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.hkeys(key);

        } finally {
            //返还到连接池
            close(jedis);
        }
        return result;
    }

    public static List lrange(String key, int from, int to) {
        List result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.lrange(key, from, to);

        } finally {
            //返还到连接池
            close(jedis);

        }
        return result;
    }

    public static Map hgetAll(String key) {
        Map result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.hgetAll(key);
        } finally {
            //返还到连接池
            close(jedis);
        }
        return result;
    }

    public static void del(String key) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    public static long llen(String key) {

        long len = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            len = jedis.llen(key);
        } finally {
            //返还到连接池
            close(jedis);
        }
        return len;
    }


    public static void main(String[] args) {
        System.out.println(RedisUtils.get("name"));
    }


}

你可能感兴趣的:(数据库,java,redis,开发语言,缓存)