在上一篇文章中,简单介绍了Jedis的连接池使用方式。
如果和Spring进行整合的话,我们将获得更好的简洁性、灵活性,显然是一种更加优雅(graceful)的方式。
[一]. 搭建环境:
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>
[二]. 从SPring容器中获取JedisPool:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); JedisPool pool = (JedisPool) context.getBean("jedisPool"); Jedis jedis = pool.getResource(); ... pool.returnResource(jedis);
[三]. 缓存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; } }
3. 对JavaBean的存储和获取:
定义实体:借助于Timestamp类,获取ms值。
Userinfo actual = new Userinfo(140520, "Nick Xu", new Date(Timestamp.valueOf("1990-11-11 00:00:00").getTime()));
使用Jedis操作:key、value都需要转成byte[]字节数组。
String key = "user.userid." + actual.getUserId(); jedis.set(key.getBytes(), SerializingUtil.serialize(actual)); Userinfo expected = (Userinfo) SerializingUtil.deserialize(jedis.get(key.getBytes()));
对象的比较:需要覆写equals和hashCode方法。
assertEquals(expected, actual);
相关推荐
spring4使用jedis整合redis,教程见博客地址:https://blog.csdn.net/zhou870498/article/details/79904475
Spring mvc整合redis实例(redis连接池) 将所有jedis 增删改进行事务的封装、查询单独做为有返回参数的封装。简化了很多代码
Redis Windows32位+64位安装包,Linux压缩包, Redis图形操作管理软件,Jedis
redis3.0 widows集群 spring整合jedis redis搭建window集群代码和文档rubygems-2.5.1和rubyinstaller-2.2.3-x64软件下载
spring-data-redis-2.1.5.RELEASE和jedis2.9.0,用于java使用redis开发。
博客文章示例代码:spring boot 自学笔记(四) Redis集成—Jedis
redis3.2.1+jedis2.8.0+jedis.api.rar一站下载
关于Redis 3.0.7 的集群部署 及 jedis2.8 与 spring3.2 整合的测试笔记 配套小程序
Spring Data Redis 纯英文文档,介绍spring操作redis的一些用法
spring_redis集成,通过jedis作为redis的客户端。 只提供集成方式,具体api,参照jedis API
里面包含了spring-data-redis-1.3.4.RELEASE.jar ,但是jedis可以不用,用spring-data-redis方式的同学可以使用
项目由maven构建,使用springMVC整合了Redis的集群,发布到tomcat中,访问http://localhost:8080/SpringRedisCluster/redis/hello.do测试即可,前提是配好了redis的集群。
SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用
redis整合spring
redis的Java客户端开发包Jedis,有两个jar包 commons-pool2-2.6.0.jar jedis-2.9.0.jar
Redis支持很多编程语言的客户端,有C、C#、C++、Clojure、Common Lisp、Erlang、Go、Lua、...Redis官方列出的Java客户端也有少,有Jedis、JRedis、JDBC-Redis、RJC等,当然,Jedis是Redis官方首选的Java客户端开发包。
redis2.8windows64位版本+jedis2.5jar+jedis英文api
Redis简介 Redis安装与部署 Redis数据类型及常用操作 Jedis的介绍与应用
Redis 入门指南.pdf
spring-data集成jedis测试代码,spring配置文件及jar包。