Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET

等。基于这些限制,有必要考虑Redis

相关链接:

Redis实战

Redis实战之Redis + Jedis

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

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.1.0</version>
  5. <type>jar</type>
  6. <scope>compile</scope>
  7. </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

如果只是简单使用Jedis,以下这么几行代码足够:

  1. Jedis jedis = new Jedis("10.11.20.140");
  2. String keys = "name";
  3. // 删数据
  4. jedis.del(keys);
  5. // 存数据
  6. jedis.set(keys, "snowolf");
  7. // 取数据
  8. String value = jedis.get(keys);
  9. 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完成池化实现。

先做个配置文件:

  1. #最大分配的对象数
  2. redis.pool.maxActive=1024
  3. #最大能够保持idel状态的对象数
  4. redis.pool.maxIdle=200
  5. #当池内没有返回对象时,最大等待时间
  6. redis.pool.maxWait=1000
  7. #当调用borrow Object方法时,是否进行有效性检查
  8. redis.pool.testOnBorrow=true
  9. #当调用return Object方法时,是否进行有效性检查
  10. redis.pool.testOnReturn=true
  11. #IP
  12. redis.ip=10.11.20.140
  13. #Port
  14. 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

在静态代码段中完成初始化:

  1. private static JedisPool pool;
  2. static {
  3. ResourceBundle bundle = ResourceBundle.getBundle("redis");
  4. if (bundle == null) {
  5. throw new IllegalArgumentException(
  6. "[redis.properties] is not found!");
  7. }
  8. JedisPoolConfig config = new JedisPoolConfig();
  9. config.setMaxActive(Integer.valueOf(bundle
  10. .getString("redis.pool.maxActive")));
  11. config.setMaxIdle(Integer.valueOf(bundle
  12. .getString("redis.pool.maxIdle")));
  13. config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
  14. config.setTestOnBorrow(Boolean.valueOf(bundle
  15. .getString("redis.pool.testOnBorrow")));
  16. config.setTestOnReturn(Boolean.valueOf(bundle
  17. .getString("redis.pool.testOnReturn")));
  18. pool = new JedisPool(config, bundle.getString("redis.ip"),
  19. Integer.valueOf(bundle.getString("redis.port")));
  20. }
	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

  1. // 从池中获取一个Jedis对象
  2. Jedis jedis = pool.getResource();
  3. String keys = "name";
  4. // 删数据
  5. jedis.del(keys);
  6. // 存数据
  7. jedis.set(keys, "snowolf");
  8. // 取数据
  9. String value = jedis.get(keys);
  10. System.out.println(value);
  11. // 释放对象池
  12. 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实例:

  1. // 从池中获取一个Jedis对象
  2. Jedis jedis = pool.getResource();
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();

切记,最后使用后,释放Jedis对象:

  1. // 释放对象池
  2. pool.returnResource(jedis);
// 释放对象池
pool.returnResource(jedis);

三、一致性哈希

Memcached完全基于分布式集群,而RedisMaster-Slave,如果想把Reids,做成集群模式,无外乎多做几套Master-Slave,每套Master-Slave完成各自的容灾处理,通过Client工具,完成一致性哈希。

PS:Memcached是在Server端完成ShardingRedis只能依靠各个ClientSharding。可能会在Redis 3.0系列支持ServerSharding

保留前面的JedisPoolConfig,新增两个Redis的IP(redis1.ip,redis2.ip),完成两个JedisShardInfo实例,并将其丢进List中:

  1. JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
  2. bundle.getString("redis1.ip"), Integer.valueOf(bundle                       .getString("redis.port")));
  3. JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
  4. bundle.getString("redis2.ip"), Integer.valueOf(bundle                       .getString("redis.port")));
  5. List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
  6. list.add(jedisShardInfo1);
  7. 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:

  1. ShardedJedisPool pool = new ShardedJedisPool(config, list);
ShardedJedisPool pool = new ShardedJedisPool(config, list);

改由ShardedJedis,获取Jedis对象:

  1. // 从池中获取一个Jedis对象
  2. ShardedJedis jedis = pool.getResource();
  3. String keys = "name";
  4. String value = "snowolf";
  5. // 删数据
  6. jedis.del(keys);
  7. // 存数据
  8. jedis.set(keys, value);
  9. // 取数据
  10. String v = jedis.get(keys);
  11. System.out.println(v);
  12. // 释放对象池
  13. 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封装初始化:

  1. <context:property-placeholder location="classpath:redis.properties" />
  2. <bean
  3. id="jedisPoolConfig"
  4. class="redis.clients.jedis.JedisPoolConfig"
  5. >
  6. <property
  7. name="maxActive"
  8. value="${redis.pool.maxActive}" />
  9. <property
  10. name="maxIdle"
  11. value="${redis.pool.maxIdle}" />
  12. <property
  13. name="maxWait"
  14. value="${redis.pool.maxWait}" />
  15. <property
  16. name="testOnBorrow"
  17. value="${redis.pool.testOnBorrow}" />
  18. </bean>
  19. <bean
  20. id="shardedJedisPool"
  21. class="redis.clients.jedis.ShardedJedisPool"
  22. >
  23. <constructor-arg
  24. index="0"
  25. ref="jedisPoolConfig" />
  26. <constructor-arg index="1">
  27. <list>
  28. <bean class="redis.clients.jedis.JedisShardInfo">
  29. <constructor-arg
  30. index="0"
  31. value="${redis1.ip}" />
  32. <constructor-arg
  33. index="1"
  34. value="${redis.port}"
  35. type="int" />
  36. </bean>
  37. <bean class="redis.clients.jedis.JedisShardInfo">
  38. <constructor-arg
  39. index="0"
  40. value="${redis2.ip}" />
  41. <constructor-arg
  42. index="1"
  43. value="${redis.port}"
  44. type="int" />
  45. </bean>
  46. </list>
  47. </constructor-arg>
  48. </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>

