<!--集成的RedisJAR-->
<!--引入jedis需的jar包-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--Spring整合jedis的依赖-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
package com.dsj.gdbd.utils.jedis;

import com.dsj.gdbd.utils.serialize.SerializingUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; public class RedisPoolUtil implements InitializingBean {
private static JedisPool pool = null;
/**
* 默认缓存时间
*/
private static final int DEFAULT_CACHE_SECONDS = 0;// 单位秒 设置成一个钟
private static Logger LOGGER = Logger.getLogger(RedisPoolUtil.class); public static JedisPool getPool() {
return pool;
} @PostConstruct
public void init() { if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10000);
config.setMaxIdle(1000);
config.setMaxWaitMillis(1000 * 10); //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
//new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
//pool = new JedisPool(config, "47.94.15.160", 6379, 10000, "Fanthful@123");
pool = new JedisPool(config, "192.168.31.206", 6379, 10000);
}
} public synchronized static Jedis getResource() {
if (pool == null) {
pool = getPool();
}
return pool.getResource();
}
public static void closeRedis(Jedis redis) {
if (redis != null) {
redis.close();
}
}
public static void set(String key, String value) {
Jedis redis = getResource();
try {
redis.set(key, value);
} finally {
closeRedis(redis);
}
} public static void set(Object key, Object value) {
Jedis redis = getResource();
try {
redis.set(SerializingUtil.serialize(key), SerializingUtil.serialize(value));
} finally {
closeRedis(redis);
}
}
public static String get(String key) {
Jedis redis = getResource();
try {
return redis.get(key);
} finally {
closeRedis(redis);
}
} /**
* 根据缓存键获取Redis缓存中的值.<br/>
*
* @param key 键.<br/>
* @return Object .<br/>
* @throws Exception
*/
public static Object get(Object key) {
Jedis jedis = null;
try {
jedis = getResource();
byte[] obj = jedis.get(SerializingUtil.serialize(key));
return obj == null ? null : SerializingUtil.deserialize(obj);
} catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return null;
} finally {
closeRedis(jedis);
}
}
/**
* 根据缓存键获取Redis缓存中的值.<br/>
*
* @param key 键.<br/>
* @return Object .<br/>
* @throws Exception
*/
public static Object getObj(String key) {
Jedis jedis = null;
try {
jedis = getResource();
byte[] obj = jedis.get(SerializingUtil.serialize(key));
return obj == null ? null : SerializingUtil.deserialize(obj);
} catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return null;
} finally {
closeRedis(jedis);
}
}
/**
* 根据缓存键获取Redis缓存中的值.<br/>
*
* @param key 键.<br/>
* @return Object .<br/>
* @throws Exception
*/
public static String getStr(String key) {
Jedis jedis = null;
try {
jedis = getResource();
return jedis.get(key); } catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return null;
} finally {
closeRedis(jedis);
}
} /**
* 根据缓存键获取Redis缓存中的值.<br/>
*
* @param key 键.<br/>
* @return Object .<br/>
* @throws Exception
*/
public static byte[] get(byte[] obj) {
Jedis jedis = null;
try {
jedis = getResource();
return jedis.get(obj);
} catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return null;
} finally {
closeRedis(jedis);
}
} /**
* 判断一个key是否存在
*
* @param key
* @return
*/
public static Boolean exists(Object key) {
Jedis jedis = null;
Boolean result = false;
try {
jedis = getResource();
return jedis.exists(SerializingUtil.serialize(key));
} catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return false;
} finally {
closeRedis(jedis);
}
} public static Boolean existsKey(String key) {
Jedis jedis = null;
Boolean result = false;
try {
jedis = getResource();
return jedis.exists(key);
} catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return false;
} finally {
closeRedis(jedis);
}
} /**
* 根据缓存键清除Redis缓存中的值.<br/>
*
* @param keys
* @return
* @throws Exception
*/
public static Boolean del(Object... keys) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.del(SerializingUtil.serialize(keys));
return true;
} catch (Exception e) {
LOGGER.error("Cache删除失败:" + e);
return false;
} finally {
closeRedis(jedis);
}
} /**
* 根据缓存键清除Redis缓存中的值.<br/>
*
* @param keys
* @return
* @throws Exception
*/
public static Long del(String... keys) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.del(keys);
return jedis.del(keys);
} catch (Exception e) {
LOGGER.error("Cache删除失败:" + e);
return 0l;
} finally {
closeRedis(jedis);
}
} /**
* 保存一个对象到Redis中(缓存过期时间:使用此工具类中的默认时间) . <br/>
*
* @param key 键 . <br/>
* @param object 缓存对象 . <br/>
* @return true or false . <br/>
* @throws Exception
*/
public static Boolean save(Object key, Object object) {
return save(key, object, DEFAULT_CACHE_SECONDS);
} /**
* 保存一个对象到redis中并指定过期时间
*
* @param key 键 . <br/>
* @param object 缓存对象 . <br/>
* @param seconds 过期时间(单位为秒).<br/>
* @return true or false .
*/
public static Boolean save(Object key, Object object, int seconds) {
Jedis jedis = null;
try {
jedis = getResource();
jedis.set(SerializingUtil.serialize(key), SerializingUtil.serialize(object));
jedis.expire(SerializingUtil.serialize(key), seconds);
return true;
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Cache保存失败:" + e);
return false;
} finally {
closeRedis(jedis);
}
}
/**
* 删除Redis中的所有key
*
* @throws Exception
*/
public static void flushAll() {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.flushAll();
} catch (Exception e) {
LOGGER.error("Cache清空失败:" + e);
} finally {
closeRedis(jedis);
}
}
/**
* 获取list
*
* @param <T>
* @param key
* @return list
*/
public static <T> Map<String, T> getMap(String key) throws Exception {
if (getResource() == null || !getResource().exists(key.getBytes())) {
return null;
}
byte[] in = getResource().get(key.getBytes());
return (Map<String, T>) SerializingUtil.deserialize(in);
}
/**
* 设置 map
*
* @param <T>
* @param key
*/
public static <T> void setMap(String key, Map<String, T> map) {
try {
getResource().set(key.getBytes(), SerializingUtil.serialize(map));
} catch (Exception e) {
LOGGER.warn("Set key error : " + e);
}
}
public static Long dbSize() {
Jedis jedis = null;
Long size = 0l;
try {
jedis = pool.getResource();
size = jedis.dbSize();
} catch (Exception e) {
LOGGER.error("查询库异常:" + e);
} finally {
closeRedis(jedis);
}
return size;
} public static Set<String> keys(String pattern) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Set<String> allKey = jedis.keys("*" + pattern + "*");
return allKey;
} catch (Exception e) {
LOGGER.error("Cache获取失败:" + e);
return new HashSet<String>();
} finally {
closeRedis(jedis);
}
} public static void flushDB() {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.flushDB();
} catch (Exception e) {
LOGGER.error("Cache清空失败:" + e);
} finally {
closeRedis(jedis);
}
} public static boolean setex(byte[] key, byte[] value, int expire) {
Jedis redis = getResource();
try {
redis.setex(key, expire, value);
return true;
} catch (Exception e) {
LOGGER.error("保存redis:" + e);
return false;
} finally {
closeRedis(redis);
}
} public static boolean setex(String key, String value, int expire) {
Jedis redis = getResource();
try {
redis.setex(key, expire, value);
return true;
} catch (Exception e) {
LOGGER.error("保存redis:" + e);
return false;
} finally {
closeRedis(redis);
}
}
public static boolean setByte(byte[] key, byte[] value) {
Jedis redis = getResource();
try {
redis.set(key, value);
return true;
} catch (Exception e) {
LOGGER.error("保存redis:" + e);
return false;
} finally {
closeRedis(redis);
}
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
}
}

