一.下载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

最新文章

  1. 计划安装SQL Server2012需求详细
  2. Linux之服务器时间同步
  3. Pycharm 2016 2 激活
  4. crontab 定时调度
  5. target不起作用了
  6. git在本地创建远程仓库
  7. 一个平时写程序通用的Makefile样例
  8. hdu 4857 逃生
  9. Project Euler 107:Minimal network 最小网络
  10. iOS 8安装教程图解
  11. linux驱动调试--段错误之oops信息分析
  12. TortoiseSVN历史版本安装及svn服务器搭建
  13. ubuntu jdk环境变量配置
  14. 如何用C#寻找100到999的所有水仙花数?
  15. MySQL如何有效的存储IP地址及字符串IP和数值之间如何转换
  16. HTML学习笔记5:修饰符和特殊标签
  17. LuoGu P2420 让我们异或吧
  18. 无法启动MYSQL服务”1067 进程意外终止”解决的方法
  19. linux-env命令解析
  20. version `GLIBC_2.14&#39; not found问题

热门文章

  1. 图片占位 css
  2. ViewState存储到服务器
  3. android通讯录导航栏源码(一)
  4. Genymotion error:The virtual device got no IP address
  5. 响应式web之媒体查询(一)
  6. 关于fsockopen pfsockopen函数被禁用的解决方法
  7. Jquery实现图片切换效果(IE,FF,Goole)都可以正常运行
  8. ps切片
  9. javascript 数据结构和算法读书笔记 &gt; 第二章 数组
  10. Python之路第三天,基础(3)-set,函数,内置函数,文件,三元运算,lambda