代码可以更简洁一些:

  1. private ApplicationContext app;
  2. private ShardedJedisPool pool;
  3. @Before
  4. public void before() throws Exception {
  5. app = new ClassPathXmlApplicationContext("applicationContext.xml");
  6. pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
  7. }
  8. @Test
  9. public void test() {
  10. // 从池中获取一个Jedis对象
  11. ShardedJedis jedis = pool.getResource();
  12. String keys = "name";
  13. String value = "snowolf";
  14. // 删数据
  15. jedis.del(keys);
  16. // 存数据
  17. jedis.set(keys, value);
  18. // 取数据
  19. String v = jedis.get(keys);
  20. System.out.println(v);
  21. // 释放对象池
  22. pool.returnResource(jedis);
  23. assertEquals(value, v);
  24. }
	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实战之Redis + Jedis

Redis实战之征服 Redis + Jedis + Spring (一)

Redis实战之征服 Redis + Jedis + Spring (二)

Redis实战之征服 Redis + Jedis + Spring (三)

Redis实战之Redis + Jedis的更多相关文章

  1. Redis实战之Redis + Jedis[转]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报   目录(?)[-] ...

  2. 分布式缓存技术redis学习系列(五)——redis实战(redis与spring整合,分布式锁实现)

    本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...

  3. 分布式缓存技术redis系列(五)——redis实战(redis与spring整合,分布式锁实现)

    本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...

  4. Redis实战总结-Redis的高可用性

    在之前的博客<Redis实战总结-配置.持久化.复制>给出了一种Redis主从复制机制,简单地实现了Redis高可用.然后,如果Master服务器宕机,会导致整个Redis瘫痪,这种方式的 ...

  5. Redis 实战 —— 05. Redis 其他命令简介

    发布与订阅 P52 Redis 实现了发布与订阅(publish/subscribe)模式,又称 pub/sub 模式(与设计模式中的观察者模式类似).订阅者负责订阅频道,发送者负责向频道发送二进制字 ...

  6. Redis 实战 —— 14. Redis 的 Lua 脚本编程

    简介 Redis 从 2.6 版本开始引入使用 Lua 编程语言进行的服务器端脚本编程功能,这个功能可以让用户直接在 Redis 内部执行各种操作,从而达到简化代码并提高性能的作用. P248 在不编 ...

  7. .Net Redis实战——使用Redis构建Web应用

    示例介绍 示例1:借助Redis实现购物车功能 示例2:Redis实现网页缓存和数据缓存 借助Redis实现购物车功能 每个用户的购物车都是一个散列,散列存储了商品ID与商品订购数量之间的映射.订购商 ...

  8. Redis实战之Redis命令

    阅读目录 1. 字符串命令 2. 列表命令 3. 集合命令 4. 散列命令 5. 有序集合命令 6. 发布与订阅命令 7. 小试牛刀 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结 ...

  9. Redis 实战 —— 01. Redis 数据结构简介

    一些数据库和缓存服务器的特性和功能 P4 名称 类型 数据存储选项 查询类型 附加功能 Redis 使用内存存储(in-memory)的非关系数据库 字符串.列表.哈希表.集合.有序集合 每种数据类型 ...

随机推荐

  1. C#中this在构造函数时的使用

    今天编程的时候,想要用this来处理构造函数,想了半天没有想起来 后来找了自己以前记录的 http://www.cnblogs.com/chucklu/p/4842766.html public Cu ...

  2. bzoj3205

    和bzoj2595类似,也是斯坦纳树 设f[l,r,]表示在点i机器人组合成了l-r最少推的次数,然后可得 f[l,r,i]=min(f[l,m,i]+f[m+1,r,i]) f[l,r,i]=min ...

  3. poj2182

    首先容易知道,最后一个数是最容易确定的,于是从后往前确定 对于位置j,它的数就是1~n中剩余数的第a[j]+1小的数 这当然可以用平衡数做,复杂度为O(nlogn) 有没有更简洁得算法?树状数组+二分 ...

  4. Gentoo安装

    Gentoo Linux安装详解--根据官方WiKi整理 时间:2014-06-26 06:37:54      阅读:549      评论:0      收藏:0      [点我收藏+] 标签: ...

  5. 判断String为空

    1.  et_string.equals("")|| et_string==null 2.  ""用equal.null用== TextUtils.isEmpt ...

  6. Centos 下Nginx 自启动脚本

    #!/bin/bash #ckconfig: NGINX_PATH=/web/container/nginx- NGINX_COMMAND=$NGINX_PATH/sbin/nginx NGINX_P ...

  7. Java [leetcode 21]Merge Two Sorted Lists

    题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

  8. Ruby准备工作

    解释性语言,自上而下执行,纯面向对象,跨平台,动态绑定,没有多重继承.NetBeans sun公司开发irb指令可快速实时输入并返回结果 quit 或者exitrdoc hello.rb 生成html ...

  9. ExecuteStoreQuery

    using (var webdb = new kyj_NewHouseDBEntities()) { string sql = "select * from developer where ...

  10. Tomcat配置JNDI数据源

    经过3个多小时的努力,配置JNDI数据源(主要是通过DBCP连接池)终于搞定-还是Tomcat官方的说明好,不过全是英文的,大概还看得懂.百度上那么花花绿绿的太多了,一个也没成功!...本例使用的数据 ...