From 0de5015f328b4ebcae3163b3063eb4e14f49f7dc Mon Sep 17 00:00:00 2001
From: hugufei <gufei.hu@yoho.cn>
Date: Fri, 14 Oct 2016 13:58:53 +0800
Subject: [PATCH] 为search 单独添加redis

---
 service/src/main/java/com/yoho/search/common/RedisCacheService.java | 22 +++++++++++-----------
 web/src/main/resources/META-INF/spring/spring-search-redis.xml      | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 web/src/main/resources/config.properties                            |  5 +++++
 web/src/main/webapp/META-INF/autoconf/config.properties             |  5 +++++
 4 files changed, 81 insertions(+), 11 deletions(-)
 create mode 100644 web/src/main/resources/META-INF/spring/spring-search-redis.xml

diff --git a/service/src/main/java/com/yoho/search/common/RedisCacheService.java b/service/src/main/java/com/yoho/search/common/RedisCacheService.java
index 6ae56e5..b5001f4 100644
--- a/service/src/main/java/com/yoho/search/common/RedisCacheService.java
+++ b/service/src/main/java/com/yoho/search/common/RedisCacheService.java
@@ -22,10 +22,10 @@ public class RedisCacheService {
 
 	private static final Logger logger = LoggerFactory.getLogger(DownGradeCacheService.class);
 
-	@Resource(name = "yhNoSyncValueOperations")
-	private YHValueOperations<String, String> yhNoSyncValueOperations;
-	@Resource(name = "yhNoSyncRedisTemplate")
-	private YHRedisTemplate<String, String> yhNoSyncRedisTemplate;
+	@Resource(name = "searchValueOperations")
+	private YHValueOperations<String, String> searchValueOperations;
+	@Resource(name = "searchRedisTemplate")
+	private YHRedisTemplate<String, String> searchRedisTemplate;
 	
 	/**
 	 * 序列化保持null值
@@ -42,7 +42,7 @@ public class RedisCacheService {
 
 	public boolean exist(String key) {
 		try {
-			return yhNoSyncRedisTemplate.hasKey(key);
+			return searchRedisTemplate.hasKey(key);
 		} catch (Exception e) {
 			logger.error(e.getMessage(), e);
 			return false;
@@ -51,7 +51,7 @@ public class RedisCacheService {
 
 	public <T> T safeGet(String key, Class<T> clazz) {
 		try {
-			String compressedVal = yhNoSyncValueOperations.get(key);
+			String compressedVal = searchValueOperations.get(key);
 			if (StringUtils.isBlank(compressedVal)) {
 				return null;
 			}
@@ -70,8 +70,8 @@ public class RedisCacheService {
 			if (StringUtils.isBlank(compressedVal)) {
 				return;
 			}
-			yhNoSyncValueOperations.set(key, compressedVal);
-			yhNoSyncRedisTemplate.longExpire(key, expiredTimeInMinute, TimeUnit.MINUTES);
+			searchValueOperations.set(key, compressedVal);
+			searchRedisTemplate.longExpire(key, expiredTimeInMinute, TimeUnit.MINUTES);
 		} catch (Exception e) {
 			logger.error(e.getMessage(), e);
 		}
@@ -84,10 +84,10 @@ public class RedisCacheService {
 			if (StringUtils.isBlank(compressedVal)) {
 				return false;
 			}
-			if(!yhNoSyncValueOperations.setIfAbsent(key, compressedVal)){
+			if(!searchValueOperations.setIfAbsent(key, compressedVal)){
 				return false;
 			}
-			yhNoSyncRedisTemplate.longExpire(key, expiredTimeInMinute, TimeUnit.MINUTES);
+			searchRedisTemplate.longExpire(key, expiredTimeInMinute, TimeUnit.MINUTES);
 			return true;
 		} catch (Exception e) {
 			logger.error(e.getMessage(), e);
@@ -97,7 +97,7 @@ public class RedisCacheService {
 
 	public void safeDelete(String key) {
 		try {
-			yhNoSyncRedisTemplate.delete(key);
+			searchRedisTemplate.delete(key);
 		} catch (Exception e) {
 			logger.error(e.getMessage(), e);
 		}
diff --git a/web/src/main/resources/META-INF/spring/spring-search-redis.xml b/web/src/main/resources/META-INF/spring/spring-search-redis.xml
new file mode 100644
index 0000000..a6b0273
--- /dev/null
+++ b/web/src/main/resources/META-INF/spring/spring-search-redis.xml
@@ -0,0 +1,60 @@
+<?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="searchStringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
+
+	<!-- pool config -->
+	<bean id="searchJedisPoolConfig" 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="searchJedisConnectionFactory"
+		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
+		p:timeout="100" 
+		p:poolConfig-ref="searchJedisPoolConfig"
+		p:hostName="${redis.search.proxy.address}" 
+		p:port="${redis.search.proxy.port}"
+		p:password="${redis.search.proxy.auth}" primary="true" />
+
+	<!-- 读写模板 -->
+	<bean id="searchRedisTemplate" class="com.yoho.core.redis.YHRedisTemplate"
+		primary="true" 
+		p:connectionFactory-ref="searchJedisConnectionFactory"
+		p:keySerializer-ref="searchStringRedisSerializer"
+		p:valueSerializer-ref="searchStringRedisSerializer"
+		p:hashKeySerializer-ref="searchStringRedisSerializer"
+		p:hashValueSerializer-ref="searchStringRedisSerializer"
+		p:redisCacheClean-ref="redisCacheClean" />
+	
+	<!-- 只读模板 -->
+	<bean id="searchRedisTemplateReadOnly" class="com.yoho.core.redis.YHRedisTemplate"
+		primary="false" 
+		p:connectionFactory-ref="searchJedisConnectionFactory"
+		p:keySerializer-ref="searchStringRedisSerializer"
+		p:valueSerializer-ref="searchStringRedisSerializer"
+		p:hashKeySerializer-ref="searchStringRedisSerializer"
+		p:hashValueSerializer-ref="searchStringRedisSerializer"
+		p:redisCacheClean-ref="redisCacheClean" />
+	
+	<bean id="searchValueOperations" class="com.yoho.core.redis.YHValueOperations"
+		primary="true" 
+		p:hashOperations-ref="searchRedisTemplate"
+		p:hashOperationsReadOnly-ref="searchRedisTemplateReadOnly"
+		p:redisCacheClean-ref="redisCacheClean" />
+	
+	<bean id="searchHashOperations" class="com.yoho.core.redis.YHHashOperations"
+		primary="true" 
+		p:hashOperations-ref="searchRedisTemplate"
+		p:hashOperationsReadOnly-ref="searchRedisTemplateReadOnly"
+		p:redisCacheClean-ref="redisCacheClean" />
+		
+</beans>
\ No newline at end of file
diff --git a/web/src/main/resources/config.properties b/web/src/main/resources/config.properties
index 95c722b..a5177c9 100644
--- a/web/src/main/resources/config.properties
+++ b/web/src/main/resources/config.properties
@@ -15,6 +15,11 @@ bigDataRedis-search.proxy.address=test-bigdata-redis-1903805580.cn-north-1.elb.a
 bigDataRedis-search.proxy.port=6379
 bigDataRedis-search.proxy.auth=
 
+#search redis
+redis.search.proxy.address = 192.168.102.211
+redis.search.proxy.port = 6379
+redis.search.proxy.auth = 
+
 #search
 search.es.cluster.name=yohosearch_test
 search.es.servers=192.168.102.209:9300 192.168.102.216:9300
diff --git a/web/src/main/webapp/META-INF/autoconf/config.properties b/web/src/main/webapp/META-INF/autoconf/config.properties
index 702507c..1f0ce35 100644
--- a/web/src/main/webapp/META-INF/autoconf/config.properties
+++ b/web/src/main/webapp/META-INF/autoconf/config.properties
@@ -15,6 +15,11 @@ bigDataRedis-search.proxy.address=${bigDataRedis.proxy.address}
 bigDataRedis-search.proxy.port=${bigDataRedis.proxy.port}
 bigDataRedis-search.proxy.auth=${bigDataRedis.proxy.auth}
 
+#search redis
+redis.search.proxy.address = ${redis.search.proxy.address}
+redis.search.proxy.port = ${redis.search.proxy.port}
+redis.search.proxy.auth = ${redis.search.proxy.auth}
+
 #es.cluster
 search.es.cluster.name=${search.es.cluster.name}
 search.es.servers=${search.es.servers}
--
libgit2 0.24.0