我在一个正在开发的表单中有几个自动补全字段。最大的包含近20k的记录,最小的包含大约1k的记录。到目前为止,我只使用了一个TreeMap来处理这个任务,但是我发现它的效率非常低。我现在的结构是这样的。
private SortedMap<String, Set<String>> cache;
public AutocompleteCacheServiceImpl() {
cache = Collections.synchronizedSortedMap(new TreeMap<String, Set<String>>());
}当像这样填充的时候,
private void populateCache(String id, String name) {
int len = name.length();
for (int i = 1; i <= len; i++) {
String key = name.substring(0, i).toLowerCase();
if(this.cache.containsKey(key)) {
Set<String> exist = cache.get(key);
if(!exist.contains(id)) {
exist.add(id);
}
} else {
Set<String> _e = new HashSet<String>();
_e.add(id);
this.cache.put(key, _e);
}
}
}输出1小时1户1户1户1户1户
我希望用Ehcache之类的东西替换我的缓存实现,但我对它并不是很熟悉。我想知道是否有人有任何建议来设置这样的东西,这样击键的响应时间将保持在500ms或更短。
我看过这个页面http://ehcache.org/documentation/get-started/getting-started
但也许我目前的填充方法导致我忽略了一种更好的方法。
有谁有什么想法吗?
发布于 2012-06-06 23:39:41
如果您希望优化性能,可以看看patricia tries,它有一个实现SortedMap的实现here。
https://stackoverflow.com/questions/10917393
复制相似问题