码迷,mamicode.com
首页 > 数据库 > 详细

Jedis API操作redis数据库

时间:2019-11-02 13:43:34      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:存在   名称   conf   private   input   get   one   onclick   timeout   

1、配置文件

classpath路径下,新建redis.properties配置文件

配置文件内容

技术图片
# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=600
# 毫秒
redis.maxWaitMillis=1000
redis.testOnBorrow=false
View Code

新建属性文件工具类,用来读取redis.properties配置文件

技术图片
/**
 * <p>属性文件工具类
 *
 * @author xupeng
 * @date 2019/10/28 10:39
 */
public class PropertyUtil {

    //加载property文件到io流里面
    public static Properties loadProperties(String fileName) {
        Properties properties = new Properties();
        try {
            InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(fileName);
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 根据key值取得对应的value值
     *
     * @param key
     * @return
     */
    public static String getValue(String fileName, String key) {
        Properties properties = loadProperties(fileName);
        return properties.getProperty(key);
    }

}
View Code

新建Jedis工具类,封装常用方法

技术图片
/**
 * <p>Jedis工具类
 *
 * @author xupeng
 * @date 2019/10/28 11:02
 */
public class JedisUtil {

    private Logger logger = LoggerFactory.getLogger(JedisUtil.class);

    private static JedisPool jedisPool = null;

    private JedisUtil(){}

    static {
        Properties properties = PropertyUtil.loadProperties("redis.properties");
        String host = properties.getProperty("redis.host");
        String port = properties.getProperty("redis.port");
        String timeout = properties.getProperty("redis.timeout");
        String maxIdle = properties.getProperty("redis.maxIdle");
        String maxTotal = properties.getProperty("redis.maxTotal");
        String maxWaitMillis = properties.getProperty("redis.maxWaitMillis");
        String testOnBorrow = properties.getProperty("redis.testOnBorrow");

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(Integer.parseInt(maxIdle));
        config.setMaxTotal(Integer.parseInt(maxTotal));
        config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
        config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
        jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout));
    }

    /**
     * <p>从jedis连接池中获取获取jedis对象
     */
    private Jedis getJedis() {
        return jedisPool.getResource();
    }

    private static final JedisUtil jedisUtil = new JedisUtil();

    /**
     * <p>获取JedisUtil实例
     */
    public static JedisUtil getInstance() {
        return jedisUtil;
    }

    /**
     * <p>设置值
     * @param key 键
     * @param value 值
     */
    public String set(String key,String value){
        String status = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                status = jedis.set(key, value);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return status;
    }

    /**
     * <p>根据键名称获取值
     * @param key 键名称
     * @return*/
    public String get(String key){
        Jedis jedis = null;
        String value = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                value = jedis.get(key);

            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return value;
    }

    /**
     * <p>如果存在,不设置,返回0;不存在,进行设置,返回1。
     * @param key 键
     * @param value 值
     */
    public Long setnx(String key,String value){
        Long setnx = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                setnx = jedis.setnx(key, value);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return setnx;
    }

    /**
     * <p>设置key的有效时间
     * @param key 键
     * @param value 值
     */
    public String setex(String key,int seconds,String value){
        String setex = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                setex = jedis.setex(key, seconds, value);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return setex;
    }

    /**
     * <p>对某个值进行递增
     * @param key 键
     */
    public Long incr(String key){
        Long incr = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                incr = jedis.incr(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return incr;
    }

    /**
     * <p>对某个值进行递减
     * @param key 键
     */
    public Long decr(String key){
        Long incr = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                incr = jedis.decr(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return incr;
    }

    /**
     * <p>设置一个map类型的值
     *
     * @author zhuangxupeng
     * @date 2019/10/31 10:30
     */
    public String hmset(String key, Map<String,String> map){
        Jedis jedis = null;
        String hmset = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                hmset = jedis.hmset(key, map);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return hmset;
    }

    /**
     * <p>获取一个map类型的值
     *
     * @author zhuangxupeng
     * @date 2019/10/31 10:30
     */
    public Map<String, String> hgetAll(String key){
        Jedis jedis = null;
        Map<String, String> map = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                map = jedis.hgetAll(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return map;
    }

    /**
     * <p>获取key对应map的大小
     *
     * @author zhuangxupeng
     * @date 2019/10/31 10:30
     */
    public Long hlen(String key){
        Jedis jedis = null;
        Long hlen = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                hlen = jedis.hlen(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return hlen;
    }

    /**
     * <p>获取key对应map的所有的键的集合
     *
     * @author zhuangxupeng
     * @date 2019/10/31 10:30
     */
    public Set<String> hkeys(String key){
        Jedis jedis = null;
        Set<String> sets = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                sets = jedis.hkeys(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return sets;
    }

    /**
     * <p>获取key对应map的所有的值的集合
     *
     * @author zhuangxupeng
     * @date 2019/10/31 10:30
     */
    public List<String> hvals(String key){
        Jedis jedis = null;
        List<String> list = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                list = jedis.hvals(key);

            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return list;
    }



    /**
     * <p>删除
     *
     * @param key 键名称
     * @return del 删除成功返回1,失败返回0
     */
    public long del(String key) {
        Jedis jedis = null;
        Long del = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                del = jedis.del(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return del;
    }

    /**
     * <p>是否存在KEY
     * @param key 键
     */
    public boolean exists(String key) {
        Jedis jedis = null;
        Boolean exists = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                exists = jedis.exists(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return exists;
    }

    /**
     * <p>设置失效时间
     * @param key 键名称
     * @param seconds 秒数
     */
    public Long expire(String key, int seconds) {
        Jedis jedis = null;
        Long aLong = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                aLong = jedis.expire(key, seconds);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return aLong;
    }

    /**
     * 删除失效时间
     * @param key
     */
    public Long persist(String key) {
        Jedis jedis = null;
        Long persist  = null;
        try {
            jedis = getJedis();
            if (null != jedis){
                persist = jedis.persist(key);
            }
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return persist;
    }

    /**
     * <p>添加sorted set
     * <p>已经存在的值,再次添加会失败。
     *
     * @param key 键名称
     * @param score 分数值
     * @param value 实际值
     * @return zadd 成功返回1,失败返回0。
     */
    public Long zadd(String key,double score,String value) {
        Jedis jedis = null;
        Long zadd = null;
        try {
            jedis = getJedis();
            zadd = jedis.zadd(key, score, value);
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return zadd;
    }

    /**
     * 返回指定下标的集合元素
     * @param key
     * @param start 0为第一个
     * @param end -1为最后一个
     * @return
     */
    public Set<String> zrange(String key, int start, int end) {
        Jedis jedis = null;
        Set<String> sets = null;
        try {
            jedis = getJedis();
            sets = jedis.zrange(key, start, end);
        }catch (Exception e){
            logger.info("Unable to get connection from connection pool");
        }finally {
            if (null != jedis){
                jedis.close();
            }
        }
        return sets;
    }

}
View Code

写一个main方法,来进行简单测试

技术图片
/**
 * <p>Jedis客户端操作redis的string数据类型
 * @author xupeng
 * @date 2019年10月28日
 */
public class JedisStringDemo {
    public static void main(String[] args) {

        JedisUtil instance = JedisUtil.getInstance();
        instance.set("name", "zhuang");

        String getNameVal = instance.get("name");
        System.out.println("get name value:" + getNameVal);

    }
}
View Code

 

Jedis API操作redis数据库

标签:存在   名称   conf   private   input   get   one   onclick   timeout   

原文地址:https://www.cnblogs.com/beanbag/p/11781298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!