加速Java中的链接替换

时间:2015-01-16 09:07:32

标签: java jsoup spring-data mongodb-java

我试图用我生成的自定义链接替换html内容中的链接。我尝试了这种方法和正则表达式替换。正则表达式的替换甚至更慢。

此方法在我的Spring MVC应用程序中执行大约需要500毫秒。 linkRepository是一个MongoRepository。使用spring-data-mongodb。

有谁知道如何加快速度?

    Document doc = Jsoup.parse(response.getContent());
    Elements tags = doc.select("a[href]");
    for(Element tag : tags) {
        String url = tag.attr("href");
        Link link = new Link();
        link.setUrl(url);
        link.setDelivery(d);
        linkRepository.save(link);
        url = linkTo(methodOn(TrackController.class).actionTrack(link.getId(), d.getId(), null)).toUri().toString();
        tag.attr("href", url);
    }
    String content = doc.outerHtml();

更新

我还尝试一次保存所有链接,而不是将它们保存在循环中并使用一些生成的ID而不是mongodb ID。那没有用。

更新2:

我添加了一些更多的分析,似乎是MongoDB需要半秒才能保存。

DEBUG foo.bar.serving.filter.TrackingUrlFilter: Parsing HTML Content took 0:00:00.003
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Select links took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.044
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.032
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.033
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.031
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.032
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.033
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.033
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.002
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.033
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.002
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.039
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.032
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.002
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.033
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Saving link took 0:00:00.036
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating link took 0:00:00.001
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Setting link took 0:00:00.000
DEBUG foo.bar.serving.filter.TrackingUrlFilter: Generating html took 0:00:00.001

有人知道如何加快速度吗?

更新3:

经过一些更多的fiddeling后,我尝试了一些MongoDB选项。将MongoTemplate的writeConcern属性设置为UNACKNOWLEDGED会将每次插入的时间降低到1-3ms。是否应该忽略MongoDB所说的任何解决方案?

0 个答案:

没有答案