Spring Cache抽象-使用Java类注解的方式整合EhCache

简介: Spring Cache抽象-使用Java类注解的方式整合EhCache

概述


Spring Cache抽象-之缓存注解这篇博文中我们介绍了SpringCache抽象注解的使用方式


既然这是一个抽象,我们需要一个具体的缓存存储实现。比价流行的有:基于JDK java.util.concurrent.ConcurrentMap的缓存,EhCache,Gemfire缓存,Caffeine,Guava缓存和兼容JSR-107的缓存等等。这里我们使用Ehcache来实现这个缓存。


同时,我们使用EhCacheManagerFactoryBean的configLocation属性指定Ehcache的设置。如果未明确指定,则默认为ehcache.xml。


工程结构


20171004084326746.jpg


以及EhCache的配置文件:


20171004084352785.jpg

pom.xml 关键的依赖

<properties>            <springframework.version>4.3.9.RELEASE</springframework.version>
 <ehcache.version>2.10.4</ehcache.version>       
</properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- EHCache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
           <version>${ehcache.version}</version>
        </dependency>
        <!-- SLF4J/Logback -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>     
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="true"
    monitoring="autodetect" 
    dynamicConfig="true">
    <diskStore path="java.io.tmpdir" />
    <cache name="products" 
        maxEntriesLocalHeap="100"
        maxEntriesLocalDisk="1000" 
        eternal="false" 
        timeToIdleSeconds="300" 
        timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU" 
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>


我们设置一个名为’products’的缓存。


最多100个products将保存在内存[堆叠]存储中,


最多1000个products将被保留在DiskStore中


指定的路径为“java.io.tmpdir”,它指的是默认的临时文件路径。


如果product闲置超过5分钟,寿命超过10分钟,products缓存将会过期


实体类

package com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain;
import java.io.Serializable;
public class Product implements Serializable {
    private static final long serialVersionUID = 123L;
    private String name;
    private double price;
    /**
     * 
     * 
     * @Title:Product
     * 
     * @Description:构造函数
     * 
     * @param name
     * @param price
     */
    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}


Product接口

package com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.service;
import com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product;
public interface ProductService {
    Product getByName(String name);
    Product updateProduct(Product product);
    void refreshAllProducts();
}


接口实现类 及缓存配置