单机版 RedisPoolUtil({基本操作封装工具类})【一】的更多相关文章

  1. 单机版 RedisUtils({基本操作封装工具类})【三】

    <!--集成的RedisJAR--> <!--引入jedis需的jar包--> <dependency> <groupId>redis.clients& ...

  2. 单机版 JedisUtil({基本操作封装工具类})【二】

    <!--集成的RedisJAR--> <!--引入jedis需的jar包--> <dependency> <groupId>redis.clients& ...

  3. JAVA之旅(五)——this,static,关键字,main函数,封装工具类,生成javadoc说明书,静态代码块

    JAVA之旅(五)--this,static,关键字,main函数,封装工具类,生成javadoc说明书,静态代码块 周末收获颇多,继续学习 一.this关键字 用于区分局部变量和成员变量同名的情况 ...

  4. Android OkHttp网络连接封装工具类

    package com.lidong.demo.utils; import android.os.Handler; import android.os.Looper; import com.googl ...

  5. 泛型(二)封装工具类CommonUtils-把一个Map转换成指定类型的javabean对象

    1.commons-beanutils的使用 commons-beanutils-1.9.3.jar 依赖 commons-logging-1.2.jar 代码1: String className ...

  6. MySQL JDBC常用知识,封装工具类,时区问题配置,SQL注入问题

    JDBC JDBC介绍 Sun公司为了简化开发人员的(对数据库的统一)操作,提供了(Java操作数据库的)规范,俗称JDBC,这些规范的由具体由具体的厂商去做 对于开发人员来说,我们只需要掌握JDBC ...

  7. 超简单的okhttp封装工具类(上)

      版权声明:转载请注明出处:http://blog.csdn.net/piaomiao8179 https://blog.csdn.net/piaomiao8179/article/details/ ...

  8. ViewPager封装工具类: 轻松实现APP导航或APP中的广告栏

    相信做app应用开发的,绝对都接触过ViewPager,毕竟ViewPager的应用可以说无处不在:APP第一次启动时的新手导航页,APP中结合Fragment实现页面滑动,APP中常见的广告栏的自动 ...

  9. FTP上传-封装工具类

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

