Redis实战之Redis + Jedis
用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET
等。基于这些限制,有必要考虑Redis!
相关链接:
Redis实战之征服 Redis + Jedis + Spring (一)
Redis实战之征服 Redis + Jedis + Spring (二)
Redis实战之征服 Redis + Jedis + Spring (三)
言归正传,目前Redis大概有3中基于Java语言的Client:
- Jredis
- Jedis
- Redis4J
这里只说Jedis,因为它是官方提供的唯一Redis Client For Java Provider!
一、简单使用Jedis
需要Jedis就从Maven获取吧!
Maven Pom.xml
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
如果只是简单使用Jedis,以下这么几行代码足够:
- Jedis jedis = new Jedis("10.11.20.140");
- String keys = "name";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
- System.out.println(value);
Jedis jedis = new Jedis("10.11.20.140");
String keys = "name"; // 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys); System.out.println(value);
二、池化使用Jedis
Jedis使用commons-pool完成池化实现。
先做个配置文件:
- #最大分配的对象数
- redis.pool.maxActive=1024
- #最大能够保持idel状态的对象数
- redis.pool.maxIdle=200
- #当池内没有返回对象时,最大等待时间
- redis.pool.maxWait=1000
- #当调用borrow Object方法时,是否进行有效性检查
- redis.pool.testOnBorrow=true
- #当调用return Object方法时,是否进行有效性检查
- redis.pool.testOnReturn=true
- #IP
- redis.ip=10.11.20.140
- #Port
- redis.port=6379
#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=10.11.20.140
#Port
redis.port=6379
在静态代码段中完成初始化:
- private static JedisPool pool;
- static {
- ResourceBundle bundle = ResourceBundle.getBundle("redis");
- if (bundle == null) {
- throw new IllegalArgumentException(
- "[redis.properties] is not found!");
- }
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(Integer.valueOf(bundle
- .getString("redis.pool.maxActive")));
- config.setMaxIdle(Integer.valueOf(bundle
- .getString("redis.pool.maxIdle")));
- config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
- config.setTestOnBorrow(Boolean.valueOf(bundle
- .getString("redis.pool.testOnBorrow")));
- config.setTestOnReturn(Boolean.valueOf(bundle
- .getString("redis.pool.testOnReturn")));
- pool = new JedisPool(config, bundle.getString("redis.ip"),
- Integer.valueOf(bundle.getString("redis.port")));
- }
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")));
}
然后,修改前面那段jedis操作Redis
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
- String keys = "name";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
- System.out.println(value);
- // 释放对象池
- pool.returnResource(jedis);
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
String keys = "name"; // 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, "snowolf");
// 取数据
String value = jedis.get(keys); System.out.println(value); // 释放对象池
pool.returnResource(jedis);
改为从对象池中,获取Jedis实例:
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
切记,最后使用后,释放Jedis对象:
- // 释放对象池
- pool.returnResource(jedis);
// 释放对象池
pool.returnResource(jedis);
三、一致性哈希
Memcached完全基于分布式集群,而Redis是Master-Slave,如果想把Reids,做成集群模式,无外乎多做几套Master-Slave,每套Master-Slave完成各自的容灾处理,通过Client工具,完成一致性哈希。
PS:Memcached是在Server端完成Sharding,Redis只能依靠各个Client做Sharding。可能会在Redis 3.0系列支持Server端Sharding。
保留前面的JedisPoolConfig,新增两个Redis的IP(redis1.ip,redis2.ip),完成两个JedisShardInfo实例,并将其丢进List中:
- JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
- bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
- JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
- bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
- List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
- list.add(jedisShardInfo1);
- list.add(jedisShardInfo2);
JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port"))); List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
list.add(jedisShardInfo1);
list.add(jedisShardInfo2);
初始化ShardedJedisPool代替JedisPool:
- ShardedJedisPool pool = new ShardedJedisPool(config, list);
ShardedJedisPool pool = new ShardedJedisPool(config, list);
改由ShardedJedis,获取Jedis对象:
- // 从池中获取一个Jedis对象
- ShardedJedis jedis = pool.getResource();
- String keys = "name";
- String value = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
- System.out.println(v);
- // 释放对象池
- pool.returnResource(jedis);
// 从池中获取一个Jedis对象
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "snowolf";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, value);
// 取数据
String v = jedis.get(keys); System.out.println(v); // 释放对象池
pool.returnResource(jedis);
四、Spring封装参考
Ok,完成上述代码足够完成简单任务,如果有必要,可以用Spring封装初始化:
- <context:property-placeholder location="classpath:redis.properties" />
- <bean
- id="jedisPoolConfig"
- class="redis.clients.jedis.JedisPoolConfig"
- >
- <property
- name="maxActive"
- value="${redis.pool.maxActive}" />
- <property
- name="maxIdle"
- value="${redis.pool.maxIdle}" />
- <property
- name="maxWait"
- value="${redis.pool.maxWait}" />
- <property
- name="testOnBorrow"
- value="${redis.pool.testOnBorrow}" />
- </bean>
- <bean
- id="shardedJedisPool"
- class="redis.clients.jedis.ShardedJedisPool"
- >
- <constructor-arg
- index="0"
- ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg
- index="0"
- value="${redis1.ip}" />
- <constructor-arg
- index="1"
- value="${redis.port}"
- type="int" />
- </bean>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg
- index="0"
- value="${redis2.ip}" />
- <constructor-arg
- index="1"
- value="${redis.port}"
- type="int" />
- </bean>
- </list>
- </constructor-arg>
- </bean>
<context:property-placeholder location="classpath:redis.properties" />
<bean
id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig"
>
<property
name="maxActive"
value="${redis.pool.maxActive}" />
<property
name="maxIdle"
value="${redis.pool.maxIdle}" />
<property
name="maxWait"
value="${redis.pool.maxWait}" />
<property
name="testOnBorrow"
value="${redis.pool.testOnBorrow}" />
</bean>
<bean
id="shardedJedisPool"
class="redis.clients.jedis.ShardedJedisPool"
>
<constructor-arg
index="0"
ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg
index="0"
value="${redis1.ip}" />
<constructor-arg
index="1"
value="${redis.port}"
type="int" />
</bean>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg
index="0"
value="${redis2.ip}" />
<constructor-arg
index="1"
value="${redis.port}"
type="int" />
</bean>
</list>
</constructor-arg>
</bean>
代码可以更简洁一些:
- private ApplicationContext app;
- private ShardedJedisPool pool;
- @Before
- public void before() throws Exception {
- app = new ClassPathXmlApplicationContext("applicationContext.xml");
- pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
- }
- @Test
- public void test() {
- // 从池中获取一个Jedis对象
- ShardedJedis jedis = pool.getResource();
- String keys = "name";
- String value = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
- System.out.println(v);
- // 释放对象池
- pool.returnResource(jedis);
- assertEquals(value, v);
- }
private ApplicationContext app;
private ShardedJedisPool pool; @Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("applicationContext.xml");
pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
} @Test
public void test() { // 从池中获取一个Jedis对象
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "snowolf";
// 删数据
jedis.del(keys);
// 存数据
jedis.set(keys, value);
// 取数据
String v = jedis.get(keys); System.out.println(v); // 释放对象池
pool.returnResource(jedis); assertEquals(value, v);
}
当然,Spring提供了对于Redis的专门支持:spring-data-redis,以后有机会再深入研究。
相关链接:
Redis实战之征服 Redis + Jedis + Spring (一)
Redis实战之征服 Redis + Jedis + Spring (二)
Redis实战之征服 Redis + Jedis + Spring (三)
Redis实战之Redis + Jedis的更多相关文章
- Redis实战之Redis + Jedis[转]
http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报 目录(?)[-] ...
- 分布式缓存技术redis学习系列(五)——redis实战(redis与spring整合,分布式锁实现)
本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...
- 分布式缓存技术redis系列(五)——redis实战(redis与spring整合,分布式锁实现)
本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...
- Redis实战总结-Redis的高可用性
在之前的博客<Redis实战总结-配置.持久化.复制>给出了一种Redis主从复制机制,简单地实现了Redis高可用.然后,如果Master服务器宕机,会导致整个Redis瘫痪,这种方式的 ...
- Redis 实战 —— 05. Redis 其他命令简介
发布与订阅 P52 Redis 实现了发布与订阅(publish/subscribe)模式,又称 pub/sub 模式(与设计模式中的观察者模式类似).订阅者负责订阅频道,发送者负责向频道发送二进制字 ...
- Redis 实战 —— 14. Redis 的 Lua 脚本编程
简介 Redis 从 2.6 版本开始引入使用 Lua 编程语言进行的服务器端脚本编程功能,这个功能可以让用户直接在 Redis 内部执行各种操作,从而达到简化代码并提高性能的作用. P248 在不编 ...
- .Net Redis实战——使用Redis构建Web应用
示例介绍 示例1:借助Redis实现购物车功能 示例2:Redis实现网页缓存和数据缓存 借助Redis实现购物车功能 每个用户的购物车都是一个散列,散列存储了商品ID与商品订购数量之间的映射.订购商 ...
- Redis实战之Redis命令
阅读目录 1. 字符串命令 2. 列表命令 3. 集合命令 4. 散列命令 5. 有序集合命令 6. 发布与订阅命令 7. 小试牛刀 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结 ...
- Redis 实战 —— 01. Redis 数据结构简介
一些数据库和缓存服务器的特性和功能 P4 名称 类型 数据存储选项 查询类型 附加功能 Redis 使用内存存储(in-memory)的非关系数据库 字符串.列表.哈希表.集合.有序集合 每种数据类型 ...
随机推荐
- Python学习笔记一--字符串的使用
一.基本操作 1. 合并字符串:“+” 2. 打印重复的字符串:"*" 3. 按位获取字符串中的字符:索引 4. 按位获取字符串中的子字符串:分片 5 ...
- STM32硬件复位时间
两个参数,,1低电平时间 2低电平压值 1.stm32复位时间 ------ 低电平时间:1.5 至 4.5 ms 2.压值
- sizeof(结构体)的计算
摘要: 经常被计算结构体的sizeof给搞晕,于是找了个时间,静下心来,搞定它. 一.为什么结构体计算这么乱? 答案是字节对齐,计算机存储系统中以Byte为单位存储数据,不同数据类型所占的空间不同,如 ...
- POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)
package excelPoiTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...
- mac osx 升级yosemite后java出错的解决
原文 http://www.cnblogs.com/walkerwang/p/4034152.html
- POJ 2398 Toy Storage
这道题和POJ 2318几乎是一样的. 区别就是输入中坐标不给排序了,=_=|| 输出变成了,有多少个区域中有t个点. #include <cstdio> #include <cma ...
- web前端优化-温故知新系列(1)
有关web前端优化的博文,博客园中有许多网友的博客中都有介绍,而且详细.精准.楼主打算写这个博客,算是对自己一年工作来的一个总结和积累有些知识从别的地方拷贝过来的,但是都审查过. 引言: 1. 慢的页 ...
- unix network programming(3rd)Vol.1 [第2~5章]《读书笔记系列》
13~22章 重要 第2章 传输层: TCP/ UDP / STCP (Stream Control Transmission Protocol) TCP 可靠,有重传机制,SYN队列号 UDP 不可 ...
- Myeclipse最简单的svn插件安装方法
首先来这儿下载插件 http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 找个最新的下载 解压到对应的位置, ...
- Asp.Net 高性能框架 SqlSugar.ORM 2.3
一.前言 SqlSugar从去年到现在已经一年了,版本从1.0升到了现在的2.3 ,这是一个稳定版本 ,有数家公司已经项目上线,在这里我将SqlSugar的功能重新整理成一篇新的贴子,希望大家喜欢. ...