Redis工具之Jedis
//jedis的连接池
public void test1(){
//创建连接池配置对象
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(20); //最大资源个数
//创建连接池
//poolConfig 连接池的配置信息对象
JedisPool jedisPool = new JedisPool(poolConfig, "192.168.**.**", 6379);
//获得连接资源
Jedis jedis = jedisPool.getResource();
jedis.set("name", "张三疯"); //set name 张三疯
jedis.hset("user", "username", "芷若"); // hset user username 芷若
//取出数据
String name = jedis.get("name");
String hget = jedis.hget("user", "username");
System.out.println(name);
System.out.println(hget);
//释放资源
jedis.close();
jedisPool.close();
}
//封装后的工具类
public class RedisUtils {
private static JedisPoolConfig poolConfig = null;
private static JedisPool jedisPool = null;
static{
try {
//读取配置文件 为连接参数赋值
/*InputStream inStream = RedisUtils.class.getClassLoader().getResourceAsStream("redis.properties");
Properties props = new Properties();
props.load(inStream);
String host = props.getProperty("redis.host");
int port = Integer.parseInt(props.getProperty("redis.port"));
int maxTotal = Integer.parseInt(props.getProperty("redis.maxTotal"));*/
//专门读取properties配置文件的
//baseName:基本名称 不包含properties扩展名的名称
//getBundle方法参数相对于src的地址
ResourceBundle rb = ResourceBundle.getBundle("redis");
String host = rb.getString("redis.host");
int port = Integer.parseInt(rb.getString("redis.port"));
int maxTotal = Integer.parseInt(rb.getString("redis.maxTotal"));
poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxTotal);
jedisPool = new JedisPool(poolConfig,host, port);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Jedis getResource(){
Jedis jedis = jedisPool.getResource();
return jedis;
}
}
//为了降低耦合性,将连接池的配置信息放入redis.properties配置文件中
redis.host=192.168.**.**
redis.port=6379
redis.maxTotal=20
//测试工具的使用
//测试jedis工具的使用
public void test2(){ Jedis jedis = RedisUtils.getResource();
String name = jedis.get("name");
System.out.println(name);
jedis.close(); }
Redis工具之Jedis的更多相关文章
- Jedis 操作 Redis 工具类
配置类 pom.xml pom.xml 里配置依赖 <dependency> <groupId>redis.clients</groupId> <artifa ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- redis 工具类 单个redis、JedisPool 及多个redis、shardedJedisPool与spring的集成配置
http://www.cnblogs.com/edisonfeng/p/3571870.html http://javacrazyer.iteye.com/blog/1840161 http://ww ...
- Redis 订阅发布 - Jedis实现
Redis 订阅发布 - Jedis实现 我想到使用Redis的订阅发布模式是用来解决推送问题的-. 对于概念性的叙述,多多少少还是要提一下的: 什么是Redis发布订阅?Redis发布订阅是一种 ...
- Redis 工具类 java 实现的redis 工具类
最近了解了一下非关系型数据库 redis 会使用简单的命令 在自己本地电脑 使用时必须先启动服务器端 在启动客户端 redis 简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...
- Java操作Redis工具类
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- spring boot 使用redis 及redis工具类
1-添加maven依赖 2-添加redis配置 3-工具类 1-添加maven依赖 实际上是封装了jedis <!-- redis 依赖--> <dependency> < ...
- java中的redis工具类
1.redis基础类 package com.qlchat.component.redis.template; import javax.annotation.PostConstruct; impor ...
随机推荐
- BlackLowKey主题CSS
/* Minification failed. Returning unminified contents. (151,61): run-time error CSS1062: Expected se ...
- JavaScript对象 继承
JavaScript继承主要依靠原型链实现. 原型链 利用原型让一个引用类型继承另一个引用类型水位属性和方法. 每一个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指 ...
- js之箭头函数
原文 ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: ...
- sharepoint2007就地升级2010系列(一)2007概览
大家可能熟悉了sharepoint2010,熟悉了sharepoint2013,做了N多个项目,但是有没有做过从sharepoint2007升级或者迁移到sharepoint2010的项目呢?我们做实 ...
- ASP.NET全局异常处理
Web项目部署后,异常直接暴露给用户会产生很不好的体验.只是暴露在服务器端又无法实时记录异常原因以便加以重现并修复.所以配合Log4Net记录日志信息,同时全局异常处理来营造良好用户体验就比较重要了. ...
- Linux CentOS如何汉化系统
su root切换为root用户 写入环境变量 echo "export LANG="zh_CN.UTF8"">>/etc/profile sour ...
- 爬虫入门之Requests模块学习(四)
1 Requests模块解析 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用 Requests 继承了urllib2的所有特性.Requests支持HTTP连接保 ...
- 【Leetcode】【Easy】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- June 17th 2017 Week 24th Saturday
Absence sharpens love, presence strengthens it. 相聚爱益切,离别情更深. There is almost no such love that can i ...
- android中View点击和触摸事件的处理
android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解. 一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN-& ...