随机推荐

  1. cat 命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  2. Spring在注入bean异常时的替换

    情形:需要把数据库1的连接池A注入到DAO中,但是如果数据库A的连接池实例化失败了整个项目也启动不了,这个时候希望用数据库2的连接池来替换. 这里没有想到什么好的解决方法,只是想到了工厂方法提供Bea ...

  3. use-svn-cmd(Linux)

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,它采用了分支管理系统,是一个跨平台的软件,支持大多数常见的操作系统. svn命令行使用: 1.查看svn所支持的全部命令 $ svn ...

  4. iOS开发之 AES+Base64数据混合加密与解密

    2016-04-08 09:03 编辑: liubinqww 分类:iOS开发 来源:liubinqww 投稿 4 889     "APP的数据安全已经牵动着我们开发者的心,简单的MD5/ ...

  5. 安装Struts2 类库

    现在,如果一切正常,那么你可以继续设置您的Struts 2框架.以下是简单的步骤,下载并安装在机器上Struts2. 请选择是否要安装Hibernate在Windows或Unix,然后继续进行下一个步 ...

  6. winerror.h中的内容(可以查看last error对应)

    /************************************************************************* ** winerror.h -- error co ...

  7. ios 深入讲解iOS键盘一:控制键盘隐藏显示

    在iOS的开发中,我们一般使用UITextField.UITextView处理文字输入等操作,大部分情况下我们只需要一两行代码去手动管理键盘的显示隐藏:让UITextField或UITextView成 ...

  8. 如何通过git客户端上传项目到github上

    参考地址: 1.http://1ke.co/course/194 2.https://github.com/wohugb/git-reference/blob/master/Git-on-the-Se ...

  9. SpringMVC基于代码的配置方式(零配置,无web.xml)

    基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...

  10. web.xml配置整理

    虽然是做web开发,但是web中的很多配置有的时候却不是很清楚,只是知道怎么配置,于是就把在网上看到各种关于web.xml的东西整理一下: web.xml中url-pattern的3种写法 1完全匹配 ...