【问题标题】:Java Web site download Cache APIJava网站下载缓存API
【发布时间】:2011-10-04 10:59:09
【问题描述】:

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

我很想看到 Java 端的缓存,如果在以后的级别上不可能的话,比如一些 Web 缓存代理或其他东西

【问题讨论】:

  • 当您说“模块”时,您指的是两个独立的可执行文件,还是同一个可执行文件中的两个不同代码区域?
  • 我说的是同一个可执行文件中的 2 个不同的代码区域。

标签: java web-applications web browser-cache


【解决方案1】:

如果两个“模块”在同一个 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,但这完全是另一个讨论(争论?):)

(示例应该立即可用。)

【讨论】:

    【解决方案2】:

    这可能有点离题,因为此解决方案使用弹簧:

    您可以使用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) {
        ...
    }
    

    然后,这将根据其参数 (url) 缓存 getHtml 方法的结果,以此类推,当第二次使用相同的 url 调用该方法时,结果将直接来自缓存.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2015-08-06
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      相关资源
      最近更新 更多