Skip to content
Snippets Groups Projects
Commit 0c09ec3b authored by Mic's avatar Mic
Browse files

added @Cacheable support with ehcache

parent 4ccf1eaa
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@
<config>src/main/webapp/WEB-INF/mvc-view-config.xml</config>
<config>src/main/resources/spring/dao-config.xml</config>
<config>src/main/resources/spring/datasource-config.xml</config>
<config>src/main/resources/spring/tools-config.xml</config>
</configs>
<configSets>
<configSet>
......@@ -22,7 +23,7 @@
<config>src/main/webapp/WEB-INF/mvc-view-config.xml</config>
<config>src/main/resources/spring/dao-config.xml</config>
<config>src/main/resources/spring/datasource-config.xml</config>
<config>src/main/resources/spring/jmx-aop-config.xml</config>
<config>src/main/resources/spring/tools-config.xml</config>
</configs>
<profiles>
</profiles>
......
......@@ -31,6 +31,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
......@@ -204,6 +209,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.5.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
......
......@@ -6,14 +6,12 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.samples.petclinic.model.Specialty;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.repository.VetRepository;
......@@ -28,16 +26,13 @@ import org.springframework.stereotype.Repository;
* @author Sam Brannen
* @author Thomas Risberg
* @author Mark Fisher
* @author Michael Isvy
*/
@Repository
public class JdbcVetRepositoryImpl implements VetRepository {
private final Logger logger = LoggerFactory.getLogger(getClass());
private JdbcTemplate jdbcTemplate;
private final List<Vet> vets = new ArrayList<Vet>();
@Autowired
public JdbcVetRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
......@@ -47,14 +42,11 @@ public class JdbcVetRepositoryImpl implements VetRepository {
* Refresh the cache of Vets that the ClinicService is holding.
* @see org.springframework.samples.petclinic.model.service.ClinicService#findVets()
*/
@ManagedOperation
public void refreshVetsCache() throws DataAccessException {
synchronized (this.vets) {
this.logger.info("Refreshing vets cache");
// Retrieve the list of all vets.
this.vets.clear();
this.vets.addAll(this.jdbcTemplate.query(
@Cacheable(value="vets")
public Collection<Vet> findAll() throws DataAccessException {
List<Vet> vets = new ArrayList<Vet>();
// Retrieve the list of all vets.
vets.addAll(this.jdbcTemplate.query(
"SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
ParameterizedBeanPropertyRowMapper.newInstance(Vet.class)));
......@@ -64,7 +56,7 @@ public class JdbcVetRepositoryImpl implements VetRepository {
ParameterizedBeanPropertyRowMapper.newInstance(Specialty.class));
// Build each vet's list of specialties.
for (Vet vet : this.vets) {
for (Vet vet : vets) {
final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
new ParameterizedRowMapper<Integer>() {
......@@ -77,17 +69,6 @@ public class JdbcVetRepositoryImpl implements VetRepository {
vet.addSpecialty(specialty);
}
}
return vets;
}
}
public Collection<Vet> findAll() throws DataAccessException {
synchronized (this.vets) {
if (this.vets.isEmpty()) {
refreshVetsCache();
}
return this.vets;
}
}
}
......@@ -5,6 +5,7 @@ import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.stereotype.Repository;
......@@ -27,6 +28,7 @@ public class JpaVetRepositoryImpl implements VetRepository {
private EntityManager em;
@Cacheable(value="vets")
@SuppressWarnings("unchecked")
public Collection<Vet> findAll() {
return this.em.createQuery("SELECT vet FROM Vet vet join fetch vet.specialties ORDER BY vet.lastName, vet.firstName").getResultList();
......
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir"/>
<!-- objects are evicted from the cache every 60 seconds -->
<cache name="vets"
timeToLiveSeconds="60"
maxElementsInMemory="100"
eternal="false"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
This diff is collapsed.
File mode changed from 100755 to 100644
......@@ -10,7 +10,6 @@
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
......
......@@ -4,8 +4,10 @@
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--
......@@ -18,12 +20,26 @@
</aop:aspectj-autoproxy>
<!-- Call monitoring aspect that monitors call count and call invocation time -->
<bean id="callMonitor" class="org.springframework.samples.petclinic.util.CallMonitoringAspect"/>
<bean id="callMonitor" class="org.springframework.samples.petclinic.util.CallMonitoringAspect"/>
<!--
Exporter that exposes the CallMonitoringAspect via JMX,
based on the @ManagedResource, @ManagedAttribute, and @ManagedOperation annotations.
-->
<context:mbean-export />
<context:mbean-export />
<!-- enables scanning for @Cacheable annotation -->
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
</beans>
\ No newline at end of file
......@@ -23,7 +23,7 @@ id="WebApp_ID" version="2.5">
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dao-config.xml, classpath:spring/jmx-aop-config.xml</param-value>
<param-value>classpath:spring/dao-config.xml, classpath:spring/tools-config.xml</param-value>
</context-param>
<listener>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment