Java网站下载Cache API

发布于 2024-12-08 04:10:04 字数 154 浏览 0 评论 0原文

我在我的 java 应用程序中使用 2 个模块来下载相同的网页。 因此,实际上该网站被下载了两次。 为了避免这种情况,我是否可以附加一些缓存层,以便实际只下载该网站的 1 个副本。

我希望看到 Java 端的缓存,如果不能在更高的级别(例如某些 Web 缓存代理或其他东西)实现

I am using 2 modules in my java application which downloads the same web page.
So in effect the site is downloaded twice.
In order to avoid this , is there some caching layer i can attach , so that only 1 copy of the site is actually downloaded.

I would love to see the caching at Java side , if not possible at a later level like some web caching proxy or something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-12-15 04:10:04

如果两个“模块”位于同一个 jvm 中,因此可以访问彼此的内存,请尝试基于单例的“缓存”。我倾向于使用 HtmlSnippit 缓存来缓存多次重复的 HTML 片段,并取得了巨大成功:

public class Testing {
    public static void main(String[] args) {

    String html = "<div>The quick brown fox jumps over the lazy dog</div>";

    /* Access via the getInstance() getter */
    HtmlSnippitCache.getInstance().putSnippit("FOXY", html);

    /* Or via local var */
    HtmlSnippitCache cache = HtmlSnippitCache.getInstance();
    String moreHtml = cache.getSnippit("FOXY");

    System.out.println(moreHtml);
}

public static class HtmlSnippitCache {
    /* Singleton implementation */
    private static HtmlSnippitCache instance;

    public static HtmlSnippitCache getInstance() {
        if (HtmlSnippitCache.instance == null)
            synchronized (HtmlSnippitCache.class) {
                if (HtmlSnippitCache.instance == null)
                    HtmlSnippitCache.instance = new HtmlSnippitCache();
            }
        return HtmlSnippitCache.instance;
    }

    /* Ensure only local construction. */
    private HtmlSnippitCache() {}

    /* Clas Impl */
    private HashMap<String, String> map = new HashMap<String, String>();

    public boolean containsSnippit(String key) {
        return map.containsKey(key);
    }

    public String getSnippit(String key) {
        return map.get(key);
    }

    public String putSnippit(String key, String value) {
        return map.put(key, value);
    }

        public int size() {
            return map.size();
        }
    }
}

现在, getSnippit()putSnippit() 方法可能需要是 出于某种原因,synchronized 是为了线程安全,但这完全是另一个讨论(争论?):)

(示例应该开箱即用。)

If the two 'modules' are in the same jvm and thus can access eachothers memory, try a singleton based 'cache'. I tend to use a HtmlSnippit cache for caching much repeated HTML snippits with great success:

public class Testing {
    public static void main(String[] args) {

    String html = "<div>The quick brown fox jumps over the lazy dog</div>";

    /* Access via the getInstance() getter */
    HtmlSnippitCache.getInstance().putSnippit("FOXY", html);

    /* Or via local var */
    HtmlSnippitCache cache = HtmlSnippitCache.getInstance();
    String moreHtml = cache.getSnippit("FOXY");

    System.out.println(moreHtml);
}

public static class HtmlSnippitCache {
    /* Singleton implementation */
    private static HtmlSnippitCache instance;

    public static HtmlSnippitCache getInstance() {
        if (HtmlSnippitCache.instance == null)
            synchronized (HtmlSnippitCache.class) {
                if (HtmlSnippitCache.instance == null)
                    HtmlSnippitCache.instance = new HtmlSnippitCache();
            }
        return HtmlSnippitCache.instance;
    }

    /* Ensure only local construction. */
    private HtmlSnippitCache() {}

    /* Clas Impl */
    private HashMap<String, String> map = new HashMap<String, String>();

    public boolean containsSnippit(String key) {
        return map.containsKey(key);
    }

    public String getSnippit(String key) {
        return map.get(key);
    }

    public String putSnippit(String key, String value) {
        return map.put(key, value);
    }

        public int size() {
            return map.size();
        }
    }
}

Now, the getSnippit() and putSnippit() methods will probably need to be synchronized somehow for thread safety, but that's another discussion (argument?) altogether :)

(Example should run out of the box.)

软甜啾 2024-12-15 04:10:04

这可能有点偏离主题,因为此解决方案使用 spring:

您可以使用 ehcache-spring -annotations 无需任何代码即可进行缓存。

本质上,您可以在 ehcache.xml 中定义一个缓存:

<ehcache>
    ...
    <cache 
       name="htmlCache" 
       maxElementsInMemory="10" 
       eternal="true" 
       overflowToDisk="false" />
</ehcache>

配置为在 Spring 应用程序上下文中使用缓存注释:

<ehcache:annotation-driven />

<ehcache:config cache-manager="cacheManager"/>

<bean 
    id="cacheManager"    
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

并注释您的代码以自动缓存:

@Cacheable(cacheName = "htmlCache")
public String getHtml(String url) {
    ...
}

然后这将缓存 getHtml 方法的结果基于其参数 (url),因此当使用相同的 url 第二次调用该方法时,结果将直接来自缓存。

This might be slightly off-topic as this solution uses spring:

You could use the ehcache-spring-annotations to do the caching without any code.

Essentially, you'd define a cache in ehcache.xml:

<ehcache>
    ...
    <cache 
       name="htmlCache" 
       maxElementsInMemory="10" 
       eternal="true" 
       overflowToDisk="false" />
</ehcache>

Configure to use the cache annotations in your spring application context:

<ehcache:annotation-driven />

<ehcache:config cache-manager="cacheManager"/>

<bean 
    id="cacheManager"    
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

And annotate your code to automatically cache:

@Cacheable(cacheName = "htmlCache")
public String getHtml(String url) {
    ...
}

This would then cache the results of the getHtml method based on its parameter (url), so on when the method is called the second time with the same url, the results would come straight from the cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文