Authored by ping

add redis

... ... @@ -36,6 +36,10 @@
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-redis</artifactId>
</dependency>
</dependencies>
<build>
... ...
... ... @@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
... ... @@ -12,9 +13,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.activity.service.DrawService;
import com.yoho.core.cache.CacheClient;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.core.redis.YHValueOperations;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.union.request.DrawRequestBO;
... ... @@ -34,8 +37,15 @@ public class DrawServiceImpl implements DrawService {
@Resource
IUserBaseDAO userBaseDAO;
// @Resource
// CacheClient cacheClient;
@Resource
CacheClient cacheClient;
YHValueOperations<String, String> valueOperations;
@Resource(name = "yhRedisTemplate")
private YHRedisTemplate redis;
/**
* 通用的抽签
... ... @@ -117,9 +127,14 @@ public class DrawServiceImpl implements DrawService {
//获取参与的人员列表
Map<Integer, JSONObject> maps = cacheClient.get(DRAW_USER_LIST_KEY, Map.class);
if (maps == null) {
maps = new HashMap<Integer, JSONObject>();
// redis.opsForHash().get(DRAW_USER_LIST_KEY, hashKey)
// Map<Integer, JSONObject> maps = cacheClient.get(DRAW_USER_LIST_KEY, Map.class);
Map<String, JSONObject> maps = null;
String arr = valueOperations.get(DRAW_USER_LIST_KEY);
if (StringUtils.isEmpty(arr)) {
maps = new HashMap<String, JSONObject>();
} else {
maps = (Map) JSONObject.parse(arr);
}
JSONObject json = new JSONObject();
json.put("randomCode", response.getRandomCode());
... ... @@ -127,10 +142,12 @@ public class DrawServiceImpl implements DrawService {
json.put("nickname", request.getNickname());
json.put("gender", request.getGender());
json.put("birthday", request.getBirthday());
maps.put(request.getUid(), json);
maps.put(String.valueOf(request.getUid()), json);
//保存到缓存
cacheClient.set(DRAW_USER_LIST_KEY, 30 * 24 * 60 * 60, maps);
log.info("draw total nums is {}", maps.size());
// redis.longExpire(DRAW_USER_LIST_KEY, 30 * 24 * 60 * 60, TimeUnit.SECONDS);
valueOperations.set(DRAW_USER_LIST_KEY, JSON.toJSONString(maps), 30 * 24 * 60 * 60, TimeUnit.SECONDS);
// cacheClient.set(DRAW_USER_LIST_KEY, 30 * 24 * 60 * 60, maps);
// log.info("draw total nums is {}", maps.size());
return response;
}
... ... @@ -144,11 +161,15 @@ public class DrawServiceImpl implements DrawService {
throw new ServiceException(ServiceError.UID_IS_NULL);
}
//获取参与的人员列表
Map<Integer, JSONObject> maps = cacheClient.get(DRAW_USER_LIST_KEY, Map.class);
if (maps == null) {
maps = new HashMap<Integer, JSONObject>();
// Map<Integer, JSONObject> maps = cacheClient.get(DRAW_USER_LIST_KEY, Map.class);
Map<String, JSONObject> maps = null;
String arr = valueOperations.get(DRAW_USER_LIST_KEY);
if (StringUtils.isEmpty(arr)) {
maps = new HashMap<String, JSONObject>();
} else {
maps = (Map) JSONObject.parse(arr);
}
JSONObject json = maps.get(uid);
JSONObject json = maps.get(String.valueOf(uid));
DrawInfoResponseBO response = new DrawInfoResponseBO();
if (json == null) {
return response;
... ...
... ... @@ -12,7 +12,14 @@ rabbit.mq.virtualHost=/yoho
#激活有效期(秒)
activeTime=3600
# ******************** redis servers ********************
redis.readonly.proxy.address=192.168.102.217
redis.readonly.proxy.port=6479
redis.readonly.proxy.auth=
redis.proxy.address=192.168.102.217
redis.proxy.port=6379
redis.proxy.auth=
zkAddress=127.0.0.1:2181
# web context
... ...
... ... @@ -13,6 +13,15 @@ rabbit.mq.virtualHost=${rabbit.mq.virtualHost}
activeTime=${activeTime}
# ******************** redis servers ********************
redis.proxy.address=${redis.proxy.address}
redis.proxy.port=${redis.proxy.port}
redis.proxy.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}
#zookeeper地址
zkAddress=${zkAddress}
# web context
... ...