Authored by DengXinFei

redis重构

  1 +package com.yoho.unions.common.redis;
  2 +
  3 +import org.springframework.data.redis.core.ListOperations;
  4 +import org.springframework.data.redis.core.RedisOperations;
  5 +
  6 +import javax.annotation.Resource;
  7 +import java.util.Collection;
  8 +import java.util.List;
  9 +import java.util.concurrent.TimeUnit;
  10 +
  11 +/**
  12 + * Created by xjipeng on 16/2/11.
  13 + */
  14 +public class YHListOperations<K,V> implements ListOperations<K, V> {
  15 +
  16 + @Resource(name = "yhRedisTemplate")
  17 + private ListOperations<K, V> listOperations;
  18 +
  19 + @Resource(name = "yhRedisTemplateReadOnly")
  20 + private ListOperations<K, V> listOperationsReadOnly;
  21 +
  22 + @Override
  23 + public List<V> range(K key, long start, long end) {
  24 + return listOperationsReadOnly.range(key, start, end);
  25 + }
  26 +
  27 + @Override
  28 + public void trim(K key, long start, long end) {
  29 + listOperations.trim(key,start,end);
  30 + }
  31 +
  32 + @Override
  33 + public Long size(K key) {
  34 + return listOperationsReadOnly.size(key);
  35 + }
  36 +
  37 + @Override
  38 + public Long leftPush(K key, V value) {
  39 + return listOperations.leftPush(key,value);
  40 + }
  41 +
  42 + @Override
  43 + public Long leftPushAll(K key, V... values) {
  44 + return listOperations.leftPushAll(key, values);
  45 + }
  46 +
  47 + /**
  48 + * Insert all {@literal values} at the head of the list stored at {@literal key}.
  49 + *
  50 + * @param key must not be {@literal null}.
  51 + * @param values must not be {@literal empty} nor contain {@literal null} values.
  52 + * @return
  53 + * @since 1.5
  54 + */
  55 + @Override
  56 + public Long leftPushAll(K key, Collection<V> values) {
  57 + return listOperations.leftPushAll( key, values);
  58 + }
  59 +
  60 + @Override
  61 + public Long leftPushIfPresent(K key, V value) {
  62 + return listOperations.leftPushIfPresent(key, value);
  63 + }
  64 +
  65 + @Override
  66 + public Long leftPush(K key, V pivot, V value) {
  67 + return listOperations.leftPush(key, pivot, value);
  68 + }
  69 +
  70 + @Override
  71 + public Long rightPush(K key, V value) {
  72 + return listOperations.rightPush( key, value);
  73 + }
  74 +
  75 + @Override
  76 + public Long rightPushAll(K key, V... values) {
  77 + return listOperations.rightPushAll( key, values);
  78 + }
  79 +
  80 + /**
  81 + * Insert all {@literal values} at the tail of the list stored at {@literal key}.
  82 + *
  83 + * @param key must not be {@literal null}.
  84 + * @param values must not be {@literal empty} nor contain {@literal null} values.
  85 + * @return
  86 + * @since 1.5
  87 + */
  88 + @Override
  89 + public Long rightPushAll(K key, Collection<V> values) {
  90 + return listOperations.rightPushAll(key, values);
  91 + }
  92 +
  93 + @Override
  94 + public Long rightPushIfPresent(K key, V value) {
  95 + return listOperations.rightPushIfPresent(key, value);
  96 + }
  97 +
  98 + @Override
  99 + public Long rightPush(K key, V pivot, V value) {
  100 + return listOperations.rightPush( key, pivot, value);
  101 + }
  102 +
  103 + @Override
  104 + public void set(K key, long index, V value) {
  105 + listOperations.set( key, index, value);
  106 + }
  107 +
  108 + @Override
  109 + public Long remove(K key, long i, Object value) {
  110 + return listOperations.remove(key, i, value);
  111 + }
  112 +
  113 + @Override
  114 + public V index(K key, long index) {
  115 + return listOperationsReadOnly.index(key, index);
  116 + }
  117 +
  118 + @Override
  119 + public V leftPop(K key) {
  120 + return listOperations.leftPop(key);
  121 + }
  122 +
  123 + @Override
  124 + public V leftPop(K key, long timeout, TimeUnit unit) {
  125 + return listOperations.leftPop(key, timeout, unit);
  126 + }
  127 +
  128 + @Override
  129 + public V rightPop(K key) {
  130 + return listOperations.rightPop( key);
  131 + }
  132 +
  133 + @Override
  134 + public V rightPop(K key, long timeout, TimeUnit unit) {
  135 + return listOperations.rightPop( key, timeout, unit);
  136 + }
  137 +
  138 + @Override
  139 + public V rightPopAndLeftPush(K sourceKey, K destinationKey) {
  140 + return listOperations.rightPopAndLeftPush( sourceKey, destinationKey);
  141 + }
  142 +
  143 + @Override
  144 + public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
  145 + return listOperations.rightPopAndLeftPush( sourceKey, destinationKey, timeout, unit);
  146 + }
  147 +
  148 + @Override
  149 + public RedisOperations<K, V> getOperations() {
  150 + return null;
  151 + }
  152 +}
  1 +package com.yoho.unions.common.redis;
  2 +
  3 +import org.slf4j.Logger;
  4 +import org.slf4j.LoggerFactory;
  5 +import org.springframework.data.redis.connection.RedisConnection;
  6 +
  7 +import java.io.UnsupportedEncodingException;
  8 +import java.util.concurrent.TimeUnit;
  9 +
  10 +/**
  11 + *
  12 + * 重写RedisTemplate,支持读写分离,支持long expire
  13 + *
  14 + */
  15 +public class YHRedisTemplate<K,V> extends RedisTemplate<K, V> {
  16 +
  17 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  18 +
  19 + /**
  20 + * spring redis 设置超长时间expire,会调用twemproxy不支持的time命令,导致报错。
  21 + * @param key
  22 + * @param timeout
  23 + * @param unit
  24 + * @return
  25 + */
  26 + public Boolean longExpire(K key, final long timeout, final TimeUnit unit) {
  27 +
  28 + try {
  29 + final byte[] rawKey = String.valueOf(key).getBytes("UTF-8");
  30 +
  31 + return (Boolean) this.execute(new RedisCallback() {
  32 + public Boolean doInRedis(RedisConnection connection) {
  33 + return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit));
  34 + }
  35 + }, true);
  36 + } catch (UnsupportedEncodingException e) {
  37 + e.printStackTrace();
  38 + }
  39 +
  40 + return false;
  41 + }
  42 +
  43 +
  44 +}
  45 +
  46 +
  1 +package com.yoho.unions.common.redis;
  2 +
  3 +import org.springframework.data.redis.core.RedisOperations;
  4 +import org.springframework.data.redis.core.ValueOperations;
  5 +
  6 +import javax.annotation.Resource;
  7 +import java.util.Collection;
  8 +import java.util.List;
  9 +import java.util.Map;
  10 +import java.util.concurrent.TimeUnit;
  11 +
  12 +/**
  13 + * Created by xjipeng on 16/2/11.
  14 + */
  15 +
  16 +public class YHValueOperations<K,V> implements ValueOperations<K, V> {
  17 +
  18 + @Resource(name="yhRedisTemplate")
  19 + private ValueOperations<K, V> valueOperations;
  20 +
  21 + @Resource(name="yhRedisTemplateReadOnly")
  22 + private ValueOperations<K, V> valueOperationsReadOnly;
  23 +
  24 + @Override
  25 + public void set(K key, V value) {
  26 + valueOperations.set(key,value);
  27 + }
  28 +
  29 + /**
  30 + * Set {@code key} to hold the string {@code value} until {@code timeout}.
  31 + *
  32 + * @param key
  33 + * @param value
  34 + * @param timeout
  35 + * @param unit
  36 + */
  37 + @Override
  38 + public void set(K key, V value, long timeout, TimeUnit unit) {
  39 + valueOperations.set(key, value, timeout, unit);
  40 + }
  41 +
  42 + @Override
  43 + public Boolean setIfAbsent(K key, V value) {
  44 + return valueOperations.setIfAbsent(key,value);
  45 + }
  46 +
  47 + @Override
  48 + public void multiSet(Map<? extends K, ? extends V> m) {
  49 + valueOperations.multiSet(m);
  50 + }
  51 +
  52 + @Override
  53 + public Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m) {
  54 + return valueOperations.multiSetIfAbsent(m);
  55 + }
  56 +
  57 + @Override
  58 + public V get(Object key) {
  59 + return valueOperationsReadOnly.get(key);
  60 + }
  61 +
  62 + @Override
  63 + public V getAndSet(K key, V value) {
  64 + return valueOperations.getAndSet(key,value);
  65 + }
  66 +
  67 + @Override
  68 + public List<V> multiGet(Collection<K> keys) {
  69 + return valueOperationsReadOnly.multiGet(keys);
  70 + }
  71 +
  72 + @Override
  73 + public Long increment(K key, long delta) {
  74 + return valueOperations.increment(key,delta);
  75 + }
  76 +
  77 + @Override
  78 + public Double increment(K key, double delta) {
  79 + return valueOperations.increment(key, delta);
  80 + }
  81 +
  82 + @Override
  83 + public Integer append(K key, String value) {
  84 + return null;
  85 + }
  86 +
  87 + @Override
  88 + public String get(K key, long start, long end) {
  89 + return null;
  90 + }
  91 +
  92 + @Override
  93 + public void set(K key, V value, long offset) {
  94 + valueOperations.set(key, value, offset);
  95 + }
  96 +
  97 + @Override
  98 + public Long size(K key) {
  99 + return valueOperationsReadOnly.size(key);
  100 + }
  101 +
  102 + /**
  103 + *
  104 + * @return
  105 + */
  106 + @Override
  107 + public RedisOperations<K, V> getOperations() {
  108 + return null;
  109 + }
  110 +
  111 + /**
  112 + * @param key
  113 + * @param offset
  114 + * @param value
  115 + * @return
  116 + * @since 1.5
  117 + */
  118 + @Override
  119 + public Boolean setBit(K key, long offset, boolean value) {
  120 + return null;
  121 + }
  122 +
  123 + /**
  124 + * @param key
  125 + * @param offset
  126 + * @return
  127 + * @since 1.5
  128 + */
  129 + @Override
  130 + public Boolean getBit(K key, long offset) {
  131 + return null;
  132 + }
  133 +}
@@ -36,10 +36,10 @@ @@ -36,10 +36,10 @@
36 <groupId>org.aspectj</groupId> 36 <groupId>org.aspectj</groupId>
37 <artifactId>aspectjweaver</artifactId> 37 <artifactId>aspectjweaver</artifactId>
38 </dependency> 38 </dependency>
39 - <dependency>  
40 - <groupId>com.yoho.core</groupId>  
41 - <artifactId>yoho-core-redis</artifactId>  
42 - </dependency> 39 + <!--<dependency>-->
  40 + <!--<groupId>com.yoho.core</groupId>-->
  41 + <!--<artifactId>yoho-core-redis</artifactId>-->
  42 + <!--</dependency>-->
43 </dependencies> 43 </dependencies>
44 44
45 <build> 45 <build>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3 + xmlns:p="http://www.springframework.org/schema/p"
  4 + xmlns="http://www.springframework.org/schema/beans"
  5 + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6 +
  7 + <!-- redis template definition -->
  8 + <bean id="stringRedisSerializer"
  9 + class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  10 +
  11 + <!-- pool config -->
  12 + <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  13 + <property name="maxTotal" value="${redis.pool.maxTotal:50}" />
  14 + <property name="maxIdle" value="${redis.pool.maxIdle:20}" />
  15 + <property name="minIdle" value="${redis.pool.minIdle:20}"></property>
  16 + <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis:2000}" />
  17 + <property name="testOnBorrow" value="${redis.pool.testOnBorrow:false}" />
  18 + <property name="testWhileIdle" value="${redis.pool.testWhileIdle:false}"></property>
  19 + </bean>
  20 +
  21 +
  22 + <!-- 只读 -->
  23 + <bean id="yhJedisConnectionFactoryReadOnly" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  24 + p:poolConfig-ref="jedisPoolConfig"
  25 + p:hostName="${redis.union.readonly.proxy.address}"
  26 + p:port="${redis.union.readonly.proxy.port}"
  27 + p:password="${redis.union.readonly.proxy.auth}"
  28 + p:database="${redis.union.readonly.proxy.database:0}" primary="true" />
  29 +
  30 + <bean id="yhRedisTemplateReadOnly"
  31 + class="com.yoho.core.redis.YHRedisTemplate" primary="false"
  32 + p:connectionFactory-ref="yhJedisConnectionFactoryReadOnly"
  33 + p:keySerializer-ref="stringRedisSerializer"
  34 + p:valueSerializer-ref="stringRedisSerializer"/>
  35 +
  36 +
  37 + <!-- 读写 -->
  38 + <bean id="yhJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  39 + p:poolConfig-ref="jedisPoolConfig"
  40 + p:hostName="${redis.union.proxy.address}"
  41 + p:port="${redis.union.proxy.port}"
  42 + p:password="${redis.union.auth}"
  43 + p:database="${redis.proxy.database:0}" primary="true" />
  44 +
  45 + <bean id="yhRedisTemplate"
  46 + class="com.yoho.core.redis.YHRedisTemplate" primary="true"
  47 + p:connectionFactory-ref="yhJedisConnectionFactory"
  48 + p:keySerializer-ref="stringRedisSerializer"
  49 + p:valueSerializer-ref="stringRedisSerializer"/>
  50 +
  51 + <bean id="yhValueOperations" class="com.yoho.core.redis.YHValueOperations"/>
  52 + <bean id="yhListOperations" class="com.yoho.core.redis.YHListOperations"/>
  53 +
  54 +</beans>
