用服务器ip做网站,免费网站建设程序,鞍山吧最新消息,制作视频的软件手机即将发布的Spring 3.1版本中引入的新功能之一是缓存抽象之一 。 Spring Framework提供了对将缓存透明添加到现有Spring应用程序中的支持。 与事务支持类似#xff0c;缓存抽象允许一致使用各种缓存解决方案#xff0c;而对代码的影响最小。 从本质上讲#xff0c;抽象将缓存… 即将发布的Spring 3.1版本中引入的新功能之一是缓存抽象之一 。 Spring Framework提供了对将缓存透明添加到现有Spring应用程序中的支持。 与事务支持类似缓存抽象允许一致使用各种缓存解决方案而对代码的影响最小。 从本质上讲抽象将缓存应用于Java方法从而基于缓存中可用的信息减少了执行次数。 也就是说每次调用目标方法时抽象将应用缓存行为以检查该方法是否已针对给定参数执行。 如果有则返回缓存的结果而不必执行实际的方法 如果还没有则执行该方法将结果缓存并返回给用户以便下次调用该方法时返回缓存的结果。 这个概念当然不是什么新鲜事物。 您可以查看SpringAspectJEhcache方法的缓存Aspect是我们JCG合作伙伴之一Brian Du Preez撰写的一篇非常有趣的文章其中使用了Aspect Oriented Programming 。 顾名思义“缓存抽象”不是实际的实现因此它需要使用实际的存储来存储缓存数据。 您可能已经猜到了开箱即用地提供了Ehcache支持。 还有一个基于JDK的ConcurrentMap的实现您实际上可以插入不同的后端缓存 。 现在让我们看一些有关缓存抽象的示例代码。 为此我将使用JCG的另一位合作伙伴 James Carr 在Spring 3.1.0.M1中提供的非常有用的Cache Abstraction 。 确保在此过程中将Spring Cache包Javadocs标记为书签。 注意对原始帖子进行了少量编辑以提高可读性 昨天发布的另一个新功能与我同时尝试了一些基于注释的缓存策略。 缓存抽象基本上是从现有项目中获取约定并使其成为Spring核心的一部分。 本质上它引入了一个新接口CacheManager 可以通过特定的缓存实现来实现。 从那里开始它添加了一些新的注释以使方法可缓存。 这是一个使用我以前的posts对象的示例。 package com.jamescarr.example;import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;import javax.annotation.PostConstruct;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;Repository
public class MemoryMessageRepository implements MessageRepository {private static final Logger LOG LoggerFactory.getLogger(MemoryMessageRepository.class);private final MapString, Message messages new ConcurrentHashMapString, Message();Cacheable(message)public Message getMessage(String title){LOG.info(Fetching message);return messages.get(title);}CacheEvict(valuemessage, keymessage.title)public void save(Message message){LOG.info(Saving message);messages.put(message.getTitle(), message);}public CollectionMessage findAll() {return messages.values();}PostConstructpublic void addSomeDefaultMessages(){save(new Message(Hello, Hello World));save(new Message(Appointment, Remember the milk!));}} 在这里您会发现finder方法具有Cachable批注其名称指定了要存储到的缓存。 它还可以使用其他属性例如使用表达式语言从传入的参数确定键的键。默认值为所有方法参数的值。 在保存方法上我使用CacheEvict从缓存中删除已缓存的元素如果已存在。 当然这本身并不会起作用因此您必须自己启用它这很好……您需要做的最后一件事是发现一个生产应用程序将不需要缓存的内容缓存起来。 不幸的是在撰写本文时我还没有发现如何在非xml中执行此操作因此这是启用它并使用ehcache作为实现的spring xml文件。 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:cachehttp://www.springframework.org/schema/cachexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsdcache:annotation-driven /bean idcacheManager classorg.springframework.cache.ehcache.EhCacheCacheManager p:cache-manager-refehcache/bean idehcache classorg.springframework.cache.ehcache.EhCacheManagerFactoryBean p:config-locationclasspath:com/jamescarr/example/ehcache.xml//beans ehcache配置 ehcachediskStore pathjava.io.tmpdir/cache namemessagemaxElementsInMemory100eternalfalsetimeToIdleSeconds120timeToLiveSeconds120overflowToDisktruemaxElementsOnDisk10000000diskPersistentfalsediskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRU//ehcache 最后将其添加到AppConfiguration中其中包括执行一个简单的ImportResource 。 package com.jamescarr.configuration;
import javax.annotation.PostConstruct;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;import com.jamescarr.example.MessagePrinter;Configuration
ComponentScan(com.jamescarr.example)
ImportResource(classpath:com/jamescarr/example/cache-context.xml)
public class AppConfig {Autowiredprivate MessagePrinter messagePrinter;PostConstructpublic void doSomething(){messagePrinter.printMessage(Hello);messagePrinter.printMessage(Hello);}public static void main(String[] args) {new AnnotationConfigApplicationContext(AppConfig.class);}
} 在运行此示例时第一次单击该方法时应该有一条日志消息然后第二次则看不到因为它是从缓存中拉出的。对于为可能只是为了实现该方法的方法实现备忘录化而言 这绝对是很棒的选择进行一些CPU密集型计算但是在一组输入的情况下给出了确切的预期结果。我为在该领域进行更多工作而感到兴奋……我之前已经做过方法级缓存这很常见但是无需DIY就可以使用它。 就是这样。 一个简单的指南可帮助您开始使用James Carr的 Spring的Cache抽象 。 别忘了分享 相关文章 使用Spring AOP进行面向方面的编程 带有Spring和Maven教程的JAX–WS 使用Spring使用Java发送电子邮件– GMail SMTP服务器示例 使用Spring AspectJ和Maven进行面向方面的编程 翻译自: https://www.javacodegeeks.com/2011/02/spring-31-cache-abstraction-tutorial.html