我注意到我的实现中的Spring数据redis findAll存在性能问题。花费的时间太长了。(500条记录就像7-8一样)秒我做错了什么或者我能做些什么来让它更快?
我最初的怀疑是嵌套类(MyObject中的ReliableProperties)
我的测试用例:
watch.start("selecting with 50 input");
List keys = new ArrayList<>();
for(int i=0;i<50;i++){
keys.add("101:RATE:20000"+i);
}
objectIte = objectRepo.findAll(keys);
watch.stop();
结果如下:
-----------------------------------------
millisecs % Task name
-----------------------------------------
00258 002% selecting with 5 input
00901 008% selecting with 50 input
01742 016% selecting with 100 input
07746 073% selecting with 500 input
我的回购:
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.redis.entity.object.MyObject;
@Repository
public interface MyObjectRepository extends CrudRepository<MyObject,String>
{
}
我的对象: 1. MyObject.java
@Data
@NoArgsConstructor
@RedisHash(MyRedisNS.MY_OBJECT)
public class MyObject implements Serializable {
private static final long serialVersionUID = 550571757049326776L;
public static final String REDIS_HASH = MyRedisNS.MY_OBJECT;
@Id
private String objectKey;
@Indexed
private String locationId;
private MyObjectProperties properties;
public <MyObject(String locationId, String objectId, String objectType, MyObjectProperties properties) {
this.objectKey = getKey(locationId,objectType,objectId);
this.properties = properties;
}
private static String coalesceSubRateToRate(String objectType) {
if (NodeType.SUBRATE.name().equalsIgnoreCase(objectType)) {
return NodeType.RATE.name();
}
if (NodeType.SUBRAW.name().equalsIgnoreCase(objectType)) {
return NodeType.RAW.name();
}
return objectType;
}
public static String getKey(String locationId, String objectType, String objectId) {
return locationId + ":" + coalesceSubRateToRate(objectType) + ":" + objectId;
}
public static MyObjectKey getMySObjectKey(String key) {
String [] keys = key.split(":");
return new MyObjectKey(keys[0],keys[2],keys[1]);
}
public MyObjectKey getKeyClass(){
return <MyObject.getMyObjectKey(this.getObjectKey());
}
}
MyProperties
public interface MyObjectProperties {
String getId();
String getType();
default NodeType getNodeType() {
NodeType nodeTypeFromName = NodeType.fromName(getType());
NodeType nodeTypeFromValue = NodeType.fromValue(getType());
if (Objects.nonNull(nodeTypeFromName)) {
return nodeTypeFromName;
}
if (Objects.nonNull(nodeTypeFromValue)) {
return nodeTypeFromValue;
}
return null;
}
String getLabel();
boolean isActivated();
Integer getStatus();
}
ReliableProperties
@Data
@NoArgsConstructor
public class ReliableProperties implements MyObjectProperties,Serializable {
private static final long serialVersionUID = 3670401189368428805L;
@NonNull
@SerializedName("status")
protected Integer status;
@SerializedName("functionName")
protected String fnName;
@SerializedName("isSubRate")
protected Boolean isSubRate = false;
@SerializedName("rateId")
private String id;
@SerializedName("rateLabel")
private String label;
@SerializedName("type")
private String type = NodeType.RATE.name();
@SerializedName("activated")
private boolean activated;
@SerializedName("isOnHoliday")
private Boolean isOnHoliday;
@SerializedName("minSourcesRequired")
private Integer minSource;
@SerializedName("prioritySourceNum")
private Integer prioritySourceNum;
@SerializedName("priorityType")
private PriorityTypeEnum priorityType;
@SerializedName("benchMarkIsIn")
private String benchmarkIsIn;
@SerializedName("lastPriorityUsed")
private String lastPriorityUsed;
@Builder
public ReliableProperties(Integer status, String id, String label, String fnName, Boolean isOnHoliday, Integer minSource, Integer
prioritySourceNum, PriorityTypeEnum priorityType, String benchmarkIsIn, String lastPriorityUsed, boolean activated) {
this.status = status;
this.id = id;
this.label = label;
this.fnName = fnName;
this.isOnHoliday = isOnHoliday;
this.minSource = minSource;
this.prioritySourceNum = prioritySourceNum;
this.priorityType = priorityType;
this.benchmarkIsIn = benchmarkIsIn;
this.lastPriorityUsed = lastPriorityUsed;
this.activated = activated;
}
public boolean isMinSourceEnabled() {
return this.minSource != null && this.minSource > 0;
}
public boolean isPrioritySourceEnabled() {
return this.prioritySourceNum != null && this.priorityType != null && this.prioritySourceNum > 0;
}
public boolean isCalculationEnabled() {
return this.activated && this.isOnHoliday != null && !this.isOnHoliday;
}
}