[英]Is there a good pattern for reusing Morphia queries?
I've just been profiling some code where I increment some frequency counters with the following code: 我刚刚分析了一些代码,我用以下代码增加了一些频率计数器:
Datastore ds = ...
final Query<Unit> query = ds.createQuery(Unit.class);
query.field("text").equal(text);
query.field("langCode").equal(lang.getCode());
UpdateOperations ops = ds.createUpdateOperations(Unit.class);
ops.inc("frequency", value);
ds.update(query, ops, false);
The creation of the query takes almost 50% of the execution time, and I'd like to reuse the work somehow. 查询的创建花费了近50%的执行时间,我想以某种方式重用该工作。 Is it safe to save the
query
and ops
objects in a ThreadLocal and just call query.field("text").equal(text)
again to replace the "text" field? 将
query
和ops
对象保存在ThreadLocal中是否安全,只需再次调用query.field("text").equal(text)
来替换“text”字段? It also looks like validation is taking up some 30% of the total time. 它看起来似乎验证占用了总时间的约30%。
Yes, you can do that. 是的,你可以这么做。 Each field is stored in a map so it will replace it when called multiple times.
每个字段都存储在一个映射中,因此在多次调用时它将替换它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.