Java Web中使用 Jedis 操作 Redis 数据库

  现在的redis这种内存型数据库,已经成为大型项目的必须,与很老的session存数据相比,他有很多优势。在Java中,一般使用 Jedis 扩展来使用redis数据库。

引入jar包

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

代码

数据库连接类(因为项目上线以后,可能很多参数是需要修改的,建议使用配置文件的方式配置相关参数,这样便于配置参数的修改):

public class RedisProvider {
    protected static final Logger logger = Logger.getLogger(RedisProvider.class);
    protected static JedisPool jedispool;
    private static PropertiesConfiguration properties = null;
    protected static int EXPIRE = 120;
    static{
        try {
            properties = new PropertiesConfiguration("cache/redis.properties");
        } catch (ConfigurationException e) {
            logger.error("配置文件未加载成功");
        }

        JedisPoolConfig jedisconfig = new JedisPoolConfig();
        jedisconfig.setMaxTotal(Integer.valueOf(properties
                .getString("redis.pool.maxTotal")));
        jedisconfig.setMaxIdle(Integer.valueOf(properties
                .getString("redis.pool.maxIdle")));
        jedisconfig.setMaxWaitMillis(Long.valueOf(properties
                .getString("redis.pool.maxWait")));
        jedisconfig.setTestOnBorrow(Boolean.valueOf(properties
                .getString("redis.pool.testOnBorrow")));
        jedisconfig.setTestOnReturn(Boolean.valueOf(properties
                .getString("redis.pool.testOnReturn")));
        jedispool = new JedisPool(jedisconfig, properties.getString("redis.ip"),
                Integer.valueOf(properties.getString("redis.port")), 100000);
    }

    public static Jedis getJedis() {
        Jedis jedis = null;
        try {
            jedis = jedispool.getResource();
        } catch (JedisConnectionException jce) {
            logger.warn("获取连接失败:" + jce.getMessage());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                logger.warn("线程睡眠失败:" + e.getMessage());
            }
            jedis = jedispool.getResource();
        }
        return jedis;
    }

    public static void closeJedis(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

数据操作类(简单的写了几个示例):

public class RedisHelper extends RedisProvider {
    private static final Logger logger = Logger.getLogger(RedisHelper.class);

    /**
     * 保存键值对,过期时间为默认值(默认2分钟)
     * @param key
     * @param value
     */
    public static void set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.setex(key, EXPIRE, value);
        } catch (Exception e) {
            logger.error("默认时间set:" + e.getMessage());
        } finally {
            closeJedis(jedis);
        }
    }

    /**
     * 保存键值对,自定义过期时间(单位s)
     * @param key
     * @param value
     * @param expire
     */
    public static void set(String key, String value, int expire) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.setex(key, expire, value);
        } catch (Exception e) {
            logger.error("自定义时间set:" + e.getMessage());
        } finally {
            closeJedis(jedis);
        }
    }

    /**
     * 根据key获取value
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = getJedis();
            value = jedis.get(key);
        } catch (Exception e) {
            logger.error("根据key获取value:" + e.getMessage());
        } finally {
            closeJedis(jedis);
            return value;
        }
    }

    /**
     * 删除键值对,根据所提供的key
     * @param key
     */
    public static void clear(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.del(key);
        } catch (Exception e) {
            logger.error("根据key删除值:" + e.getMessage());
        } finally {
            closeJedis(jedis);
        }
    }

    /**
     * 检查值是否存在
     * @param key
     * @return
     */
    public static boolean exists(String key) {
        Jedis jedis = null;
        boolean bool = false;
        try {
            jedis = getJedis();
            bool = jedis.exists(key);
        } catch (Exception e) {
            logger.error("判断值是否存在" + e.getMessage());
        } finally {
            closeJedis(jedis);
            return bool;
        }
    }

}

redis on windows: https://github.com/MicrosoftArchive/redis/releases
GUI工具:https://github.com/uglide/RedisDesktopManager

0

发表评论