spring-data-redis操作redis集群
2016-07-08 17:26
786 查看
在这里我就不说redis到底是如何集群的了,具体可以看我的博客,集群的好处大家可以看看
http://www.jianshu.com/p/ee2aa7fe341b
pom.xml
源码地址http://download.csdn.net/detail/u011703657/9571075
http://www.jianshu.com/p/ee2aa7fe341b
pom.xml
<!-- jedis 配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="2048" /> <property name="maxIdle" value="200" /> <property name="numTestsPerEvictionRun" value="1024" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="-1" /> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <property name="maxWaitMillis" value="1500" /> <property name="testOnBorrow" value="true" /> <property name="testWhileIdle" value="true" /> <property name="testOnReturn" value="false" /> <property name="jmxEnabled" value="true" /> <property name="jmxNamePrefix" value="chbigdata" /> <property name="blockWhenExhausted" value="false" /> </bean> <!-- 单个节点 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig" /> <property name="hostName" value="192.168.0.134" /> <property name="port" value="6379"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory"> <ref bean="connectionFactory" /> </property> </bean> <!-- 集群配置 --> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg index="3"> <ref bean="jedisPoolConfig" /> </constructor-arg> <constructor-arg index="2" type="int" value="6" /> <constructor-arg index="1" type="int" value="5000" /> <constructor-arg index="0"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.0.133" /> <constructor-arg index="1" value="7000" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.0.133" /> <constructor-arg index="1" value="7002" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.0.133" /> <constructor-arg index="1" value="7003" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.0.133" /> <constructor-arg index="1" value="7004" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.0.133" /> <constructor-arg index="1" value="7005" /> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg index="0" value="192.168.0.133" /> <constructor-arg index="1" value="7006" /> </bean> </set> </constructor-arg> </bean>
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.JedisCluster; @Component public class JedisClusterUtil { @Autowired private JedisCluster jedisCluster; private static Logger logger = LoggerFactory.getLogger(JedisClusterUtil.class); /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern(final String pattern) { jedisCluster.del(pattern); logger.debug("del key >" + pattern); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { jedisCluster.del(key); logger.debug("del key >" + key); } else { logger.debug("del key >" + key + "not exist"); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return jedisCluster.exists(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { return jedisCluster.get(key); } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, String value) { boolean result = false; try { jedisCluster.set(key, value); logger.debug("set key >" + key + " value >" + value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存并设置缓存有效期 * * @param key * @param value * @param expireTime * 单位是秒 * @return */ public boolean set(final String key, String value, int expireTime) { boolean result = false; try { jedisCluster.setex(key, expireTime, value); logger.debug("set key >" + key + " value >" + value + " expireTime>" + expireTime); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }
/** * * 类描述:redis缓存类 * * @author aiqi.gong 创建时间 2016年4月22日 */ @Component public final class RedisUtil { @Autowired(required=false) private RedisTemplate<Serializable, Object> redisTemplate; /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存并设置缓存有效期 * * @param key * @param value * @param expireTime * 单位是秒 * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }
源码地址http://download.csdn.net/detail/u011703657/9571075
相关文章推荐
- redis安装问题小结
- RedHat 5.8 安装Oracle 11gR2_Grid集群
- mysql集群之MMM简单搭建
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- Redis偶发连接失败案例实战记录
- Redis中实现查找某个值的范围
- win 7 安装redis服务【笔记】
- redis的hGetAll函数的性能问题(记Redis那坑人的HGETALL)
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- Redis实现信息已读未读状态提示
- redis中修改配置文件中的端口号 密码方法
- 在Ruby on Rails上使用Redis Store的方法
- Redis和Memcache的区别总结
- 在Node.js应用中使用Redis的方法简介
- Redis服务器的启动过程分析