在这里我就不说redis到底是如何集群的了,具体可以看我的博客,集群的好处大家可以看看
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;
}
}