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)的非关系数据库 字符串.列表.哈希表.集合.有序集合 每种数据类型 ...
随机推荐
- 8款替代Dreamweaver的开源网页开发工具
Adobe Dreamweaver虽然非常好用,但它并不是唯一一个能够设计.开发.发布精彩网站的Web开发集成环境.我们的开源世界里有很多非常棒的可以完全替代Dreamweaver的各种功能的优秀We ...
- 查看Vim的option变量的值
以t_Co变量为例,最好用 :echo &t_Co 也可以使用 :set t_Co?,但是漏打?的话就会设置,得不偿失 要想知道在哪里这个变量被设置的,用 :verbose set t_Co? ...
- 逆序对的相关问题:bzoj1831,bzoj2431
先从简单一点的bzoj2431入手: n个数1~n已经限定了,所以 对于1~i-1,新加入i,最多可以增加i-1个逆序对,最少增加0个逆序对 f[i,j]表示1~i形成的序列逆序对为j的方案数 比较容 ...
- jquery 图片无缝切换
想要和园友分享一下学习jquery的经验.总结,更希望园友提出点建议. 第一次写,有不好的地方请多多见谅! 文笔有限,很多时候不知道怎么来描述,唉.硬伤啊!!那只好多做了,贴代码... ok,废话少说 ...
- 15个极好的Linux find命令示例(二)
前阵子,我们审查了15件实事 find命令的例子(第一部分).查找命令可以做很多比只是在寻找基于名称的文件 (第2部分)在这篇文章中,让我们来讨论15高级find命令的例子, 包括-根据它访问,修改或 ...
- jQuery遍历DOM
jQuery提供了多种遍历DOM的方法.遍历方法中最大的种类是树遍历. 向上遍历DOM树 parent():返回被选元素的直接父元素 parents():返回被选元素的所有祖先元素,它一直遍历到根元素 ...
- C#中数据类型的安全转换(is,as)
原文 C#中数据类型的安全转换(is,as) 下面代码中,不能装箱,在强制类型转换时出错,因为之前 c 是 class 类型,而却要把它转换为 int 类型,这是不可以的.虽然编译器能通过编译,但是 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.2
The elementary tensors $x\otimes \cdots \otimes x$, with all factors equal, are all in the subspace ...
- 设计模式_Proxy_代理模式
形象例子: 跟MM在网上聊天,一开头总是“hi,你好”,“你从哪儿来呀?”“你多大了?”“身高多少呀?”这些话,真烦人,写个程序做为我的Proxy吧,凡是接收到这些话都设置好了自动的回答,接收到其他的 ...
- 多线程与网络之SDWebImage和NSCache
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...