generated from root/miduo_server
玩家查询缓存
parent
a93bc47c72
commit
4228cb9fd1
|
|
@ -162,7 +162,7 @@ public class CdkInfoController {
|
|||
infoVo.setCdkId(v.getCdk());
|
||||
infoVo.setUseTime(DateUtil.stampToTime(v.getCreateTime()));
|
||||
// 玩家信息
|
||||
GSUser gsUser = gsUserDao.findUserInfo(v.getServerId(), v.getUid());
|
||||
GSUser gsUser = gsUserDao.findUserInfoQuick(v.getServerId(), v.getUid());
|
||||
infoVo.setUserId(v.getUid());
|
||||
infoVo.setUserName(gsUser.getPlayerManager().getNickName());
|
||||
infoVo.setOpenId(gsUser.getPlayerManager().getOpenId());
|
||||
|
|
|
|||
|
|
@ -166,13 +166,13 @@ public class OrderInfoController {
|
|||
}
|
||||
corder.setRecharge_type(cgPayOrder.getRecharge_type());
|
||||
|
||||
GSUser gsUser = gsUserDao.findUserInfo(server,Integer.valueOf(accountid));
|
||||
GSUserFinal gsUser = gsUserDao.findUserInfoCache(server,Integer.valueOf(accountid));
|
||||
if (gsUser == null){
|
||||
corder.setRegisterTime("");
|
||||
corder.setOpenId("");
|
||||
}else {
|
||||
corder.setRegisterTime(DateUtil.stampToTime(gsUser.getPlayerManager().getCreateTime()));
|
||||
corder.setOpenId(gsUser.getPlayerManager().getOpenId());
|
||||
corder.setRegisterTime(gsUser.getCreateTime());
|
||||
corder.setOpenId(gsUser.getOpenId());
|
||||
}
|
||||
|
||||
ChannelInfo infoById = channelInfoDao.getChannelInfoById(ccId);
|
||||
|
|
|
|||
|
|
@ -367,7 +367,7 @@ public class SupportController {
|
|||
return 0;
|
||||
}
|
||||
// 用户详细信息
|
||||
GSUser gsUser = gsUserDao.findUserInfo(cUserInfo.getServerid(), cUserInfo.getUserId());
|
||||
GSUser gsUser = gsUserDao.findUserInfoQuick(cUserInfo.getServerid(), cUserInfo.getUserId());
|
||||
if (gsUser == null){
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.jmfy.dao;
|
||||
|
||||
import com.jmfy.model.GSUser;
|
||||
import com.jmfy.model.GSUserFinal;
|
||||
import com.jmfy.model.vo.MarkedVo;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
|
||||
|
|
@ -8,7 +9,7 @@ import java.util.List;
|
|||
|
||||
public interface GSUserDao {
|
||||
|
||||
GSUser findUserInfo(int serverId, int userId) throws Exception;
|
||||
GSUserFinal findUserInfoCache(int serverId, int userId) throws Exception;
|
||||
|
||||
GSUser findUserInfoQuick(int serverId, int userId) throws Exception;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,12 +2,11 @@ package com.jmfy.dao.impl;
|
|||
|
||||
import com.jmfy.dao.GSUserDao;
|
||||
import com.jmfy.model.GSUser;
|
||||
import com.jmfy.model.GSUserFinal;
|
||||
import com.jmfy.model.vo.MarkedVo;
|
||||
import com.jmfy.redisProperties.GlobalsDef;
|
||||
import com.jmfy.redisProperties.RedisUserKey;
|
||||
import com.jmfy.utils.Connect;
|
||||
import com.jmfy.utils.MongoName;
|
||||
import com.jmfy.utils.RedisUtil;
|
||||
import com.jmfy.utils.*;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
|
|
@ -15,6 +14,7 @@ import org.springframework.data.mongodb.core.query.Update;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -26,25 +26,32 @@ public class GSUserDaoImpl implements GSUserDao {
|
|||
private Connect connect;
|
||||
public static final String colon = ":";
|
||||
|
||||
public static ExpiryMap<Integer,GSUserFinal> userFinalExpiryMap = new ExpiryMap<>();
|
||||
|
||||
@Override
|
||||
public GSUser findUserInfo(int serverId, int userId) throws Exception {
|
||||
GSUser cUser = RedisUtil.getInstence().getObject(RedisUserKey.CUser_Key + colon + serverId,
|
||||
String.valueOf(userId), GSUser.class, GlobalsDef.REDIS_OVER_FOREVER);
|
||||
public GSUserFinal findUserInfoCache(int serverId, int userId) throws Exception {
|
||||
GSUserFinal cUser = userFinalExpiryMap.get(userId);
|
||||
if (cUser != null) {
|
||||
return cUser;
|
||||
}
|
||||
// 读库
|
||||
String dbName = MongoName.getMongoDBName(serverId);
|
||||
MongoTemplate mongoTemplate = connect.getMongoTemplete(dbName);
|
||||
// 读取数据库
|
||||
MongoTemplate mongoTemplate = connect.getMongoTemplete(MongoName.getMongoDBName(serverId));
|
||||
Query query = new Query(Criteria.where("_id").is(userId));
|
||||
|
||||
GSUser gsUser = mongoTemplate.findOne(query, GSUser.class);
|
||||
// 写入缓存
|
||||
RedisUtil.getInstence().putObject(RedisUserKey.CUser_Key + colon + serverId,
|
||||
String.valueOf(userId), gsUser, GlobalsDef.DAY_TIME);
|
||||
|
||||
return gsUser;
|
||||
if (gsUser == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
// 封装
|
||||
GSUserFinal userFinal = new GSUserFinal();
|
||||
userFinal.setUserId(gsUser.getId());
|
||||
userFinal.setOpenId(gsUser.getPlayerManager().getOpenId());
|
||||
userFinal.setCreateTime(DateUtil.stampToTime(gsUser.getPlayerManager().getCreateTime()));
|
||||
|
||||
// 写入缓存
|
||||
userFinalExpiryMap.put(userId,userFinal,DateUtil.ONE_DAY);
|
||||
return userFinal;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ public class GsGuildDaoImpl implements GsGuildDao {
|
|||
GuildMemberVo vo = new GuildMemberVo();
|
||||
try {
|
||||
// 查找玩家
|
||||
GSUser gsUser = gsUserDao.findUserInfo(beanVo.getServerId(), v2);
|
||||
GSUser gsUser = gsUserDao.findUserInfoQuick(beanVo.getServerId(), v2);
|
||||
if (gsUser != null){
|
||||
// 赋值
|
||||
vo.setUserId(v2);
|
||||
|
|
|
|||
|
|
@ -299,7 +299,7 @@ public class GMHandler extends BaseHandler {
|
|||
// 用户id
|
||||
userBanned.setUserId(cUser.getUserId());
|
||||
// 用户名称
|
||||
GSUser gsUser = gsUserDao.findUserInfo(cUser.getServerid(), cUser.getUserId());
|
||||
GSUser gsUser = gsUserDao.findUserInfoQuick(cUser.getServerid(), cUser.getUserId());
|
||||
if (gsUser != null){
|
||||
userBanned.setUserName(gsUser.getPlayerManager().getNickName());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
|||
|
||||
public class GSPlayerManagerBean {
|
||||
|
||||
private transient String openId;
|
||||
private String openId;
|
||||
|
||||
private int pid;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package com.jmfy.model;
|
||||
|
||||
/**
|
||||
* @Author hj
|
||||
* @Date 2021/10/15 17:49:20
|
||||
* @Description:
|
||||
* @Version 1.0
|
||||
*/
|
||||
public class GSUserFinal {
|
||||
private int userId;
|
||||
private String openId;
|
||||
private String createTime;
|
||||
|
||||
public GSUserFinal(int userId, String openId, String createTime) {
|
||||
this.userId = userId;
|
||||
this.openId = openId;
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public GSUserFinal() {
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getOpenId() {
|
||||
return openId;
|
||||
}
|
||||
|
||||
public void setOpenId(String openId) {
|
||||
this.openId = openId;
|
||||
}
|
||||
|
||||
public String getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(String createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,220 @@
|
|||
package com.jmfy.utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Title: ExpiryMap 可以设置过期时间的Map
|
||||
* @description ExpiryMap继承至HashMap 重写了所有对外的方法,对每个key值都设置了有效期
|
||||
* @Author: xx
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class ExpiryMap<K, V> extends HashMap<K, V> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* default expiry time 2s
|
||||
*/
|
||||
private long EXPIRY = 1000 * 2;
|
||||
|
||||
private HashMap<K, Long> expiryMap = new HashMap<>();
|
||||
|
||||
/** 缓存实例对象 */
|
||||
private volatile static ExpiryMap<String, String> SameUrlMap;
|
||||
|
||||
/**
|
||||
* 采用单例模式获取实例
|
||||
* @return
|
||||
*/
|
||||
public static ExpiryMap getInstance() {
|
||||
//第一次判空,提高效率
|
||||
if (null == SameUrlMap) {
|
||||
//保证线程安全
|
||||
synchronized (ExpiryMap.class) {
|
||||
//第二次判空,保证单例对象的唯一性,防止第一次有多个线程进入第一个if判断
|
||||
if (null == SameUrlMap) {
|
||||
SameUrlMap = new ExpiryMap<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
return SameUrlMap;
|
||||
}
|
||||
|
||||
public ExpiryMap(){
|
||||
super();
|
||||
}
|
||||
|
||||
public ExpiryMap(long defaultExpiryTime){
|
||||
this(1 << 4, defaultExpiryTime);
|
||||
}
|
||||
|
||||
public ExpiryMap(int initialCapacity, long defaultExpiryTime){
|
||||
super(initialCapacity);
|
||||
this.EXPIRY = defaultExpiryTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
expiryMap.put(key, System.currentTimeMillis() + EXPIRY);
|
||||
return super.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return !checkExpiry(key, true) && super.containsKey(key);
|
||||
}
|
||||
/**
|
||||
* @param key
|
||||
* @param value
|
||||
* @param expiryTime 键值对有效期 毫秒
|
||||
* @return
|
||||
*/
|
||||
public V put(K key, V value, long expiryTime) {
|
||||
expiryMap.put(key, System.currentTimeMillis() + expiryTime);
|
||||
return super.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return entrySet().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return entrySet().size() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
if (value == null) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
Set<Entry<K, V>> set = super.entrySet();
|
||||
Iterator<Entry<K, V>> iterator = set.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
java.util.Map.Entry<K, V> entry = iterator.next();
|
||||
if(value.equals(entry.getValue())){
|
||||
if(checkExpiry(entry.getKey(), false)) {
|
||||
iterator.remove();
|
||||
return Boolean.FALSE;
|
||||
}else {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
|
||||
Collection<V> values = super.values();
|
||||
|
||||
if(values == null || values.size() < 1) {
|
||||
return values;
|
||||
}
|
||||
|
||||
Iterator<V> iterator = values.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
V next = iterator.next();
|
||||
if(!containsValue(next)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
if(checkExpiry(key, true)) {
|
||||
return null;
|
||||
}
|
||||
return super.get(key);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @Description: 是否过期
|
||||
* @param key
|
||||
* @return null:不存在或key为null -1:过期 存在且没过期返回value 因为过期的不是实时删除,所以稍微有点作用
|
||||
*/
|
||||
public Object isInvalid(Object key) {
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
if(!expiryMap.containsKey(key)){
|
||||
return null;
|
||||
}
|
||||
long expiryTime = expiryMap.get(key);
|
||||
|
||||
boolean flag = System.currentTimeMillis() > expiryTime;
|
||||
|
||||
if(flag){
|
||||
super.remove(key);
|
||||
expiryMap.remove(key);
|
||||
return -1;
|
||||
}
|
||||
return super.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends K, ? extends V> m) {
|
||||
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
|
||||
expiryMap.put(e.getKey(), System.currentTimeMillis() + EXPIRY);
|
||||
}
|
||||
super.putAll(m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Map.Entry<K,V>> entrySet() {
|
||||
Set<java.util.Map.Entry<K, V>> set = super.entrySet();
|
||||
Iterator<java.util.Map.Entry<K, V>> iterator = set.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
java.util.Map.Entry<K, V> entry = iterator.next();
|
||||
if(checkExpiry(entry.getKey(), false)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @Description: 是否过期
|
||||
* @param expiryTime true 过期
|
||||
* @param isRemoveSuper true super删除
|
||||
* @return
|
||||
*/
|
||||
private boolean checkExpiry(Object key, boolean isRemoveSuper){
|
||||
|
||||
if(!expiryMap.containsKey(key)){
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
long expiryTime = expiryMap.get(key);
|
||||
|
||||
boolean flag = System.currentTimeMillis() > expiryTime;
|
||||
|
||||
if(flag){
|
||||
if(isRemoveSuper) {
|
||||
super.remove(key);
|
||||
}
|
||||
expiryMap.remove(key);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ExpiryMap<String, String> map = new ExpiryMap<>();
|
||||
map.put("test", "xxx");
|
||||
map.put("test2", "ankang", 5000);
|
||||
System.out.println("test==" + map.get("test"));
|
||||
Thread.sleep(3000);
|
||||
System.out.println("test==" + map.get("test"));
|
||||
System.out.println("test2==" + map.get("test2"));
|
||||
Thread.sleep(3000);
|
||||
System.out.println("test2==" + map.get("test2"));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue