好友推荐优化
parent
fc0c3b1250
commit
bedc8a1c0a
|
|
@ -38,9 +38,6 @@ public class LjsdMongoTemplate implements MongoUpdateImp {
|
|||
|
||||
//注释
|
||||
public <T> void save(int uid,T obj) throws Exception {
|
||||
// MongoRoot root = (MongoRoot)obj;
|
||||
// mongoTemplate.save(obj,root.getCollection());
|
||||
// root.setBsave(true);
|
||||
int serverIdByUid = AreaManager.getInstance().getServerIdByUid(uid);
|
||||
MongoTemplate otherMonogTemplate = getMongoTemplate(serverIdByUid);
|
||||
if (otherMonogTemplate == null) {
|
||||
|
|
@ -75,14 +72,8 @@ public class LjsdMongoTemplate implements MongoUpdateImp {
|
|||
if (mongoTemplate != null) {
|
||||
return mongoTemplate.findAll(clazz);
|
||||
}
|
||||
|
||||
// for (MongoTemplate mongoTemplate : MongoUtil.mongoTemplateMap.values()) {
|
||||
// List<T> all = mongoTemplate.findAll(clazz);
|
||||
// list.addAll(all);
|
||||
// }
|
||||
}
|
||||
return list;
|
||||
//return mongoTemplate.findAll(clazz,collectionName);
|
||||
}
|
||||
|
||||
public <T> List<T> findAllByCondition(Query query, Class<T> entityClass) {
|
||||
|
|
@ -149,7 +140,6 @@ public class LjsdMongoTemplate implements MongoUpdateImp {
|
|||
return otherMonogTemplate.findById(id,clazz,collectionName);
|
||||
}
|
||||
|
||||
|
||||
public <T> T findById(int id, Class<T> clazz) throws Exception {
|
||||
int serverIdByUid ;
|
||||
if(clazz.equals(GlobalSystemControl.class)||clazz.equals(ServerConfig.class)){
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ import com.ljsd.jieling.logic.dao.root.User;
|
|||
import com.ljsd.jieling.util.ConfigurableApplicationContextManager;
|
||||
import com.ljsd.jieling.util.InnerMessageUtil;
|
||||
import com.ljsd.jieling.util.SysUtil;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.MongoClient;
|
||||
import org.bson.Document;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.annotation.Transient;
|
||||
|
|
@ -86,22 +88,37 @@ public class MongoUtil {
|
|||
}
|
||||
|
||||
//todo 慢查 需要修改
|
||||
public List<User> getRandomUsers(int size, int uid, int level) throws Exception {
|
||||
public List<User> getRandomUsers(int size, int uid, int level) {
|
||||
// 验证size参数的有效性
|
||||
if (size <= 0) {
|
||||
return null;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
int randomLevel = MathUtils.random(5, 20);
|
||||
Criteria criatira = new Criteria();
|
||||
int minLevel = level - randomLevel;
|
||||
minLevel = Math.max(minLevel, 5);
|
||||
int maxLevel = level + randomLevel;
|
||||
long befor3Day = System.currentTimeMillis() - BEFOR_3_DAY;
|
||||
|
||||
criatira.andOperator(Criteria.where("id").nin(uid),//不包含自己
|
||||
Criteria.where("playerManager.level").gte(minLevel), Criteria.where("playerManager.level").lte(maxLevel),
|
||||
Criteria.where("playerManager.loginTime").gte(befor3Day));
|
||||
Query query = new Query(criatira).limit(size * 2 + 1);
|
||||
// 随机等级差异的计算
|
||||
int randomLevel = MathUtils.random(5, 20);
|
||||
int minLevel = Math.max(level - randomLevel, 5);
|
||||
int maxLevel = level + randomLevel;
|
||||
|
||||
// 计算三天前的时间点
|
||||
long threeDaysAgo = System.currentTimeMillis() - BEFOR_3_DAY;
|
||||
|
||||
// 构建查询条件
|
||||
Criteria criteria = Criteria.where("id").nin(uid)
|
||||
.and("playerManager.level").gte(minLevel).lte(maxLevel)
|
||||
.and("playerManager.loginTime").gte(threeDaysAgo);
|
||||
|
||||
// 创建查询对象,并设置限制条件
|
||||
int length = size * 2 + 1;
|
||||
Query query = Query.query(criteria).limit(length);
|
||||
|
||||
// 限定返回的字段,只返回需要的字段可以提高效率
|
||||
query.fields().include("playerManager");
|
||||
|
||||
// MongoTemplate mongoTemplate = mongoTemplateMap.get(GameApplication.serverId);
|
||||
// DBObject explainResult = mongoTemplate.getCollection("user").find(query.getQueryObject()).explain();
|
||||
// LOGGER.info("随机玩家mongo检查:{}",explainResult.toString());
|
||||
|
||||
// 执行查询
|
||||
return this.myMongoTemplate.findAllByCondition(query, User.class);
|
||||
}
|
||||
|
||||
|
|
@ -266,7 +283,7 @@ public class MongoUtil {
|
|||
mongoTemplate = new MongoTemplate(mongoDbFactory, mappingMongoConverter);
|
||||
mongoTemplateMap.put(serverId,mongoTemplate);
|
||||
}
|
||||
return mongoTemplate;
|
||||
return mongoTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@ import com.ljsd.jieling.handler.map.MapManager;
|
|||
import com.ljsd.jieling.jbean.ActivityManager;
|
||||
import com.ljsd.jieling.jbean.gm.GmActivityManager;
|
||||
import com.ljsd.jieling.logic.dao.*;
|
||||
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document
|
||||
@CompoundIndex(def = "{'playerManager.level' : 1, 'playerManager.loginTime': 1}", name = "level_loginTime_idx")
|
||||
public class User {
|
||||
|
||||
public static final String _COLLECTION_NAME = "user";
|
||||
|
|
|
|||
Loading…
Reference in New Issue