使用Jedis连接集群Redis(支持Redis复杂操作)
来源:互联网 发布:视频放大的软件 编辑:程序博客网 时间:2024/06/05 03:15
Jedis-2.7.2 版本虽然支持连接集群 Redis,但是仅仅支持 String 字符串的存储,并不支持 Map、Set、List,在这里分享一个能够支持复杂操作的方法。
import java.util.Set;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisClusterCommand;import redis.clients.jedis.JedisClusterConnectionHandler;import redis.clients.jedis.JedisSlotBasedConnectionHandler;public class BinaryJedisCluster { private int maxRedirections; private JedisClusterConnectionHandler connectionHandler; public BinaryJedisCluster(Set<HostAndPort> nodes, int timeout, int maxRedirections) { this(nodes, timeout, maxRedirections, new GenericObjectPoolConfig()); } public BinaryJedisCluster(Set<HostAndPort> jedisClusterNode, int timeout, int maxRedirections, final GenericObjectPoolConfig poolConfig) { this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, timeout); this.maxRedirections = maxRedirections; } public String set(final String key, final String value) { return new JedisClusterCommand<String>(connectionHandler, maxRedirections) { @Override public String execute(Jedis connection) { return connection.set(key, value); } }.run(key); } public String get(final String key) { return new JedisClusterCommand<String>(connectionHandler, maxRedirections) { @Override public String execute(Jedis connection) { return connection.get(key); } }.run(key); }}
(注:这是简化版的,具体代码并非原创,就不贴了。)
Map、Set、List 等类似 String,在 execute 方法中通过 Jedis 直接操作 Redis 即可。
在与 Spring 结合使用时,可以通过继承 InitializingBean 来实现初始化注入。
private BinaryJedisCluster jc; @Override public void afterPropertiesSet() throws Exception { Set<HostAndPort> set = new HashSet<HostAndPort>(); set.add(new HostAndPort(host,port)); ... jc = new BinaryJedisCluster(set, 5000, 6); }
1 1
- 使用Jedis连接集群Redis(支持Redis复杂操作)
- redis--jedis连接集群
- 使用jedis测试连接操作redis
- 使用Jedis连接Redis
- 使用Jedis操作redis
- 使用Jedis操作Redis
- 使用Jedis操作redis
- 使用Jedis操作Redis
- 使用Jedis操作redis
- 使用Jedis操作redis
- 使用Jedis操作redis
- 使用Jedis操作redis
- 使用Jedis操作redis
- 使用Jedis操作Redis
- java使用jedis连接redis
- Redis-Jedis连接Redis
- Redis:使用jedis连接redis数据库过程
- jedis 集群模式下连接redis原理
- HDU 5441 Travel(最短路径变形)
- excel操作类
- POJ 3614 Sun Bathing (贪心)
- leetcode_Move Zeroes
- TCP协议疑难杂症全景解析
- 使用Jedis连接集群Redis(支持Redis复杂操作)
- 关于编译报错“dereferencing pointer to incomplete type...
- Windows传感器开发之请求用户许可
- nasm : test eflags ZF
- 在美做开发多年,写给国内iphone新手,转自http://bbs.feng.com/read-htm-tid-768284.html
- AnimatorListener 里面的几种方法分别对应动画播放的一个事件
- MacBook鼠标指针乱窜-不受控制问题的解决方法
- 详解RVO与NRVO(区别于网上常见的RVO)
- leetcode_Restore IP Addresses