6
jdbc/howto/caching/README.adoc
Normal file
6
jdbc/howto/caching/README.adoc
Normal file
@@ -0,0 +1,6 @@
|
||||
== Spring Data JDBC How To cache.
|
||||
|
||||
Spring Data JDBC doesn't include any caching for entities.
|
||||
But it is trivially combined with Springs Cache abstraction.
|
||||
|
||||
This project demonstrates this.
|
||||
21
jdbc/howto/caching/pom.xml
Normal file
21
jdbc/howto/caching/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-jdbc-how-to-caching</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-jdbc-how-to</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data JDBC - How to do caching</name>
|
||||
<description>Sample project for Spring Data JDBC demonstrating how it can be used with Springs caching
|
||||
abstraction.
|
||||
</description>
|
||||
<url>https://projects.spring.io/spring-data-jdbc</url>
|
||||
<inceptionYear>2021</inceptionYear>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package example.springdata.jdbc.howto.caching;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
|
||||
@EnableCaching
|
||||
@SpringBootApplication
|
||||
class CachingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CachingApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.howto.caching;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Minion {
|
||||
@Id
|
||||
Long id;
|
||||
String name;
|
||||
|
||||
Minion(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jdbc.howto.caching;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
interface MinionRepository extends CrudRepository<Minion, Long> {
|
||||
|
||||
@Override
|
||||
@CacheEvict(value="minions",beforeInvocation = false,key = "#result.id")
|
||||
<S extends Minion> S save(S s);
|
||||
|
||||
@Override
|
||||
@Cacheable("minions")
|
||||
Optional<Minion> findById(Long aLong);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
|
||||
5
jdbc/howto/caching/src/main/resources/schema.sql
Normal file
5
jdbc/howto/caching/src/main/resources/schema.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE MINION
|
||||
(
|
||||
ID IDENTITY PRIMARY KEY,
|
||||
NAME VARCHAR(255),
|
||||
);
|
||||
@@ -0,0 +1,39 @@
|
||||
package example.springdata.jdbc.howto.caching;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class CachingApplicationTests {
|
||||
|
||||
private Long bobsId;
|
||||
@Autowired MinionRepository minions;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
|
||||
Minion bob = minions.save(new Minion("Bob"));
|
||||
bobsId = bob.id;
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveloadMultipleTimes() {
|
||||
|
||||
Optional<Minion> bob = null;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
bob = minions.findById(bobsId);
|
||||
}
|
||||
|
||||
minions.save(bob.get());
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
bob = minions.findById(bobsId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
<modules>
|
||||
<module>bidirectionalexternal</module>
|
||||
<module>bidirectionalinternal</module>
|
||||
<module>caching</module>
|
||||
<module>idgeneration</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user