Authored by DengXinFei

redis重构

package com.yoho.unions.common.redis;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by xjipeng on 16/2/11.
*/
public class YHListOperations<K,V> implements ListOperations<K, V> {
@Resource(name = "yhRedisTemplate")
private ListOperations<K, V> listOperations;
@Resource(name = "yhRedisTemplateReadOnly")
private ListOperations<K, V> listOperationsReadOnly;
@Override
public List<V> range(K key, long start, long end) {
return listOperationsReadOnly.range(key, start, end);
}
@Override
public void trim(K key, long start, long end) {
listOperations.trim(key,start,end);
}
@Override
public Long size(K key) {
return listOperationsReadOnly.size(key);
}
@Override
public Long leftPush(K key, V value) {
return listOperations.leftPush(key,value);
}
@Override
public Long leftPushAll(K key, V... values) {
return listOperations.leftPushAll(key, values);
}
/**
* Insert all {@literal values} at the head of the list stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @param values must not be {@literal empty} nor contain {@literal null} values.
* @return
* @since 1.5
*/
@Override
public Long leftPushAll(K key, Collection<V> values) {
return listOperations.leftPushAll( key, values);
}
@Override
public Long leftPushIfPresent(K key, V value) {
return listOperations.leftPushIfPresent(key, value);
}
@Override
public Long leftPush(K key, V pivot, V value) {
return listOperations.leftPush(key, pivot, value);
}
@Override
public Long rightPush(K key, V value) {
return listOperations.rightPush( key, value);
}
@Override
public Long rightPushAll(K key, V... values) {
return listOperations.rightPushAll( key, values);
}
/**
* Insert all {@literal values} at the tail of the list stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @param values must not be {@literal empty} nor contain {@literal null} values.
* @return
* @since 1.5
*/
@Override
public Long rightPushAll(K key, Collection<V> values) {
return listOperations.rightPushAll(key, values);
}
@Override
public Long rightPushIfPresent(K key, V value) {
return listOperations.rightPushIfPresent(key, value);
}
@Override
public Long rightPush(K key, V pivot, V value) {
return listOperations.rightPush( key, pivot, value);
}
@Override
public void set(K key, long index, V value) {
listOperations.set( key, index, value);
}
@Override
public Long remove(K key, long i, Object value) {
return listOperations.remove(key, i, value);
}
@Override
public V index(K key, long index) {
return listOperationsReadOnly.index(key, index);
}
@Override
public V leftPop(K key) {
return listOperations.leftPop(key);
}
@Override
public V leftPop(K key, long timeout, TimeUnit unit) {
return listOperations.leftPop(key, timeout, unit);
}
@Override
public V rightPop(K key) {
return listOperations.rightPop( key);
}
@Override
public V rightPop(K key, long timeout, TimeUnit unit) {
return listOperations.rightPop( key, timeout, unit);
}
@Override
public V rightPopAndLeftPush(K sourceKey, K destinationKey) {
return listOperations.rightPopAndLeftPush( sourceKey, destinationKey);
}
@Override
public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
return listOperations.rightPopAndLeftPush( sourceKey, destinationKey, timeout, unit);
}
@Override
public RedisOperations<K, V> getOperations() {
return null;
}
}
... ...
package com.yoho.unions.common.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnection;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;
/**
*
* 重写RedisTemplate,支持读写分离,支持long expire
*
*/
public class YHRedisTemplate<K,V> extends RedisTemplate<K, V> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* spring redis 设置超长时间expire,会调用twemproxy不支持的time命令,导致报错。
* @param key
* @param timeout
* @param unit
* @return
*/
public Boolean longExpire(K key, final long timeout, final TimeUnit unit) {
try {
final byte[] rawKey = String.valueOf(key).getBytes("UTF-8");
return (Boolean) this.execute(new RedisCallback() {
public Boolean doInRedis(RedisConnection connection) {
return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit));
}
}, true);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
}
... ...
package com.yoho.unions.common.redis;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ValueOperations;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by xjipeng on 16/2/11.
*/
public class YHValueOperations<K,V> implements ValueOperations<K, V> {
@Resource(name="yhRedisTemplate")
private ValueOperations<K, V> valueOperations;
@Resource(name="yhRedisTemplateReadOnly")
private ValueOperations<K, V> valueOperationsReadOnly;
@Override
public void set(K key, V value) {
valueOperations.set(key,value);
}
/**
* Set {@code key} to hold the string {@code value} until {@code timeout}.
*
* @param key
* @param value
* @param timeout
* @param unit
*/
@Override
public void set(K key, V value, long timeout, TimeUnit unit) {
valueOperations.set(key, value, timeout, unit);
}
@Override
public Boolean setIfAbsent(K key, V value) {
return valueOperations.setIfAbsent(key,value);
}
@Override
public void multiSet(Map<? extends K, ? extends V> m) {
valueOperations.multiSet(m);
}
@Override
public Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m) {
return valueOperations.multiSetIfAbsent(m);
}
@Override
public V get(Object key) {
return valueOperationsReadOnly.get(key);
}
@Override
public V getAndSet(K key, V value) {
return valueOperations.getAndSet(key,value);
}
@Override
public List<V> multiGet(Collection<K> keys) {
return valueOperationsReadOnly.multiGet(keys);
}
@Override
public Long increment(K key, long delta) {
return valueOperations.increment(key,delta);
}
@Override
public Double increment(K key, double delta) {
return valueOperations.increment(key, delta);
}
@Override
public Integer append(K key, String value) {
return null;
}
@Override
public String get(K key, long start, long end) {
return null;
}
@Override
public void set(K key, V value, long offset) {
valueOperations.set(key, value, offset);
}
@Override
public Long size(K key) {
return valueOperationsReadOnly.size(key);
}
/**
*
* @return
*/
@Override
public RedisOperations<K, V> getOperations() {
return null;
}
/**
* @param key
* @param offset
* @param value
* @return
* @since 1.5
*/
@Override
public Boolean setBit(K key, long offset, boolean value) {
return null;
}
/**
* @param key
* @param offset
* @return
* @since 1.5
*/
@Override
public Boolean getBit(K key, long offset) {
return null;
}
}
... ...
... ... @@ -36,10 +36,10 @@
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-redis</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>com.yoho.core</groupId>-->
<!--<artifactId>yoho-core-redis</artifactId>-->
<!--</dependency>-->
</dependencies>
<build>
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- redis template definition -->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- pool config -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal:50}" />
<property name="maxIdle" value="${redis.pool.maxIdle:20}" />
<property name="minIdle" value="${redis.pool.minIdle:20}"></property>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis:2000}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow:false}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle:false}"></property>
</bean>
<!-- 只读 -->
<bean id="yhJedisConnectionFactoryReadOnly" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:poolConfig-ref="jedisPoolConfig"
p:hostName="${redis.union.readonly.proxy.address}"
p:port="${redis.union.readonly.proxy.port}"
p:password="${redis.union.readonly.proxy.auth}"
p:database="${redis.union.readonly.proxy.database:0}" primary="true" />
<bean id="yhRedisTemplateReadOnly"
class="com.yoho.core.redis.YHRedisTemplate" primary="false"
p:connectionFactory-ref="yhJedisConnectionFactoryReadOnly"
p:keySerializer-ref="stringRedisSerializer"
p:valueSerializer-ref="stringRedisSerializer"/>
<!-- 读写 -->
<bean id="yhJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:poolConfig-ref="jedisPoolConfig"
p:hostName="${redis.union.proxy.address}"
p:port="${redis.union.proxy.port}"
p:password="${redis.union.auth}"
p:database="${redis.proxy.database:0}" primary="true" />
<bean id="yhRedisTemplate"
class="com.yoho.core.redis.YHRedisTemplate" primary="true"
p:connectionFactory-ref="yhJedisConnectionFactory"
p:keySerializer-ref="stringRedisSerializer"
p:valueSerializer-ref="stringRedisSerializer"/>
<bean id="yhValueOperations" class="com.yoho.core.redis.YHValueOperations"/>
<bean id="yhListOperations" class="com.yoho.core.redis.YHListOperations"/>
</beans>
\ No newline at end of file
... ...
... ... @@ -14,16 +14,14 @@ activeTime=${activeTime}
# ******************** redis servers ********************
redis.proxy.address=${redis.proxy.address}
redis.proxy.port=${redis.proxy.port}
redis.proxy.auth=${redis.proxy.auth}
redis.union.proxy.address=${redis.proxy.address}
redis.union.proxy.port=${redis.proxy.port}
redis.union.auth=${redis.proxy.auth}
redis.readonly.proxy.address=${redis.readonly.proxy.address}
redis.readonly.proxy.port=${redis.readonly.proxy.port}
redis.readonly.proxy.auth=${redis.readonly.proxy.auth}
redis.union.readonly.proxy.address=${redis.readonly.proxy.address}
redis.union.readonly.proxy.port=${redis.readonly.proxy.port}
redis.union.readonly.proxy.auth=${redis.readonly.proxy.auth}
#zookeeper地址
zkAddress=${zkAddress}
# web context
web.context=union
... ...