Showing
20 changed files
with
277 additions
and
149 deletions
@@ -3,14 +3,25 @@ package com.yoho.search.common.cache.impls; | @@ -3,14 +3,25 @@ package com.yoho.search.common.cache.impls; | ||
3 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | 3 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; |
4 | import com.yoho.search.common.cache.model.CacheObject; | 4 | import com.yoho.search.common.cache.model.CacheObject; |
5 | 5 | ||
6 | +import java.util.BitSet; | ||
7 | +import java.util.Collection; | ||
8 | +import java.util.List; | ||
9 | +import java.util.Map; | ||
10 | + | ||
6 | public interface CacheInterface { | 11 | public interface CacheInterface { |
7 | 12 | ||
8 | - public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute); | ||
9 | - | ||
10 | public boolean exist(RedisKeyBuilder redisKeyBuilder); | 13 | public boolean exist(RedisKeyBuilder redisKeyBuilder); |
11 | 14 | ||
12 | - public void delete(RedisKeyBuilder redisKeyBuilder); | ||
13 | - | ||
14 | public CacheObject get(RedisKeyBuilder redisKeyBuilder); | 15 | public CacheObject get(RedisKeyBuilder redisKeyBuilder); |
15 | 16 | ||
17 | + public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute); | ||
18 | + | ||
19 | + public void mutiSet(final Map<RedisKeyBuilder,String> map, final int expiredTimeInMinute); | ||
20 | + | ||
21 | + public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) ; | ||
22 | + | ||
23 | + public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute); | ||
24 | + | ||
25 | + public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, List<Integer> offsets); | ||
26 | + | ||
16 | } | 27 | } |
1 | package com.yoho.search.common.cache.impls; | 1 | package com.yoho.search.common.cache.impls; |
2 | 2 | ||
3 | -import java.io.InputStream; | ||
4 | -import java.util.ArrayList; | ||
5 | -import java.util.Collection; | ||
6 | -import java.util.List; | ||
7 | -import java.util.Map; | ||
8 | - | ||
9 | -import javax.annotation.PostConstruct; | ||
10 | -import javax.annotation.PreDestroy; | ||
11 | - | ||
12 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | 3 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; |
4 | +import com.yoho.search.common.cache.model.CacheObject; | ||
13 | import net.sf.ehcache.Cache; | 5 | import net.sf.ehcache.Cache; |
14 | import net.sf.ehcache.CacheManager; | 6 | import net.sf.ehcache.CacheManager; |
15 | import net.sf.ehcache.Element; | 7 | import net.sf.ehcache.Element; |
16 | import net.sf.ehcache.Statistics; | 8 | import net.sf.ehcache.Statistics; |
17 | - | ||
18 | import org.slf4j.Logger; | 9 | import org.slf4j.Logger; |
19 | import org.slf4j.LoggerFactory; | 10 | import org.slf4j.LoggerFactory; |
20 | import org.springframework.stereotype.Service; | 11 | import org.springframework.stereotype.Service; |
21 | 12 | ||
22 | -import com.yoho.search.common.cache.model.CacheObject; | 13 | +import javax.annotation.PostConstruct; |
14 | +import javax.annotation.PreDestroy; | ||
15 | +import java.io.InputStream; | ||
16 | +import java.util.*; | ||
23 | 17 | ||
24 | @Service("ehCache") | 18 | @Service("ehCache") |
25 | public class EhCache implements CacheInterface { | 19 | public class EhCache implements CacheInterface { |
@@ -67,15 +61,6 @@ public class EhCache implements CacheInterface { | @@ -67,15 +61,6 @@ public class EhCache implements CacheInterface { | ||
67 | } | 61 | } |
68 | 62 | ||
69 | @Override | 63 | @Override |
70 | - public void delete(RedisKeyBuilder redisKeyBuilder) { | ||
71 | - try { | ||
72 | - cache.remove(redisKeyBuilder.getKey()); | ||
73 | - } catch (Exception e) { | ||
74 | - logger.error(e.getMessage(), e); | ||
75 | - } | ||
76 | - } | ||
77 | - | ||
78 | - @Override | ||
79 | public CacheObject get(RedisKeyBuilder redisKeyBuilder) { | 64 | public CacheObject get(RedisKeyBuilder redisKeyBuilder) { |
80 | try { | 65 | try { |
81 | Element e = cache.get(redisKeyBuilder.getKey()); | 66 | Element e = cache.get(redisKeyBuilder.getKey()); |
@@ -91,12 +76,13 @@ public class EhCache implements CacheInterface { | @@ -91,12 +76,13 @@ public class EhCache implements CacheInterface { | ||
91 | return this.get(redisKeyBuilder) == null ? false : true; | 76 | return this.get(redisKeyBuilder) == null ? false : true; |
92 | } | 77 | } |
93 | 78 | ||
94 | - public void mutiSet(final Map<RedisKeyBuilder,String> map, final int timeoutInSecond) { | 79 | + @Override |
80 | + public void mutiSet(final Map<RedisKeyBuilder,String> map, final int expiredTimeInMinute) { | ||
95 | try { | 81 | try { |
96 | Collection<Element> elements = new ArrayList<>(); | 82 | Collection<Element> elements = new ArrayList<>(); |
97 | for (Map.Entry<RedisKeyBuilder, ? extends String> entry: map.entrySet()) { | 83 | for (Map.Entry<RedisKeyBuilder, ? extends String> entry: map.entrySet()) { |
98 | Element element = new Element(entry.getKey().getKey(), entry.getValue()); | 84 | Element element = new Element(entry.getKey().getKey(), entry.getValue()); |
99 | - element.setTimeToLive(timeoutInSecond); | 85 | + element.setTimeToLive(expiredTimeInMinute * 60); |
100 | elements.add(element); | 86 | elements.add(element); |
101 | } | 87 | } |
102 | cache.putAll(elements); | 88 | cache.putAll(elements); |
@@ -105,6 +91,7 @@ public class EhCache implements CacheInterface { | @@ -105,6 +91,7 @@ public class EhCache implements CacheInterface { | ||
105 | } | 91 | } |
106 | } | 92 | } |
107 | 93 | ||
94 | + @Override | ||
108 | public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) { | 95 | public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) { |
109 | Collection<String> keys = new ArrayList<>(); | 96 | Collection<String> keys = new ArrayList<>(); |
110 | for (RedisKeyBuilder redisKeyBuilder:redisKeyBuilders) { | 97 | for (RedisKeyBuilder redisKeyBuilder:redisKeyBuilders) { |
@@ -121,6 +108,37 @@ public class EhCache implements CacheInterface { | @@ -121,6 +108,37 @@ public class EhCache implements CacheInterface { | ||
121 | } | 108 | } |
122 | } | 109 | } |
123 | return values; | 110 | return values; |
111 | + } | ||
124 | 112 | ||
113 | + @Override | ||
114 | + public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) { | ||
115 | + try { | ||
116 | + Element element = new Element(redisKeyBuilder.getKey(),bitSet); | ||
117 | + element.setTimeToLive(expiredTimeInMinute * 60); | ||
118 | + cache.put(element); | ||
119 | + return true; | ||
120 | + } catch (Exception e) { | ||
121 | + logger.error(e.getMessage(), e); | ||
122 | + return false; | ||
123 | + } | ||
124 | + } | ||
125 | + | ||
126 | + @Override | ||
127 | + public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, List<Integer> offsets){ | ||
128 | + try { | ||
129 | + Element e = this.cache.get(key.getKey()); | ||
130 | + if(e==null){ | ||
131 | + return null; | ||
132 | + } | ||
133 | + BitSet cacheBitSet = (BitSet)e.getObjectValue(); | ||
134 | + Map<Integer, Boolean> results = new HashMap<>(); | ||
135 | + for (Integer offset:offsets) { | ||
136 | + results.put(offset,cacheBitSet.get(offset)); | ||
137 | + } | ||
138 | + return results; | ||
139 | + } catch (Exception e) { | ||
140 | + logger.error(e.getMessage(), e); | ||
141 | + return null; | ||
142 | + } | ||
125 | } | 143 | } |
126 | } | 144 | } |
@@ -11,56 +11,86 @@ import org.slf4j.LoggerFactory; | @@ -11,56 +11,86 @@ import org.slf4j.LoggerFactory; | ||
11 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
12 | import org.springframework.stereotype.Service; | 12 | import org.springframework.stereotype.Service; |
13 | 13 | ||
14 | +import java.util.BitSet; | ||
15 | +import java.util.Collection; | ||
16 | +import java.util.List; | ||
17 | +import java.util.Map; | ||
18 | + | ||
14 | @Service("searchRedis") | 19 | @Service("searchRedis") |
15 | public class SearchRedis implements CacheInterface { | 20 | public class SearchRedis implements CacheInterface { |
16 | 21 | ||
17 | - private static final Logger logger = LoggerFactory.getLogger(SearchRedis.class); | 22 | + private static final Logger logger = LoggerFactory.getLogger(SearchRedis.class); |
23 | + | ||
24 | + @Autowired | ||
25 | + private YohoSearchRedisComponent yohoSearchRedisComponent; | ||
26 | + | ||
27 | + @Override | ||
28 | + public boolean exist(RedisKeyBuilder redisKeyBuilder) { | ||
29 | + try { | ||
30 | + YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate(); | ||
31 | + return RedisCacheUtils.exist(redisTemplate, redisKeyBuilder); | ||
32 | + } catch (Exception e) { | ||
33 | + logger.error(e.getMessage(), e); | ||
34 | + return false; | ||
35 | + } | ||
36 | + } | ||
18 | 37 | ||
19 | - @Autowired | ||
20 | - private YohoSearchRedisComponent yohoSearchRedisComponent; | 38 | + @Override |
39 | + public CacheObject get(RedisKeyBuilder redisKeyBuilder) { | ||
40 | + try { | ||
41 | + YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations(); | ||
42 | + return RedisCacheUtils.get(valueOperations, redisKeyBuilder, CacheObject.class); | ||
43 | + } catch (Exception e) { | ||
44 | + logger.error(e.getMessage(), e); | ||
45 | + return null; | ||
46 | + } | ||
47 | + } | ||
21 | 48 | ||
22 | - @Override | ||
23 | - public CacheObject get(RedisKeyBuilder redisKeyBuilder) { | ||
24 | - try { | ||
25 | - YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations(); | ||
26 | - return RedisCacheUtils.get(valueOperations, redisKeyBuilder, CacheObject.class); | ||
27 | - } catch (Exception e) { | ||
28 | - logger.error(e.getMessage(), e); | ||
29 | - return null; | ||
30 | - } | ||
31 | - } | 49 | + @Override |
50 | + public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) { | ||
51 | + try { | ||
52 | + YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations(); | ||
53 | + RedisCacheUtils.add(valueOperations, redisKeyBuilder, value, expiredTimeInMinute); | ||
54 | + } catch (Exception e) { | ||
55 | + logger.error(e.getMessage(), e); | ||
56 | + } | ||
57 | + } | ||
32 | 58 | ||
33 | - @Override | ||
34 | - public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) { | ||
35 | - try { | ||
36 | - YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations(); | ||
37 | - RedisCacheUtils.add(valueOperations, redisKeyBuilder, value, expiredTimeInMinute); | ||
38 | - } catch (Exception e) { | ||
39 | - logger.error(e.getMessage(), e); | ||
40 | - } | 59 | + @Override |
60 | + public void mutiSet(Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) { | ||
61 | + YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate(); | ||
62 | + RedisCacheUtils.mutiSet(redisTemplate, map, expiredTimeInMinute); | ||
63 | + } | ||
41 | 64 | ||
42 | - } | 65 | + @Override |
66 | + public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) { | ||
67 | + YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations(); | ||
68 | + return RedisCacheUtils.mutiGet(valueOperations, redisKeyBuilders); | ||
69 | + } | ||
43 | 70 | ||
44 | - @Override | ||
45 | - public void delete(RedisKeyBuilder redisKeyBuilder) { | ||
46 | - try { | ||
47 | - YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate(); | ||
48 | - RedisCacheUtils.delete(redisTemplate, redisKeyBuilder); | ||
49 | - } catch (Exception e) { | ||
50 | - logger.error(e.getMessage(), e); | ||
51 | - } | ||
52 | 71 | ||
53 | - } | 72 | + @Override |
73 | + public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) { | ||
74 | + try { | ||
75 | + return RedisCacheUtils.setBitSet(yohoSearchRedisComponent.getRedisTemplate(), redisKeyBuilder, bitSet, expiredTimeInMinute); | ||
76 | + } catch (Exception e) { | ||
77 | + logger.error(e.getMessage(), e); | ||
78 | + return false; | ||
79 | + } | ||
80 | + } | ||
54 | 81 | ||
55 | - @Override | ||
56 | - public boolean exist(RedisKeyBuilder redisKeyBuilder) { | ||
57 | - try { | ||
58 | - YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate(); | ||
59 | - return RedisCacheUtils.exist(redisTemplate, redisKeyBuilder); | ||
60 | - } catch (Exception e) { | ||
61 | - logger.error(e.getMessage(), e); | ||
62 | - return false; | ||
63 | - } | ||
64 | - } | 82 | + @Override |
83 | + public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, List<Integer> offsets) { | ||
84 | + try { | ||
85 | + YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate(); | ||
86 | + if (!redisTemplate.hasKey(key)) { | ||
87 | + return null; | ||
88 | + } | ||
89 | + return RedisCacheUtils.getBits(redisTemplate, key, offsets); | ||
90 | + } catch (Exception e) { | ||
91 | + logger.error(e.getMessage(), e); | ||
92 | + return null; | ||
93 | + } | ||
94 | + } | ||
65 | 95 | ||
66 | } | 96 | } |
@@ -13,58 +13,88 @@ import org.slf4j.LoggerFactory; | @@ -13,58 +13,88 @@ import org.slf4j.LoggerFactory; | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | 13 | import org.springframework.beans.factory.annotation.Autowired; |
14 | import org.springframework.stereotype.Service; | 14 | import org.springframework.stereotype.Service; |
15 | 15 | ||
16 | +import java.util.BitSet; | ||
17 | +import java.util.Collection; | ||
18 | +import java.util.List; | ||
19 | +import java.util.Map; | ||
20 | + | ||
16 | /** | 21 | /** |
17 | * 使用有货共用的不同步redis | 22 | * 使用有货共用的不同步redis |
18 | - * | ||
19 | - * @author hugufei | ||
20 | * | 23 | * |
24 | + * @author hugufei | ||
21 | */ | 25 | */ |
22 | 26 | ||
23 | @Service("yohoRedis") | 27 | @Service("yohoRedis") |
24 | public class YohoRedis implements CacheInterface { | 28 | public class YohoRedis implements CacheInterface { |
25 | 29 | ||
26 | - private static final Logger logger = LoggerFactory.getLogger(YohoRedis.class); | 30 | + private static final Logger logger = LoggerFactory.getLogger(YohoRedis.class); |
31 | + | ||
32 | + @Autowired | ||
33 | + private YohoNoSyncRedisComponent yohoNoSyncRedisComponent; | ||
34 | + | ||
35 | + @Override | ||
36 | + public boolean exist(RedisKeyBuilder redisKeyBuilder) { | ||
37 | + try { | ||
38 | + return RedisCacheUtils.exist(yohoNoSyncRedisComponent.getRedisTemplate(), redisKeyBuilder); | ||
39 | + } catch (Exception e) { | ||
40 | + logger.error(e.getMessage(), e); | ||
41 | + return false; | ||
42 | + } | ||
43 | + } | ||
44 | + | ||
45 | + @Override | ||
46 | + public CacheObject get(RedisKeyBuilder redisKeyBuilder) { | ||
47 | + try { | ||
48 | + return RedisCacheUtils.get(yohoNoSyncRedisComponent.getValueOperations(), redisKeyBuilder, CacheObject.class); | ||
49 | + } catch (Exception e) { | ||
50 | + logger.error(e.getMessage(), e); | ||
51 | + return null; | ||
52 | + } | ||
53 | + } | ||
54 | + | ||
55 | + @Override | ||
56 | + public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) { | ||
57 | + try { | ||
58 | + RedisCacheUtils.add(yohoNoSyncRedisComponent.getValueOperations(), redisKeyBuilder, value, expiredTimeInMinute); | ||
59 | + } catch (Exception e) { | ||
60 | + logger.error(e.getMessage(), e); | ||
61 | + } | ||
62 | + } | ||
63 | + | ||
27 | 64 | ||
28 | - @Autowired | ||
29 | - private YohoNoSyncRedisComponent yohoNoSyncRedisComponent; | 65 | + @Override |
66 | + public void mutiSet(Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) { | ||
67 | + YHRedisTemplate redisTemplate = yohoNoSyncRedisComponent.getRedisTemplate(); | ||
68 | + RedisCacheUtils.mutiSet(redisTemplate, map, expiredTimeInMinute); | ||
69 | + } | ||
30 | 70 | ||
31 | - @Override | ||
32 | - public CacheObject get(RedisKeyBuilder redisKeyBuilder) { | ||
33 | - try { | ||
34 | - return RedisCacheUtils.get(yohoNoSyncRedisComponent.getValueOperations(), redisKeyBuilder, CacheObject.class); | ||
35 | - } catch (Exception e) { | ||
36 | - logger.error(e.getMessage(), e); | ||
37 | - return null; | ||
38 | - } | ||
39 | - } | ||
40 | - | ||
41 | - @Override | ||
42 | - public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) { | ||
43 | - try { | ||
44 | - RedisCacheUtils.add(yohoNoSyncRedisComponent.getValueOperations(), redisKeyBuilder, value, expiredTimeInMinute); | ||
45 | - } catch (Exception e) { | ||
46 | - logger.error(e.getMessage(), e); | ||
47 | - } | ||
48 | - } | ||
49 | - | 71 | + @Override |
72 | + public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) { | ||
73 | + YHValueOperations valueOperations = yohoNoSyncRedisComponent.getValueOperations(); | ||
74 | + return RedisCacheUtils.mutiGet(valueOperations, redisKeyBuilders); | ||
75 | + } | ||
50 | 76 | ||
51 | - @Override | ||
52 | - public void delete(RedisKeyBuilder redisKeyBuilder) { | ||
53 | - try { | ||
54 | - RedisCacheUtils.delete(yohoNoSyncRedisComponent.getRedisTemplate(), redisKeyBuilder); | ||
55 | - } catch (Exception e) { | ||
56 | - logger.error(e.getMessage(), e); | ||
57 | - } | ||
58 | - } | 77 | + @Override |
78 | + public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) { | ||
79 | + try { | ||
80 | + return RedisCacheUtils.setBitSet(yohoNoSyncRedisComponent.getRedisTemplate(), redisKeyBuilder, bitSet, expiredTimeInMinute); | ||
81 | + } catch (Exception e) { | ||
82 | + logger.error(e.getMessage(), e); | ||
83 | + return false; | ||
84 | + } | ||
85 | + } | ||
59 | 86 | ||
60 | - @Override | ||
61 | - public boolean exist(RedisKeyBuilder redisKeyBuilder) { | ||
62 | - try { | ||
63 | - return RedisCacheUtils.exist(yohoNoSyncRedisComponent.getRedisTemplate(), redisKeyBuilder); | ||
64 | - } catch (Exception e) { | ||
65 | - logger.error(e.getMessage(), e); | ||
66 | - return false; | ||
67 | - } | ||
68 | - } | 87 | + @Override |
88 | + public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, List<Integer> offsets) { | ||
89 | + try { | ||
90 | + if (!this.exist(key)) { | ||
91 | + return null; | ||
92 | + } | ||
93 | + return RedisCacheUtils.getBits(yohoNoSyncRedisComponent.getRedisTemplate(), key, offsets); | ||
94 | + } catch (Exception e) { | ||
95 | + logger.error(e.getMessage(), e); | ||
96 | + return null; | ||
97 | + } | ||
98 | + } | ||
69 | 99 | ||
70 | } | 100 | } |
@@ -5,9 +5,12 @@ import com.alibaba.fastjson.serializer.SerializerFeature; | @@ -5,9 +5,12 @@ import com.alibaba.fastjson.serializer.SerializerFeature; | ||
5 | import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate; | 5 | import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate; |
6 | import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations; | 6 | import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations; |
7 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | 7 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; |
8 | +import com.yoho.search.base.utils.CollectionUtils; | ||
8 | import org.apache.commons.lang3.StringUtils; | 9 | import org.apache.commons.lang3.StringUtils; |
9 | 10 | ||
11 | +import java.util.*; | ||
10 | import java.util.concurrent.TimeUnit; | 12 | import java.util.concurrent.TimeUnit; |
13 | +import java.util.stream.Collectors; | ||
11 | 14 | ||
12 | public class RedisCacheUtils { | 15 | public class RedisCacheUtils { |
13 | 16 | ||
@@ -29,15 +32,51 @@ public class RedisCacheUtils { | @@ -29,15 +32,51 @@ public class RedisCacheUtils { | ||
29 | } | 32 | } |
30 | valueOperations.set(redisKeyBuilder, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES); | 33 | valueOperations.set(redisKeyBuilder, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES); |
31 | } | 34 | } |
32 | - | ||
33 | - public static void delete(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder) { | ||
34 | - redisTemplate.delete(redisKeyBuilder); | ||
35 | - } | ||
36 | 35 | ||
37 | public static boolean exist(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder) { | 36 | public static boolean exist(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder) { |
38 | return redisTemplate.hasKey(redisKeyBuilder); | 37 | return redisTemplate.hasKey(redisKeyBuilder); |
39 | } | 38 | } |
40 | 39 | ||
40 | + public static void mutiSet(YHRedisTemplate redisTemplate, Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) { | ||
41 | + redisTemplate.mset(map, expiredTimeInMinute*60); | ||
42 | + } | ||
43 | + | ||
44 | + public static List<String> mutiGet(YHValueOperations valueOperations, Collection<RedisKeyBuilder> redisKeyBuilders) { | ||
45 | + return valueOperations.multiGet(redisKeyBuilders); | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * 设置bitset-整个设置 | ||
50 | + * @param redisTemplate | ||
51 | + * @param redisKeyBuilder | ||
52 | + * @param bitSet | ||
53 | + * @param expiredTimeInMinute | ||
54 | + * @return | ||
55 | + */ | ||
56 | + public static boolean setBitSet(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) { | ||
57 | + return redisTemplate.setBits(redisKeyBuilder,bitSet,expiredTimeInMinute * 60); | ||
58 | + } | ||
59 | + | ||
60 | + /** | ||
61 | + * 获取bitset | ||
62 | + * @param redisTemplate | ||
63 | + * @param redisKeyBuilder | ||
64 | + * @param offsets | ||
65 | + * @return | ||
66 | + */ | ||
67 | + public static Map<Integer, Boolean> getBits(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder, List<Integer> offsets) { | ||
68 | + List<Long> LongOffsets = CollectionUtils.toList(offsets,(a)->a.longValue()); | ||
69 | + Map<Long, Boolean> redisResults = redisTemplate.getBits(redisKeyBuilder,LongOffsets); | ||
70 | + if(redisResults==null || redisResults.isEmpty()){ | ||
71 | + return null; | ||
72 | + } | ||
73 | + Map<Integer, Boolean> results = new HashMap<>(); | ||
74 | + for (Map.Entry<Long, Boolean> entry: redisResults.entrySet()) { | ||
75 | + results.put(entry.getKey().intValue(),entry.getValue()); | ||
76 | + } | ||
77 | + return results; | ||
78 | + } | ||
79 | + | ||
41 | /** | 80 | /** |
42 | * 序列化保持null值 | 81 | * 序列化保持null值 |
43 | * | 82 | * |
@@ -4,7 +4,7 @@ import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | @@ -4,7 +4,7 @@ import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | ||
4 | import com.yoho.search.base.utils.CollectionUtils; | 4 | import com.yoho.search.base.utils.CollectionUtils; |
5 | import com.yoho.search.base.utils.Transfer; | 5 | import com.yoho.search.base.utils.Transfer; |
6 | import com.yoho.search.common.cache.impls.EhCache; | 6 | import com.yoho.search.common.cache.impls.EhCache; |
7 | -import com.yoho.search.core.redis.components.YohoSearchRedisComponent; | 7 | +import com.yoho.search.common.cache.impls.SearchRedis; |
8 | import com.yoho.search.recall.scene.models.common.AbstractCacheRequestResponse; | 8 | import com.yoho.search.recall.scene.models.common.AbstractCacheRequestResponse; |
9 | import com.yoho.search.recall.scene.models.common.ICacheRequest; | 9 | import com.yoho.search.recall.scene.models.common.ICacheRequest; |
10 | import org.apache.commons.lang.StringUtils; | 10 | import org.apache.commons.lang.StringUtils; |
@@ -19,9 +19,9 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | @@ -19,9 +19,9 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | ||
19 | private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL"); | 19 | private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL"); |
20 | 20 | ||
21 | @Autowired | 21 | @Autowired |
22 | - private YohoSearchRedisComponent yohoSearchRedisComponent; | ||
23 | - @Autowired | ||
24 | private EhCache ehCache; | 22 | private EhCache ehCache; |
23 | + @Autowired | ||
24 | + private SearchRedis searchRedis; | ||
25 | 25 | ||
26 | /** | 26 | /** |
27 | * 批量处理请求,以及添加缓存 | 27 | * 批量处理请求,以及添加缓存 |
@@ -170,7 +170,7 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | @@ -170,7 +170,7 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | ||
170 | if(request==null){ | 170 | if(request==null){ |
171 | return 0; | 171 | return 0; |
172 | } | 172 | } |
173 | - return request.cacheTimeInSecond(); | 173 | + return request.cacheTimeInMinute(); |
174 | } | 174 | } |
175 | }); | 175 | }); |
176 | //2、按缓存时间大小直接加入缓存 | 176 | //2、按缓存时间大小直接加入缓存 |
@@ -179,7 +179,7 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | @@ -179,7 +179,7 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | ||
179 | } | 179 | } |
180 | } | 180 | } |
181 | 181 | ||
182 | - private void batchAddResponseToCache(List<RequestResponse> requestResponses,int timeOutInSecond) { | 182 | + private void batchAddResponseToCache(List<RequestResponse> requestResponses,int expiredTimeInMinute) { |
183 | try { | 183 | try { |
184 | Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>(); | 184 | Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>(); |
185 | for (RequestResponse requestResponse : requestResponses) { | 185 | for (RequestResponse requestResponse : requestResponses) { |
@@ -205,7 +205,7 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | @@ -205,7 +205,7 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | ||
205 | if (toCacheMap.isEmpty()) { | 205 | if (toCacheMap.isEmpty()) { |
206 | return; | 206 | return; |
207 | } | 207 | } |
208 | - this.batchAddToCache(toCacheMap, timeOutInSecond); | 208 | + this.batchAddToCache(toCacheMap, expiredTimeInMinute); |
209 | } catch (Exception e) { | 209 | } catch (Exception e) { |
210 | RECALL_NEW_LOGGER.error(e.getMessage(), e); | 210 | RECALL_NEW_LOGGER.error(e.getMessage(), e); |
211 | } | 211 | } |
@@ -215,15 +215,15 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | @@ -215,15 +215,15 @@ public abstract class AbstractCacheBean<Request extends ICacheRequest, Response, | ||
215 | if (useEhCache()){ | 215 | if (useEhCache()){ |
216 | return ehCache.mutiGet(keys); | 216 | return ehCache.mutiGet(keys); |
217 | } else { | 217 | } else { |
218 | - return yohoSearchRedisComponent.getValueOperations().multiGet(keys); | 218 | + return searchRedis.mutiGet(keys); |
219 | } | 219 | } |
220 | } | 220 | } |
221 | 221 | ||
222 | - private void batchAddToCache(Map<RedisKeyBuilder, String> toCacheMap,int timeOutInSecond) { | 222 | + private void batchAddToCache(Map<RedisKeyBuilder, String> toCacheMap,int expiredTimeInMinute) { |
223 | if (useEhCache()) { | 223 | if (useEhCache()) { |
224 | - ehCache.mutiSet(toCacheMap, timeOutInSecond); | 224 | + ehCache.mutiSet(toCacheMap, expiredTimeInMinute); |
225 | } else { | 225 | } else { |
226 | - yohoSearchRedisComponent.getRedisTemplate().mset(toCacheMap, timeOutInSecond); | 226 | + searchRedis.mutiSet(toCacheMap,expiredTimeInMinute); |
227 | } | 227 | } |
228 | } | 228 | } |
229 | 229 |
@@ -15,12 +15,12 @@ public interface IStrategy { | @@ -15,12 +15,12 @@ public interface IStrategy { | ||
15 | 15 | ||
16 | String strategyCacheKey();// 可用于缓存的key | 16 | String strategyCacheKey();// 可用于缓存的key |
17 | 17 | ||
18 | - int cacheTimeInSecond();//缓存时间 | 18 | + int cacheTimeInMinute();//缓存时间 |
19 | 19 | ||
20 | default StringBuilder defaultStrategyKey(){ | 20 | default StringBuilder defaultStrategyKey(){ |
21 | StringBuilder sb = new StringBuilder(); | 21 | StringBuilder sb = new StringBuilder(); |
22 | sb.append(this.strategtEnum().name()); | 22 | sb.append(this.strategtEnum().name()); |
23 | - sb.append(this.cacheTimeInSecond()); | 23 | + sb.append(this.cacheTimeInMinute()); |
24 | sb.append(this.size()); | 24 | sb.append(this.size()); |
25 | return sb; | 25 | return sb; |
26 | } | 26 | } |
@@ -43,7 +43,7 @@ public class CommonAddFlowStrategy implements IStrategy { | @@ -43,7 +43,7 @@ public class CommonAddFlowStrategy implements IStrategy { | ||
43 | } | 43 | } |
44 | 44 | ||
45 | @Override | 45 | @Override |
46 | - public int cacheTimeInSecond() { | 46 | + public int cacheTimeInMinute() { |
47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; | 47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; |
48 | } | 48 | } |
49 | 49 |
@@ -43,7 +43,7 @@ public class CommonDirectTrainStrategy implements IStrategy { | @@ -43,7 +43,7 @@ public class CommonDirectTrainStrategy implements IStrategy { | ||
43 | } | 43 | } |
44 | 44 | ||
45 | @Override | 45 | @Override |
46 | - public int cacheTimeInSecond() { | 46 | + public int cacheTimeInMinute() { |
47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; | 47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; |
48 | } | 48 | } |
49 | 49 |
@@ -48,7 +48,7 @@ public class CommonFirstSknStrategy implements IStrategy { | @@ -48,7 +48,7 @@ public class CommonFirstSknStrategy implements IStrategy { | ||
48 | } | 48 | } |
49 | 49 | ||
50 | @Override | 50 | @Override |
51 | - public int cacheTimeInSecond() { | 51 | + public int cacheTimeInMinute() { |
52 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; | 52 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; |
53 | } | 53 | } |
54 | 54 |
service/src/main/java/com/yoho/search/recall/scene/beans/strategy/impls/CommonHeatValueStrategy.java
@@ -43,7 +43,7 @@ public class CommonHeatValueStrategy implements IStrategy { | @@ -43,7 +43,7 @@ public class CommonHeatValueStrategy implements IStrategy { | ||
43 | } | 43 | } |
44 | 44 | ||
45 | @Override | 45 | @Override |
46 | - public int cacheTimeInSecond() { | 46 | + public int cacheTimeInMinute() { |
47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; | 47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; |
48 | } | 48 | } |
49 | 49 |
@@ -43,7 +43,7 @@ public class CommonNewShopStrategy implements IStrategy { | @@ -43,7 +43,7 @@ public class CommonNewShopStrategy implements IStrategy { | ||
43 | } | 43 | } |
44 | 44 | ||
45 | @Override | 45 | @Override |
46 | - public int cacheTimeInSecond() { | 46 | + public int cacheTimeInMinute() { |
47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; | 47 | return CacheTimeConstants.COMMON_RECALL_STRATEGY_CACHE_TIME; |
48 | } | 48 | } |
49 | 49 |
@@ -43,7 +43,7 @@ public class RecommendSknStrategy implements IStrategy { | @@ -43,7 +43,7 @@ public class RecommendSknStrategy implements IStrategy { | ||
43 | } | 43 | } |
44 | 44 | ||
45 | @Override | 45 | @Override |
46 | - public int cacheTimeInSecond() { | 46 | + public int cacheTimeInMinute() { |
47 | return CacheTimeConstants.RECOMMEND_SKN_CACHE_TIME; | 47 | return CacheTimeConstants.RECOMMEND_SKN_CACHE_TIME; |
48 | } | 48 | } |
49 | 49 |
@@ -21,7 +21,7 @@ public abstract class SortBrandAbstractStrategy implements IStrategy { | @@ -21,7 +21,7 @@ public abstract class SortBrandAbstractStrategy implements IStrategy { | ||
21 | } | 21 | } |
22 | 22 | ||
23 | @Override | 23 | @Override |
24 | - public int cacheTimeInSecond() { | 24 | + public int cacheTimeInMinute() { |
25 | return CacheTimeConstants.SORT_BRAND_RECALL_STRATEGY_CACHE_TIME; | 25 | return CacheTimeConstants.SORT_BRAND_RECALL_STRATEGY_CACHE_TIME; |
26 | } | 26 | } |
27 | 27 |
@@ -7,27 +7,27 @@ package com.yoho.search.recall.scene.constants; | @@ -7,27 +7,27 @@ package com.yoho.search.recall.scene.constants; | ||
7 | public class CacheTimeConstants { | 7 | public class CacheTimeConstants { |
8 | 8 | ||
9 | //通用召回的缓存 - 10分钟 | 9 | //通用召回的缓存 - 10分钟 |
10 | - public static final int COMMON_RECALL_STRATEGY_CACHE_TIME = 10 * 60; | 10 | + public static final int COMMON_RECALL_STRATEGY_CACHE_TIME = 10 ; |
11 | 11 | ||
12 | //SKN的的缓存 - 60分钟 | 12 | //SKN的的缓存 - 60分钟 |
13 | - public static final int RECOMMEND_SKN_CACHE_TIME = 60 * 60; | 13 | + public static final int RECOMMEND_SKN_CACHE_TIME = 60 ; |
14 | 14 | ||
15 | //品类+品牌的缓存 - 60分钟 | 15 | //品类+品牌的缓存 - 60分钟 |
16 | - public static final int SORT_BRAND_RECALL_STRATEGY_CACHE_TIME = 60 * 60; | 16 | + public static final int SORT_BRAND_RECALL_STRATEGY_CACHE_TIME = 60 ; |
17 | 17 | ||
18 | //SKN基本信息的缓存-一个小时 | 18 | //SKN基本信息的缓存-一个小时 |
19 | - public static final int SKN_BASE_INFO = 60 * 60; | 19 | + public static final int SKN_BASE_INFO = 60 ; |
20 | 20 | ||
21 | //SKN返回信息的缓存 - 10分钟 | 21 | //SKN返回信息的缓存 - 10分钟 |
22 | - public static final int SKN_RETURN_INFO = 10 * 60; | 22 | + public static final int SKN_RETURN_INFO = 10 ; |
23 | 23 | ||
24 | //用户召回结果的缓存-三分钟 | 24 | //用户召回结果的缓存-三分钟 |
25 | - public static final int USER_RECALL_SKN_LIST = 3 * 60; | 25 | + public static final int USER_RECALL_SKN_LIST = 3 ; |
26 | 26 | ||
27 | //页面skn的bitset缓存 | 27 | //页面skn的bitset缓存 |
28 | - public static final int PAGE_SKN_BITSET = 60 * 60; | 28 | + public static final int PAGE_SKN_BITSET = 60 ; |
29 | 29 | ||
30 | //页面个性化因子的缓存 | 30 | //页面个性化因子的缓存 |
31 | - public static final int PAGE_PERSIONAL_FACTOR = 60 * 60; | 31 | + public static final int PAGE_PERSIONAL_FACTOR = 60 ; |
32 | 32 | ||
33 | } | 33 | } |
@@ -38,7 +38,7 @@ public class RecallRequest implements ICacheRequest, IRecallRequest { | @@ -38,7 +38,7 @@ public class RecallRequest implements ICacheRequest, IRecallRequest { | ||
38 | RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance(); | 38 | RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance(); |
39 | redisKeyBuilder.appendFixed("YOHOSEARCH:").appendFixed("RECALL:"); | 39 | redisKeyBuilder.appendFixed("YOHOSEARCH:").appendFixed("RECALL:"); |
40 | redisKeyBuilder.appendFixed(strategy.strategtEnum().name()).appendFixed(":"); | 40 | redisKeyBuilder.appendFixed(strategy.strategtEnum().name()).appendFixed(":"); |
41 | - redisKeyBuilder.appendVar(cacheTimeInSecond()).appendFixed(":"); | 41 | + redisKeyBuilder.appendVar(cacheTimeInMinute()).appendFixed(":"); |
42 | redisKeyBuilder.appendVar(cacheKey); | 42 | redisKeyBuilder.appendVar(cacheKey); |
43 | return redisKeyBuilder; | 43 | return redisKeyBuilder; |
44 | } | 44 | } |
@@ -49,8 +49,8 @@ public class RecallRequest implements ICacheRequest, IRecallRequest { | @@ -49,8 +49,8 @@ public class RecallRequest implements ICacheRequest, IRecallRequest { | ||
49 | } | 49 | } |
50 | 50 | ||
51 | @Override | 51 | @Override |
52 | - public int cacheTimeInSecond() { | ||
53 | - return this.strategy.cacheTimeInSecond(); | 52 | + public int cacheTimeInMinute() { |
53 | + return this.strategy.cacheTimeInMinute(); | ||
54 | } | 54 | } |
55 | 55 | ||
56 | @Override | 56 | @Override |
@@ -18,11 +18,11 @@ public class SknBaseInfoRequest implements ICacheRequest{ | @@ -18,11 +18,11 @@ public class SknBaseInfoRequest implements ICacheRequest{ | ||
18 | 18 | ||
19 | @Override | 19 | @Override |
20 | public RedisKeyBuilder redisKeyBuilder() { | 20 | public RedisKeyBuilder redisKeyBuilder() { |
21 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN_BASE_INFO:").appendVar(cacheTimeInSecond()).appendFixed(":").appendVar(productSkn); | 21 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN_BASE_INFO:").appendVar(cacheTimeInMinute()).appendFixed(":").appendVar(productSkn); |
22 | } | 22 | } |
23 | 23 | ||
24 | @Override | 24 | @Override |
25 | - public int cacheTimeInSecond() { | 25 | + public int cacheTimeInMinute() { |
26 | return CacheTimeConstants.SKN_BASE_INFO; | 26 | return CacheTimeConstants.SKN_BASE_INFO; |
27 | } | 27 | } |
28 | 28 |
@@ -14,11 +14,11 @@ public class SknReturnInfoResquest implements ICacheRequest { | @@ -14,11 +14,11 @@ public class SknReturnInfoResquest implements ICacheRequest { | ||
14 | 14 | ||
15 | @Override | 15 | @Override |
16 | public RedisKeyBuilder redisKeyBuilder() { | 16 | public RedisKeyBuilder redisKeyBuilder() { |
17 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN_INFO:").appendVar(cacheTimeInSecond()).appendFixed(":").appendVar(productSkn); | 17 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN_INFO:").appendVar(cacheTimeInMinute()).appendFixed(":").appendVar(productSkn); |
18 | } | 18 | } |
19 | 19 | ||
20 | @Override | 20 | @Override |
21 | - public int cacheTimeInSecond() { | 21 | + public int cacheTimeInMinute() { |
22 | return CacheTimeConstants.SKN_RETURN_INFO; | 22 | return CacheTimeConstants.SKN_RETURN_INFO; |
23 | } | 23 | } |
24 | 24 |
@@ -55,11 +55,11 @@ public class UserRecallRequest implements ICacheRequest { | @@ -55,11 +55,11 @@ public class UserRecallRequest implements ICacheRequest { | ||
55 | stringBuilder.append(uid); | 55 | stringBuilder.append(uid); |
56 | stringBuilder.append(udid==null?"": udid); | 56 | stringBuilder.append(udid==null?"": udid); |
57 | String value = MD5Util.string2MD5(stringBuilder.toString()); | 57 | String value = MD5Util.string2MD5(stringBuilder.toString()); |
58 | - return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("USER_RECALL_SKN_LIST:").appendVar(cacheTimeInSecond()).appendFixed(":").appendVar(value); | 58 | + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("USER_RECALL_SKN_LIST:").appendVar(cacheTimeInMinute()).appendFixed(":").appendVar(value); |
59 | } | 59 | } |
60 | 60 | ||
61 | @Override | 61 | @Override |
62 | - public int cacheTimeInSecond() { | 62 | + public int cacheTimeInMinute() { |
63 | return CacheTimeConstants.USER_RECALL_SKN_LIST; | 63 | return CacheTimeConstants.USER_RECALL_SKN_LIST; |
64 | } | 64 | } |
65 | 65 |
-
Please register or login to post a comment