@@ -14,16 +14,14 @@ activeTime=${activeTime} @@ -14,16 +14,14 @@ activeTime=${activeTime}
14 14
15 15
16 # ******************** redis servers ******************** 16 # ******************** redis servers ********************
17 -redis.proxy.address=${redis.proxy.address}  
18 -redis.proxy.port=${redis.proxy.port}  
19 -redis.proxy.auth=${redis.proxy.auth} 17 +redis.union.proxy.address=${redis.proxy.address}
  18 +redis.union.proxy.port=${redis.proxy.port}
  19 +redis.union.auth=${redis.proxy.auth}
20 20
21 -redis.readonly.proxy.address=${redis.readonly.proxy.address}  
22 -redis.readonly.proxy.port=${redis.readonly.proxy.port}  
23 -redis.readonly.proxy.auth=${redis.readonly.proxy.auth} 21 +redis.union.readonly.proxy.address=${redis.readonly.proxy.address}
  22 +redis.union.readonly.proxy.port=${redis.readonly.proxy.port}
  23 +redis.union.readonly.proxy.auth=${redis.readonly.proxy.auth}
24 24
25 -#zookeeper地址  
26 -zkAddress=${zkAddress}  
27 # web context 25 # web context
28 web.context=union 26 web.context=union
29 27