Spring Caching und Ehcache-Beispiel

Beispiel für Spring Caching und Ehcache

spring-ehcache

In diesem Tutorial zeigen wir Ihnen, wie Sie das Zwischenspeichern von Daten in einer Spring-Anwendung aktivieren und in das beliebte FrameworkEhcacheintegrieren.

Benutztes Werkzeug

  1. Ehcache 2.9

  2. Feder 4.1.4.FREIGABE

  3. Logback 1.0.13

  4. Maven 3 / Gradle 2

  5. JDK 1.7

  6. Eclipse 4.4

Note
Spring unterstützt das Caching seit Version 3.1
Der Spring-Cache wurde seit Version 4.1 erheblich verbessert

1. Projektverzeichnisstruktur

spring-ehcache-example

2. Projektabhängigkeiten

Das Spring-Caching befindet sich inspring-context.jar. Um das Ehcache-Caching zu unterstützen, müssen Sie auch dasspring-context-support.jar einschließen.

Für das Maven-Projekt:

pom.xml

    
        net.sf.ehcache
        ehcache
        2.9.0
    

        
    
        ch.qos.logback
        logback-classic
        1.0.13
    

    
    
        org.springframework
        spring-context
        4.1.4.RELEASE
    

    
    
        org.springframework
        spring-context-support
        4.1.4.RELEASE
    

Für Gradle-Projekt:

gradle.build

apply plugin: 'java'
apply plugin: 'eclipse-wtp'

version = '1.0'

// Uses JDK 7
sourceCompatibility = 1.7
targetCompatibility = 1.7

// Get dependencies from Maven central repository
repositories {
    mavenCentral()
}

//Project dependencies
dependencies {
    compile 'org.springframework:spring-context:4.1.4.RELEASE'
    compile 'org.springframework:spring-context-support:4.1.4.RELEASE'
    compile 'net.sf.ehcache:ehcache:2.9.0'
    compile 'ch.qos.logback:logback-classic:1.0.13'
}

3. Spring Non-Cache-Beispiel

Ein einfaches DAO, um einen Film nach Regie zu finden.

Movie.java

package com.example.movie;

import java.io.Serializable;

public class Movie implements Serializable {

    int id;
    String name;
    String directory;

    //getters and setters
    //constructor with fields
    //toString()
}

MovieDao.java

package com.example.movie;

public interface MovieDao{

    Movie findByDirector(String name);

}

MovieDaoImpl.java

package com.example.movie;

import org.springframework.stereotype.Repository;

@Repository("movieDao")
public class MovieDaoImpl implements MovieDao{

    //each call will delay 2 seconds, simulate the slow query call
    public Movie findByDirector(String name) {
        slowQuery(2000L);
        System.out.println("findByDirector is running...");
        return new Movie(1,"Forrest Gump","Robert Zemeckis");
    }

    private void slowQuery(long seconds){
        try {
                Thread.sleep(seconds);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
    }

}

AppConfig.java

package com.example.test;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({ "com.example.*" })
public class AppConfig {
}

App.java

package com.example.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.example.movie.MovieDao;

public class App {

    private static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MovieDao obj = (MovieDao) context.getBean("movieDao");

        log.debug("Result : {}", obj.findByDirector("dummy"));
        log.debug("Result : {}", obj.findByDirector("dummy"));
        log.debug("Result : {}", obj.findByDirector("dummy"));

    }
}

Ausgabe

findByDirector is running...
2015-01-22 10:39:04 [main] DEBUG com.example.test.App - Result : Movie [id=1, name=Forrest Gump, directory=Robert Zemeckis]
findByDirector is running...
2015-01-22 10:39:06 [main] DEBUG com.example.test.App - Result : Movie [id=1, name=Forrest Gump, directory=Robert Zemeckis]
findByDirector is running...
2015-01-22 10:39:08 [main] DEBUG com.example.test.App - Result : Movie [id=1, name=Forrest Gump, directory=Robert Zemeckis]

Jeder Aufruf vonfindByDirector dauert 2 Sekunden.

4. Spring Caching Beispiel + EhCache

Jetzt aktivieren wir das Daten-Caching für MethodefindByDirector.

4.1 Create a ehcache.xml file, to tell Ehcache how and where to cache the data.

src/main/resource/ehcache.xml



    

    
        
    

Note
Um zu erfahren, wie Ehcache konfiguriert wird, lesen Sie dieses offizielle Beispiel fürehcache.xml.

4.2 Add @Cacheable on the method you want to cache.

MovieDaoImpl.java

package com.example.movie;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

@Repository("movieDao")
public class MovieDaoImpl implements MovieDao{

    //This "movieFindCache" is delcares in ehcache.xml
    @Cacheable(value="movieFindCache", key="#name")
    public Movie findByDirector(String name) {
        slowQuery(2000L);
        System.out.println("findByDirector is running...");
        return new Movie(1,"Forrest Gump","Robert Zemeckis");
    }

    private void slowQuery(long seconds){
        try {
                Thread.sleep(seconds);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
    }

}

4.3 Enable Caching with @EnableCaching and declared a EhCacheCacheManager.

AppConfig.java

package com.example.test;

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;

@Configuration
@EnableCaching
@ComponentScan({ "com.example.*" })
public class AppConfig {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
        cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cmfb.setShared(true);
        return cmfb;
    }
}

4.4 In non-web application, you need to shut down the Spring context manually, so that Ehcache got chance to shut down as well, otherwise Ehcache manager will hang there.

App.java

package com.example.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.example.movie.MovieDao;

public class App {

    private static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MovieDao obj = (MovieDao) context.getBean("movieDao");

        log.debug("Result : {}", obj.findByDirector("dummy"));
        log.debug("Result : {}", obj.findByDirector("dummy"));
        log.debug("Result : {}", obj.findByDirector("dummy"));

        //shut down the Spring context.
        ((ConfigurableApplicationContext)context).close();

    }
}

Ausgabe

INFO: Initializing EhCache CacheManager

findByDirector is running...
2015-01-22 10:53:28 [main] DEBUG com.example.test.App - Result : Movie [id=1, name=Forrest Gump, directory=Robert Zemeckis]
2015-01-22 10:53:28 [main] DEBUG com.example.test.App - Result : Movie [id=1, name=Forrest Gump, directory=Robert Zemeckis]
2015-01-22 10:53:28 [main] DEBUG com.example.test.App - Result : Movie [id=1, name=Forrest Gump, directory=Robert Zemeckis]

INFO: Shutting down EhCache CacheManager

Überprüfen Sie die ausgeführte Zeit, es gibt keine Verzögerung. Außerdem wird nur ein "findByDirector wird ausgeführt ..." ausgegeben, da diese Methode nur einmal ausgeführt wird und der nachfolgende Aufruf das Objekt aus dem Cache abruft.

Erledigt.

More on Spring Caching
Dieser Artikel soll Ihnen den Einstieg in das Spring-Daten-Caching erleichtern und mehr über andere Caching-Annotationen wie@CacheEvict,@CachePut,@CacheConfig usw. erfahren Bitte beziehen Sie sich auf diese offiziellenSpring Cache Abstraction documentation, die dort sehr detailliert sind.

Quellcode herunterladen

Laden Sie es herunter -Spring-Ehcache-Example.zip (20 KB)