jedis入门一
一.下载Jedis的依赖包jedis-2.1.0.jar,然后将其添加到classpath下面。
1. 定义连接:Redis暂时不要设置登录密码
Jedis jedis = new Jedis("192.168.142.12");
2. 进行键值存储:
jedis.set("country", "China");
3. 获取value值:
String country = jedis.get("country");
4. 删除key:
jedis.del("country");
二、使用连接池:
1. 添加依赖包commons-pool.jar,注意不要选择高版本,以免不必要的错误。
2. 配置属性文件:redis.properties
redis.host=192.168.142.12 #Redis服务器地址
redis.port=6379 #服务端口
redis.timeout=3000 #超时时间:单位ms
redis.password=nick123 #授权密码
redis.pool.maxActive=200 #最大连接数:能够同时建立的“最大链接个数”
redis.pool.maxIdle=20 #最大空闲数:空闲链接数大于maxIdle时,将进行回收
redis.pool.minIdle=5 #最小空闲数:低于minIdle时,将创建新的链接
redis.pool.maxWait=3000 #最大等待时间:单位ms
redis.pool.testOnBorrow=true #使用连接时,检测连接是否成功
redis.pool.testOnReturn=true #返回连接时,检测连接是否成功
3. 加载属性文件:redis.properties
ResourceBundle bundle = ResourceBundle.getBundle("redis");
4. 创建配置对象:
JedisPoolConfig config = new JedisPoolConfig();
String host = bundle.getString("redis.host");
...
config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
...
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
...
5. 创建Jedis连接池:
JedisPool pool = new JedisPool(config, host, port, timeout, password);
[三]. 使用方式:
1. 从连接池获取Jedis对象:
Jedis jedis = pool.getResource();
2. 基本操作:
jedis.set("province", "shannxi");
String province = jedis.get("province");
jedis.del("province");
3. 将Jedis对象归还给连接池:
pool.returnResource(jedis);
三、jedis与spring整合
[一]. 搭建环境:
1. 在之前版本的基础之上,添加如下的依赖:
spring.jar
commons-logging.jar
log4j-1.2.15.jar
同时添加日志配置文件:log4j.properties到classpath下面。
2. 配置Spring文件:applicationContext.xml
注意:连接池jedisPool的配置,这里使用了构造方式注入,这是和Jedis的API一致的;
在注入port时,需要使用使用type = "int"指定注入的参数类型,否则出现异常。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 加载redis配置文件 -->
<context:property-placeholder location="classpath:redis.properties"/> <!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.maxActive}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="maxWait" value="${redis.pool.maxWait}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
</bean> <!-- redis的连接池pool,不是必选项:timeout/password -->
<bean id = "jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1" value="${redis.host}"/>
<constructor-arg index="2" value="${redis.port}" type="int"/>
<constructor-arg index="3" value="${redis.timeout}" type="int"/>
<constructor-arg index="4" value="${redis.password}"/>
</bean> </beans>
XML
[二]. 从SPring容器中获取JedisPool:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JedisPool pool = (JedisPool) context.getBean("jedisPool");
Jedis jedis = pool.getResource();
...
pool.returnResource(jedis);
JAVA
[三]. 缓存JavaBean:
上一篇文章中,已经有了对Jedis使用的基本说明。
当然很多时候我们都希望Redis能够对JavaBean进行缓存,这需要借助于JDK提供的序列化技术。
1. 要求缓存实体实现了序列化Serializable接口:这里以Userinfo为例。
2. 序列化工具类:Jedis对序列化的支持,是提供了字节数组byte[]作为参数;
为此编写SerializingUtil工具类负责byte[]和JavaBean之间的相互转换。该方法的API如下所示:
public static byte[] serialize(Object source);
public static Object deserialize(byte[] source);
/**
* 功能简述: 序列化工具类,负责byte[]和Object之间的相互转换.
* @author Nick Xu
* @version 1.0
*/
public class SerializingUtil { private static Log logger = LogFactory.getLog(SerializingUtil.class); /**
* 功能简述: 对实体Bean进行序列化操作.
* @param source 待转换的实体
* @return 转换之后的字节数组
* @throws Exception
*/
public static byte[] serialize(Object source) {
ByteArrayOutputStream byteOut = null;
ObjectOutputStream ObjOut = null;
try {
byteOut = new ByteArrayOutputStream();
ObjOut = new ObjectOutputStream(byteOut);
ObjOut.writeObject(source);
ObjOut.flush();
}
catch (IOException e) {
logger.error(source.getClass().getName()
+ " serialized error !", e);
}
finally {
try {
if (null != ObjOut) {
ObjOut.close();
}
}
catch (IOException e) {
ObjOut = null;
}
}
return byteOut.toByteArray();
} /**
* 功能简述: 将字节数组反序列化为实体Bean.
* @param source 需要进行反序列化的字节数组
* @return 反序列化后的实体Bean
* @throws Exception
*/
public static Object deserialize(byte[] source) {
ObjectInputStream ObjIn = null;
Object retVal = null;
try {
ByteArrayInputStream byteIn = new ByteArrayInputStream(source);
ObjIn = new ObjectInputStream(byteIn);
retVal = ObjIn.readObject();
}
catch (Exception e) {
logger.error("deserialized error !", e);
}
finally {
try {
if(null != ObjIn) {
ObjIn.close();
}
}
catch (IOException e) {
ObjIn = null;
}
}
return retVal;
}
}
JAVA代码
3. 对JavaBean的存储和获取:
定义实体:借助于Timestamp类,获取ms值。
使用Jedis操作:key、value都需要转成byte[]字节数组。
assertEquals(expected, actual);
请参考:http://hello-nick-xu.iteye.com/blog/2077090
jedis入门一的更多相关文章
- linux系统下安装jdk,mysql,tomcat 和redis 和jedis入门案例
Day47笔记Linux+redis入门 Day47 知识讲解:Jedis 1.Linux上jdk,mysql,tomcat安装(看着文档安装) 准备工作: 因为JDK,TOMCAT,MYSQL的 ...
- jedis入门实例
在使用传统的关系数据库,我们都需要依赖一个所谓的实现了jdbc规范的驱动程序来连接数据库,这些驱动程序由各大数据库厂商提供.这些驱动就是jar包,里面就是封装了对数据库的通信协议,我们通过简单的调用就 ...
- Jedis入门
一:介绍 1.Jedis的官网 2.使用 这个可以从上面的连接进入github. https://github.com/xetorthio/jedis 3.使用方式 或者使用jar包,不过这里我使用官 ...
- 峰Redis学习(2)Jedis 入门实例
参考博客:http://blog.java1234.com/blog/articles/314.html 第一节:使用Jedis 连接Redis 新建maven项目: pom.xml: <pro ...
- jedis入门教程
1 jedis介绍 2 java连接Redis 1 导入jar包 2 连接实例 @Test //获得单一的jedis对象操作数据库 public void test1(){ //1.获得连接对象 设置 ...
- Redis学习笔记(4)—— Jedis入门
一.Jedis介绍 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php.Node.js.Go等. 在官方网站里列的一些Java客户端,有jedi ...
- Mac上的redis安装与jedis入门
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件 安装与配置 (1) https://redis.io/download下载redis stable ...
- Redis安装部署、Jedis的使用
一.NoSQL概述 为什么需要NoSQL High performance -高并发读写 Huge Storage - 海量数据的高效率存储和访问 High Scalability && ...
- 【原】实战-Java如何使用Redis
实战-Java如何使用Redis Redis的Client支持的语言非常丰富,如下: ActionScript Bash C C# C++ Clojure Common Lisp Crystal D ...
随机推荐
- 如何确定Ubuntu下是否对某个CVE打了补丁
前些日子在月赛中,拿到了一台Ubuntu14.04的服务器,但并不是root权限,需要提权.我Google了一下,找到了CVE-2015-1318,CVE-2015-1328,CVE-2015 ...
- read op case $op in
read op case $op in
- base64图片在各种浏览器的兼容性处理
IE浏览器目前最高的版本是v11,而微软放弃了IE,转向新的浏览器开发,并取名为Edge.base64图片在IE9及以后的图片均能显示没有问题,而Firefox, Chrome, Safari等非IE ...
- SQL 时间格式化函数
1 取值后格式化 {0:d}小型:如2005-5-6 {0:D}大型:如2005年5月6日 {0:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中 ...
- 详解Linq to SQL
第一部分,什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并 ...
- JavaScript input type=file 获取文件大小及类型限制
<input name="txtName" type="file" id="pic" onchange="loadImage ...
- ./configure: error: the HTTP rewrite module requires the PCRE library
docker中CentOS安装nginx出错,提示没有PCRE,需要安装pcre-devel,同时还需要安装openssl.openssl-devel yum -y install pcre-deve ...
- 【转】jQuery on()选择器函数
on()函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统 ...
- eclipse 各种版本spring插件安装
一.安装之前先得知道怎么下载吧: 一般网上找到的资料是到这里就GG了的 --> http://spring.io/tools/sts/all 很明显这点版本是不够我们需要的,假如我们的eclip ...
- Java 四种线程池的使用
java线程线程池监控 Java通过Executors提供四种线程池,分别为: newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...