package com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.service;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product;
/**
 * 
 * 
 * @ClassName: ProductServiceImpl
 * 
 * @Description:@Service标注的服务层
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年10月3日 下午5:22:30
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {
    private static final Logger logger = Logger.getLogger(ProductServiceImpl.class);
    private static List<Product> products;
    static {
        products = getDummyProducts();
    }
    @Cacheable(cacheNames = "products", key = "#name", condition = "#name != 'HTC'", unless = "#result==null")
    @Override
    public Product getByName(String name) {
        logger.info("<!----------Entering getByName()--------------------->");
        for (Product product : products) {
            if (product.getName().equalsIgnoreCase(name)) {
                return product;
            }
        }
        return null;
    }
    @CachePut(cacheNames = "products", key = "#product.name", unless = "#result==null")
    @Override
    public Product updateProduct(Product product) {
        logger.info("<!----------Entering updateProduct()--------------------->");
        for (Product p : products) {
            if (p.getName().equalsIgnoreCase(product.getName())) {
                p.setPrice(product.getPrice());
                return p;
            }
        }
        return null;
    }
    @CacheEvict(cacheNames = "products", allEntries = true)
    @Override
    public void refreshAllProducts() {
    }
    private static List<Product> getDummyProducts() {
        products = new ArrayList<Product>();
        products.add(new Product("IPhone", 500));
        products.add(new Product("Samsung", 600));
        products.add(new Product("HTC", 800));
        return products;
    }
}


关键配置类 ,以及加载enhance

package com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.configuration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@EnableCaching
@Configuration
@ComponentScan(basePackages = "com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache")
public class AppConfig {
    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }
    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
        factory.setConfigLocation(new ClassPathResource("ehcache/ehcache.xml"));
        factory.setShared(true);
        return factory;
    }
}


单元测试

package com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.configuration.AppConfig;
import com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product;
import com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.service.ProductService;
public class SpringCacheWithEhCacheTest {
    private static final Logger logger = Logger
            .getLogger(SpringCacheWithEhCacheTest.class);
    AbstractApplicationContext context = null;
    @Before
    public void initContext() {
        context = new AnnotationConfigApplicationContext(AppConfig.class);
    }
    @Test
    public void test() {
        ProductService service = (ProductService) context
                .getBean("productService");
        logger.info("IPhone ->" + service.getByName("IPhone"));
        logger.info("IPhone ->" + service.getByName("IPhone"));
        logger.info("IPhone ->" + service.getByName("IPhone"));
        logger.info("HTC ->" + service.getByName("HTC"));
        logger.info("HTC ->" + service.getByName("HTC"));
        logger.info("HTC ->" + service.getByName("HTC"));
        Product product = new Product("IPhone", 550);
        service.updateProduct(product);
        logger.info("IPhone ->" + service.getByName("IPhone"));
        logger.info("IPhone ->" + service.getByName("IPhone"));
        logger.info("IPhone ->" + service.getByName("IPhone"));
        logger.info("Refreshing all products");
        service.refreshAllProducts();
        logger.info("IPhone [after refresh]->" + service.getByName("IPhone"));
        logger.info("IPhone [after refresh]->" + service.getByName("IPhone"));
        logger.info("IPhone [after refresh]->" + service.getByName("IPhone"));
    }
    @After
    public void releaseContext() {
        ((AbstractApplicationContext) context).close();
    }
}


输出结果分析

2017-10-03 20:54:55,026  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7bd1a567: startup date [Tue Oct 03 20:54:55 BOT 2017]; root of context hierarchy
2017-10-03 20:54:55,858  INFO [main] (EhCacheManagerFactoryBean.java:130) - Initializing EhCache CacheManager
2017-10-03 20:54:56,711  INFO [main] (ProductServiceImpl.java:40) - <!----------Entering getByName()--------------------->
2017-10-03 20:54:56,715  INFO [main] (SpringCacheWithEhCacheTest.java:32) - IPhone ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,716  INFO [main] (SpringCacheWithEhCacheTest.java:33) - IPhone ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,716  INFO [main] (SpringCacheWithEhCacheTest.java:34) - IPhone ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,717  INFO [main] (ProductServiceImpl.java:40) - <!----------Entering getByName()--------------------->
2017-10-03 20:54:56,717  INFO [main] (SpringCacheWithEhCacheTest.java:36) - HTC ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@68c06cac
2017-10-03 20:54:56,717  INFO [main] (ProductServiceImpl.java:40) - <!----------Entering getByName()--------------------->
2017-10-03 20:54:56,717  INFO [main] (SpringCacheWithEhCacheTest.java:37) - HTC ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@68c06cac
2017-10-03 20:54:56,718  INFO [main] (ProductServiceImpl.java:40) - <!----------Entering getByName()--------------------->
2017-10-03 20:54:56,718  INFO [main] (SpringCacheWithEhCacheTest.java:38) - HTC ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@68c06cac
2017-10-03 20:54:56,724  INFO [main] (ProductServiceImpl.java:52) - <!----------Entering updateProduct()--------------------->
2017-10-03 20:54:56,734  INFO [main] (SpringCacheWithEhCacheTest.java:43) - IPhone ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,735  INFO [main] (SpringCacheWithEhCacheTest.java:44) - IPhone ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,735  INFO [main] (SpringCacheWithEhCacheTest.java:45) - IPhone ->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,736  INFO [main] (SpringCacheWithEhCacheTest.java:47) - Refreshing all products
2017-10-03 20:54:56,741  INFO [main] (ProductServiceImpl.java:40) - <!----------Entering getByName()--------------------->
2017-10-03 20:54:56,741  INFO [main] (SpringCacheWithEhCacheTest.java:50) - IPhone [after refresh]->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,742  INFO [main] (SpringCacheWithEhCacheTest.java:51) - IPhone [after refresh]->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,742  INFO [main] (SpringCacheWithEhCacheTest.java:52) - IPhone [after refresh]->com.xgj.cache.springCacheAnno.CompleteDemoWithEhCache.domain.Product@1a8f392
2017-10-03 20:54:56,742  INFO [main] (AbstractApplicationContext.java:984) - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7bd1a567: startup date [Tue Oct 03 20:54:55 BOT 2017]; root of context hierarchy
2017-10-03 20:54:56,744  INFO [main] (EhCacheManagerFactoryBean.java:187) - Shutting down EhCache CacheManager


查看ProductServiceImpl中的 getName方法中的@Cacheable注解可知

@Cacheable(cacheNames = "products", key = "#name", condition = "#name != 'HTC'", unless = "#result==null")


HTC不缓存, 结果为空不缓存。


查看输出,第一次查询 IPhone Samsung HTC ,分别从慢速设备中加载, 当第二次第三次查询IPhone Samsung ,可以看到 并没有输出

logger.info("<!----------Entering getByName()--------------------->");


可知,其从缓存中加载。


因为不缓存HTC,所以每次查询HTC都从会执行方法,从慢速设备中查询。


当调用service.updateProduct(product); 我们使用的@CachePut注解更新缓存, 然后service.getByName(“IPhone”),缓存没有被清空,所以依然是从缓存中获取。


随后,service.refreshAllProducts(); 将缓存全部清掉,再此查询service.getByName(“IPhone”),然后再此查询可以看到输出了<!----------Entering getByName()--------------------->,紧接着的第二次第三次,是从缓存中获取的数据.


源码


代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

相关文章
|
18天前
|
Java Spring
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
|
6天前
|
XML JSON Java
SpringBoot必须掌握的常用注解!
SpringBoot必须掌握的常用注解!
27 4
SpringBoot必须掌握的常用注解!
|
7天前
|
人工智能 前端开发 Java
基于开源框架Spring AI Alibaba快速构建Java应用
本文旨在帮助开发者快速掌握并应用 Spring AI Alibaba,提升基于 Java 的大模型应用开发效率和安全性。
基于开源框架Spring AI Alibaba快速构建Java应用
|
15天前
|
前端开发 Java 数据库连接
Spring 框架:Java 开发者的春天
Spring 框架是一个功能强大的开源框架,主要用于简化 Java 企业级应用的开发,由被称为“Spring 之父”的 Rod Johnson 于 2002 年提出并创立,并由Pivotal团队维护。
37 1
Spring 框架:Java 开发者的春天
|
8天前
|
存储 缓存 Java
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
43 2
|
8天前
|
JSON Java 数据库
SpringBoot项目使用AOP及自定义注解保存操作日志
SpringBoot项目使用AOP及自定义注解保存操作日志
26 1
|
15天前
|
Java 数据库连接 开发者
Spring 框架:Java 开发者的春天
【10月更文挑战第27天】Spring 框架由 Rod Johnson 在 2002 年创建,旨在解决 Java 企业级开发中的复杂性问题。它通过控制反转(IOC)和面向切面的编程(AOP)等核心机制,提供了轻量级的容器和丰富的功能,支持 Web 开发、数据访问等领域,显著提高了开发效率和应用的可维护性。Spring 拥有强大的社区支持和丰富的生态系统,是 Java 开发不可或缺的工具。
|
15天前
|
JSON Java Maven
实现Java Spring Boot FCM推送教程
本指南介绍了如何在Spring Boot项目中集成Firebase云消息服务(FCM),包括创建项目、添加依赖、配置服务账户密钥、编写推送服务类以及发送消息等步骤,帮助开发者快速实现推送通知功能。
41 2
|
20天前
|
存储 人工智能 Java
将 Spring AI 与 LLM 结合使用以生成 Java 测试
AIDocumentLibraryChat 项目通过 GitHub URL 为指定的 Java 类生成测试代码,支持 granite-code 和 deepseek-coder-v2 模型。项目包括控制器、服务和配置,能处理源代码解析、依赖加载及测试代码生成,旨在评估 LLM 对开发测试的支持能力。
31 1
|
22天前
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
108 1