mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 13:05:49 +00:00
Enable ehcache with JSR-107
This commit is contained in:
parent
e879e6c566
commit
ed2b4d6280
3 changed files with 57 additions and 9 deletions
10
pom.xml
10
pom.xml
|
@ -79,6 +79,16 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- EhCache -->
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package org.springframework.samples.petclinic.config;
|
||||
|
||||
import org.ehcache.config.CacheConfiguration;
|
||||
import org.ehcache.config.builders.CacheConfigurationBuilder;
|
||||
import org.ehcache.config.builders.ResourcePoolsBuilder;
|
||||
import org.ehcache.config.units.EntryUnit;
|
||||
import org.ehcache.expiry.Duration;
|
||||
import org.ehcache.expiry.Expirations;
|
||||
import org.ehcache.jsr107.Eh107Configuration;
|
||||
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import javax.cache.CacheManager;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Cache could be disable in unit test.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CacheConfig {
|
||||
|
||||
@Bean
|
||||
public JCacheManagerCustomizer cacheManagerCustomizer() {
|
||||
return new JCacheManagerCustomizer() {
|
||||
@Override
|
||||
public void customize(CacheManager cacheManager) {
|
||||
CacheConfiguration<Object, Object> config = CacheConfigurationBuilder
|
||||
.newCacheConfigurationBuilder(Object.class, Object.class,
|
||||
ResourcePoolsBuilder.newResourcePoolsBuilder()
|
||||
.heap(100, EntryUnit.ENTRIES))
|
||||
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(60, TimeUnit.SECONDS)))
|
||||
.build();
|
||||
cacheManager.createCache("vets", Eh107Configuration.fromEhcacheCacheConfiguration(config));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -15,16 +15,9 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
import org.springframework.samples.petclinic.model.Vet;
|
||||
import org.springframework.samples.petclinic.model.Visit;
|
||||
import org.springframework.samples.petclinic.model.*;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||
|
@ -32,6 +25,9 @@ import org.springframework.samples.petclinic.repository.VisitRepository;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.cache.annotation.CacheResult;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Mostly used as a facade for all Petclinic controllers
|
||||
* Also a placeholder for @Transactional and @Cacheable annotations
|
||||
|
@ -99,7 +95,7 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable(value = "vets")
|
||||
@CacheResult(cacheName = "vets")
|
||||
public Collection<Vet> findVets() throws DataAccessException {
|
||||
return vetRepository.findAll();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue