java之jedis使用
下载
使用
# Redis settings redis.host=192.168.208.153 redis.port=6379 redis.pass=1234 redis.timeout=10000 redis.maxIdle=300 redis.maxTotal=600 # 毫秒 redis.maxWaitMillis=1000
redis.properties
package com.zze.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 属性文件加载工具类
* @author zze
*/
public class PropertyUtil {
//加载property文件到io流里面
public static Properties loadProperties(String propertyFile) {
Properties properties = new Properties();
try {
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
if (is == null) {
is = PropertyUtil.class.getClassLoader().getResourceAsStream("properties/" + propertyFile);
}
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 根据key值取得对应的value值
*
* @param key
* @return
*/
public static String getValue(String propertyFile, String key) {
Properties properties = loadProperties(propertyFile);
return properties.getProperty(key);
}
}
com.zze.util.PropertyUtil
package com.zze.util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Properties;
@SuppressWarnings("all")
public class RedisClient {
//连接池
private static JedisPool jedisPool;
static {
try {
Properties properties = PropertyUtil.loadProperties("redis.properties");
String host = properties.getProperty("redis.host");
String port = properties.getProperty("redis.port");
String pass = properties.getProperty("redis.pass");
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();
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(Integer.parseInt(maxTotal));
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(Integer.parseInt(maxIdle));
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout), pass);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取Redis资源
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis jedis = jedisPool.getResource();
return jedis;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//释放redis资源
public synchronized static void releaseConn(Jedis jedis) {
if (jedisPool != null) {
jedis.close();
}
}
}
com.zze.util.RedisClient
@Test
public void test2(){
Jedis jedis = RedisClient.getJedis();
jedis.set("name", "张三");
String name = jedis.get("name");
System.out.println(name);
}
java之jedis使用的更多相关文章
- Redis java client ==> Jedis
https://github.com/xetorthio/jedis Jedis is a blazingly small and sane Redis java client. Jedis was ...
- 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
转载地址:http://blog.csdn.net/truong/article/details/46711045 关键字:Redis的Java客户端Jedis的八种调用方式(事务.管道.分布式…)介 ...
- Redis(九):Redis的Java客户端Jedis
Redis的Java客户端Jedis导航目录: 安装JDK 安装Eclipse Jedis所需要的Jar包 Jedis常用操作 JedisPool 安装JDK tar -zxvf jdk-7u67-l ...
- 9.Redis的Java客户端Jedis
Redis的Java客户端Jedis Jedis所需jar包 commons-pool-1.6.jar jedis-2.1.0.jar 1.Jedis常用操作(jedis中的api 和 我们在 l ...
- Redis入门和Java利用jedis操作redis
Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...
- Java通过jedis操作redis缓存
package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
jedis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分布式 ...
- Java客户端Jedis的八种调用方式
redis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分 ...
- 使用Redis的Java客户端Jedis
转载自:http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 前一篇文章<Redis命令指南>讲解了通过命令行 ...
- [转载] 使用Redis的Java客户端Jedis
转载自http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 在实际的项目开发中,各种语言是使用Redis的客户端库来与Re ...
随机推荐
- 【原创 Hadoop&Spark 动手实践 4】Hadoop2.7.3 YARN原理与动手实践
简介 Apache Hadoop 2.0 包含 YARN,它将资源管理和处理组件分开.基于 YARN 的架构不受 MapReduce 约束.本文将介绍 YARN,以及它相对于 Hadoop 中以前的分 ...
- Open Cygwin at a specific folder
转自:https://stackoverflow.com/questions/9637601/open-cygwin-at-a-specific-folder# When you install Cy ...
- layer 关闭当前弹出层
关闭当前框的弹出层 layer.close(layer.index); 刷新父层 parent.location.reload(); // 父页面刷新 关闭iframe 弹出的全屏层 var inde ...
- C语言 · 空白格式化
标题:空白格式化 “空白格式化”具体做法是:去掉所有首尾空白:中间的多个空白替换为一个空格.所谓空白指的是:空格.制表符.回车符. 填空为:*p_to<*p_from: #include< ...
- SparkStreaming:关于checkpoint的弊端
当使用sparkstreaming处理流式数据的时候,它的数据源搭档大部分都是Kafka,尤其是在互联网公司颇为常见. 当他们集成的时候我们需要重点考虑就是如果程序发生故障,或者升级重启,或者集群宕机 ...
- centos7环境安装ElasticSearch
操作系统: Centos7 .64位 ========================================= 查看系统版本和系统位数: [root@localhost /]# cat /e ...
- vscode打造最佳的markdown编辑器
参考:https://www.jianshu.com/p/18876655b452 在macos下也设置成功:
- 磁盘 I/O 优化
磁盘 I/O 优化 1. 性能检测 我们的应用程序通常都需要访问磁盘系统,而磁盘 I/O 通常都很耗时, 要判断 I/O 是否是一个瓶颈,有一些参数指标可以参考. 我们可以压力测试应用程序看系统的 I ...
- 使用 maven 自动将源码打包并发布
1.maven-source-plugin 访问地址 在 pom.xml 中添加 下面的 内容,可以 使用 maven 生成 jar 的同时 生成 sources 包 <plugin> & ...
- python利用opencv合成模糊图像
之前需要评估图像质量来筛选成像质量不错的图片,去除由于对焦,运动等造成的模糊图像,所以在构建数据集的时候考虑用opencv对清晰的图片进行处理获得模糊的图片从而进行训练. 1) 运动模糊图像 一般来说 ...