通过结合jedis和spring-data-redis包开发的一个简易方便的redis 集群访问接口。对于redis-cluster方式集群安装参照:http://blog.csdn.net/cweeyii/article/details/71298905
整个工程代码:https://github.com/cweeyii/redis-parent
接口类:
public interface IRedis {
void setHashObject(K key, Map fieldValues, int expireSecond);
void setHashObject(K key, Map fieldValues);
Map getHashObject(K key, List fields);
boolean expire(K key, int expireSeconds);
void setObject(K key, HV value, int expireSecond);
void setObject(K key, HV value);
HV getObject(K key);
}
实现类:
@Component
public class RedisImpl implements IRedis {
private static final int DEFAULT_EXPIRED_TIME=7*24*3600;
@Resource
private RedisTemplate redisTemplate;
public void setHashObject(K key, Map fieldValues, int expireSecond) {
HashOperations valueOperate = redisTemplate.opsForHash();
if (!CollectionUtils.isEmpty(fieldValues)) {
valueOperate.putAll(key, fieldValues);
}
expire(key, expireSecond);
}
@Override
public void setHashObject(K key, Map fieldValues) {
setHashObject(key,fieldValues, DEFAULT_EXPIRED_TIME);
}
public Map getHashObject(K key, List fields) {
HashOperations valueOperate = redisTemplate.opsForHash();
Map map = new HashMap<>();
if (!CollectionUtils.isEmpty(fields)) {
for (HK field : fields) {
HV obj = valueOperate.get(key, field);
map.put(field, obj);
}
}
return map;
}
public boolean expire(K key, int expireSeconds) {
return redisTemplate.expire(key, expireSeconds, TimeUnit.SECONDS);
}
public void setObject(K key, HV value, int expireSecond) {
ValueOperations valueOperate = redisTemplate.opsForValue();
valueOperate.set(key, value);
redisTemplate.expire(key, expireSecond, TimeUnit.SECONDS);
}
@Override
public void setObject(K key, HV value) {
setObject(key,value,DEFAULT_EXPIRED_TIME);
}
public HV getObject(K key) {
ValueOperations valueOperate = redisTemplate.opsForValue();
return valueOperate.get(key);
}
}
spring-bean配置文件
version="1.0" encoding="UTF-8" standalone="no"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
#开启@Component和@Service注解
"com.cweeyii.**"/>
#指定配置文件的位置
id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
classpath:redis.properties
list>
property>
#redis-cluster的具体配置
#客户端链接池配置
id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.testOnReturn}"/>
#redis-cluster集群地址
id="redisCluterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
"0">
<set>
${redis.host_and_port1}
${redis.host_and_port2}
${redis.host_and_port3}
${redis.host_and_port4}
${redis.host_and_port5}
${redis.host_and_port6}
set>
#redis-client与redis-cluster的链接配置
id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
"0" ref="redisCluterConfig"/>
"1" ref="poolConfig"/>
#spring-data-redis包装,指定key,value, hkey,hvaue序列化和反序列类
id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
id="valueRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="valueRedisSerializer"/>
<property name="hashValueSerializer" ref="valueRedisSerializer"/>
redis相关配置
redis.host=192.168.31.88
redis.port=6379
redis.maxIdle=200
redis.maxTotal=1000
redis.maxWaitMillis=2000
redis.testOnBorrow=true
redis.testOnReturn=true
redis.host1=192.168.31.88
redis.port1=6379
redis.host2=192.168.31.88
redis.port2=6380
redis.host3=192.168.31.234
redis.port3=6379
redis.host4=192.168.31.234
redis.port4=6380
redis.host5=192.168.31.186
redis.port5=6379
redis.host6=192.168.31.186
redis.port6=6380
redis.host_and_port1=192.168.31.88:6379
redis.host_and_port2=192.168.31.88:6380
redis.host_and_port3=192.168.31.234:6379
redis.host_and_port4=192.168.31.234:6380
redis.host_and_port5=192.168.31.186:6379
redis.host_and_port6=192.168.31.186:6380
spring测试程序
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class BaseTest {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseTest.class);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
LOGGER.info("run setUpBeforeClass");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
LOGGER.info("run tearDownAfterClass");
}
@Test
public void doNothing() {
LOGGER.info("run doNothing");
}
}
public class RedisClientTest extends BaseTest {
@Resource
private IRedis iRedis;
@Test
public void testSetObject() {
for (int i = 0; i < 1000; i++) {
String key = "firstKey" + i;
String value = "firstValue" + i;
iRedis.setObject(key, value, DateUtils.getRandomExpireDays(21, 7) * 24 * 3600);
}
for (int i = 0; i < 10; i++) {
String key = "firstKey" + i;
String value = (String) iRedis.getObject(key);
System.out.println(value);
}
}
@Test
public void testHash() {
Map multiValueMap = new HashMap<>();
for (int i = 0; i < 1000; i++) {
String hkey = "firstKey" + i;
multiValueMap.put(hkey, i);
}
String key = "key";
iRedis.setHashObject(key, multiValueMap);
List hKeyList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
hKeyList.add("firstKey" + i);
}
Map valueMap = iRedis.getHashObject(key, hKeyList);
System.out.print(valueMap);
}
}
输出结果
setObject
firstValue0
firstValue1
firstValue2
firstValue3
firstValue4
firstValue5
firstValue6
firstValue7
firstValue8
firstValue9
setHashObject
{firstKey8=8, firstKey7=7, firstKey9=9, firstKey0=0, firstKey2=2, firstKey1=1, firstKey4=4, firstKey3=3, firstKey6=6, firstKey5=5}