Skip to content

Commit 0347d19

Browse files
committed
Added sample to show usage of Spring's caching abstraction alongside Spring Data repositories.
1 parent 4d27129 commit 0347d19

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.caching;
17+
18+
import java.util.Arrays;
19+
20+
import org.springframework.cache.Cache;
21+
import org.springframework.cache.CacheManager;
22+
import org.springframework.cache.annotation.EnableCaching;
23+
import org.springframework.cache.concurrent.ConcurrentMapCache;
24+
import org.springframework.cache.support.SimpleCacheManager;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.annotation.ImportResource;
28+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
29+
30+
/**
31+
* Java config to use Spring Data JPA alongside the Spring caching support.
32+
*
33+
* @author Oliver Gierke
34+
*/
35+
@Configuration
36+
@EnableJpaRepositories
37+
@ImportResource("classpath:infrastructure.xml")
38+
@EnableCaching
39+
public class CachingConfiguration {
40+
41+
@Bean
42+
public CacheManager cacheManager() {
43+
44+
Cache cache = new ConcurrentMapCache("byUsername");
45+
46+
SimpleCacheManager manager = new SimpleCacheManager();
47+
manager.setCaches(Arrays.asList(cache));
48+
49+
return manager;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.caching;
17+
18+
import org.springframework.cache.annotation.CacheEvict;
19+
import org.springframework.cache.annotation.Cacheable;
20+
import org.springframework.data.jpa.example.domain.User;
21+
import org.springframework.data.repository.CrudRepository;
22+
23+
/**
24+
* User repository using Spring's caching abstraction.
25+
*
26+
* @author Oliver Gierke
27+
*/
28+
public interface CachingUserRepository extends CrudRepository<User, Long> {
29+
30+
@Override
31+
@CacheEvict("byUsername")
32+
<S extends User> S save(S entity);
33+
34+
@Cacheable("byUsername")
35+
User findByUsername(String username);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Sample for the integration of the Spring Caching abstraction with Spring Data repositories.
3+
*/
4+
package org.springframework.data.jpa.example.repository.caching;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.caching;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.cache.Cache;
25+
import org.springframework.cache.Cache.ValueWrapper;
26+
import org.springframework.cache.CacheManager;
27+
import org.springframework.cache.annotation.Cacheable;
28+
import org.springframework.data.jpa.example.domain.User;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
import org.springframework.transaction.annotation.Transactional;
32+
33+
/**
34+
* Integration test to show how to use {@link Cacheable} with a Spring Data repository.
35+
*
36+
* @author Oliver Gierke
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@ContextConfiguration(classes = CachingConfiguration.class)
40+
@Transactional
41+
public class CachingRepositoryTests {
42+
43+
@Autowired
44+
CachingUserRepository repository;
45+
@Autowired
46+
CacheManager cacheManager;
47+
48+
@Test
49+
public void cachesValuesReturnedForQueryMethod() {
50+
51+
User dave = new User();
52+
dave.setUsername("dmatthews");
53+
54+
dave = repository.save(dave);
55+
56+
User result = repository.findByUsername("dmatthews");
57+
assertThat(result, is(dave));
58+
59+
// Verify entity cached
60+
Cache cache = cacheManager.getCache("byUsername");
61+
ValueWrapper wrapper = cache.get("dmatthews");
62+
assertThat(wrapper.get(), is((Object) dave));
63+
}
64+
}

0 commit comments

Comments
 (0)