redis maven 搭建

来源:互联网 发布:淘宝图片引是什么意思 编辑:程序博客网 时间:2024/06/07 08:12

redis.properties

#config for redisredis.pool.maxActive=512redis.pool.maxIdle=100redis.pool.maxWait=100000redis.pool.testOnBorrow=trueredis.pool.testOnReturn=trueredis.ip=127.0.0.1redis.port=6379redis.expire=1200

pom.xml 依赖包

  <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->    <dependency>      <groupId>org.apache.commons</groupId>      <artifactId>commons-lang3</artifactId>      <version>3.4</version>    </dependency>    <!-- redis.properties -->    <dependency>      <groupId>redis.clients</groupId>      <artifactId>jedis</artifactId>      <version>2.1.0</version>    </dependency>

RedisUtils

package com.saudio.common.redis;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.exceptions.JedisConnectionException;import java.util.Locale;import java.util.ResourceBundle;/** * Created by hongwei on 2016/9/25. */public class RedisUtils {    protected static JedisPool jedispool;    protected static int EXPIRE = 130;    static{        Locale locale1 = new Locale("zh", "CN");        ResourceBundle bundle = ResourceBundle.getBundle("redis",locale1);        if (bundle == null) {            throw new IllegalArgumentException(                    "[redis.properties] is not found!");        }        EXPIRE = Integer.valueOf(bundle.getString("redis.expire"));        JedisPoolConfig jedisconfig = new JedisPoolConfig();        jedisconfig.setMaxActive(Integer.valueOf(bundle                .getString("redis.pool.maxActive")));        jedisconfig.setMaxIdle(Integer.valueOf(bundle                .getString("redis.pool.maxIdle")));        jedisconfig.setMaxWait(Long.valueOf(bundle                .getString("redis.pool.maxWait")));        jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle                .getString("redis.pool.testOnBorrow")));        jedisconfig.setTestOnReturn(Boolean.valueOf(bundle                .getString("redis.pool.testOnReturn")));        jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"),                Integer.valueOf(bundle.getString("redis.port")), 100000);    }    public static Jedis getJedis() {        Jedis jedis = null;        try {            jedis = jedispool.getResource();        } catch (JedisConnectionException jce) {            jce.getStackTrace();            try {                Thread.sleep(3000);            } catch (InterruptedException e) {                jce.getStackTrace();            }            jedis = jedispool.getResource();        }        return jedis;    }    public static void returnResource(JedisPool pool, Jedis jedis) {        if (jedis != null) {            pool.returnResource(jedis);        }    }    public static void setKey(String key , String value , int seconds){        Jedis jedis = getJedis();        jedis.set(key,value);        jedis.expire(key,seconds);    }    public String getKey(String key){        return getJedis().get(key);    }}
0 0
原创粉丝点击