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

新建属性文件工具类,用来读取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);
} }

新建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;
} }

写一个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); }
}

最新文章

  1. SET NOCOUNT 怎么理解
  2. H5(一)
  3. UED
  4. IOS雕虫小技
  5. windows下添加mysql服务
  6. Ios中比较两个日期之间的时间差距
  7. bzoj2326: [HNOI2011]数学作业
  8. 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)
  9. 深入理解C#:编程技巧总结(一)
  10. Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法
  11. java学习之多线程
  12. HDU 3068 最长回文 【最长回文子串】
  13. JSON【介绍、语法、解析JSON】
  14. js+jq实现图片预览,支持到ie9+ff+chrome
  15. mongodb安装失败与解决方法(附安装教程)
  16. Go语言标准库之time
  17. js后退
  18. BZOJ4553: [Tjoi2016&amp;Heoi2016]序列 树套树优化DP
  19. Makefile-filter和filter-out
  20. 高性能的城市定位API接口

热门文章

  1. 在论坛中出现的比较难的sql问题:16(取一个字段中的数字)
  2. ef core2.2 mysql迁移问题
  3. java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapa
  4. 使用VS2012编译和使用C++ STL(STLport)
  5. 【小知识点】js无需刷新在url地址添加参数
  6. javascript_12-递归
  7. DataGrip像navicat一样导入导出表数据,不是导出导入insert和update这种
  8. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio
  9. Windows下静态库的制作与使用
  10. 运维开发笔记整理-template的使用