Skip to content

Commit 59626df

Browse files
Adding Spring Boot Cache implementation
1 parent b9a4ad5 commit 59626df

File tree

10 files changed

+285
-9
lines changed

10 files changed

+285
-9
lines changed

.gitignore

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
# Log file
55
*.log
66

7-
# BlueJ files
8-
*.ctxt
9-
10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
12-
137
# Package Files #
148
*.jar
159
*.war
@@ -19,6 +13,8 @@
1913
*.tar.gz
2014
*.rar
2115

22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
24-
replay_pid*
16+
# Generated Sources
17+
target
18+
19+
# IDE Files
20+
.idea

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
## Spring Boot Caching: Elevate Your App's Speed and Efficiency
2+
For complete understanding of Spring Boot Caching and how you can use it inside your application you can checkout our blog.
3+
<br/><br/>**Blog Link:** [Spring Boot Caching: Elevate Your App's Speed and Efficiency](https://bootcamptoprod.com/spring-boot-caching)
4+
<br/>
5+
16
# spring-boot-caching
27
A simple spring boot app highlighting how we can use caching

pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.1.2</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
12+
<groupId>com.bootcamptoprod</groupId>
13+
<artifactId>spring-boot-caching</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
16+
<name>spring-boot-caching</name>
17+
<description>Demo project for Spring Boot Caching</description>
18+
19+
<properties>
20+
<java.version>17</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-actuator</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-cache</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-jpa</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.h2database</groupId>
42+
<artifactId>h2</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.bootcamptoprod.caching.Entity;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
@Table(name = "Product")
7+
public class Product {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Long id;
11+
private String name;
12+
private double price;
13+
private boolean available;
14+
15+
public Product() {
16+
}
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public double getPrice() {
35+
return price;
36+
}
37+
38+
public void setPrice(double price) {
39+
this.price = price;
40+
}
41+
42+
public boolean isAvailable() {
43+
return available;
44+
}
45+
46+
public void setAvailable(boolean available) {
47+
this.available = available;
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.bootcamptoprod.caching;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cache.annotation.EnableCaching;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
7+
8+
@SpringBootApplication
9+
@EnableCaching
10+
@EnableScheduling
11+
public class SpringBootCachingApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(SpringBootCachingApplication.class, args);
15+
}
16+
17+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.bootcamptoprod.caching.controller;
2+
3+
import com.bootcamptoprod.caching.Entity.Product;
4+
import com.bootcamptoprod.caching.service.ProductService;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.util.List;
8+
9+
@RestController
10+
@RequestMapping("/api/products")
11+
public class ProductController {
12+
13+
private final ProductService productService;
14+
15+
public ProductController(ProductService productService) {
16+
this.productService = productService;
17+
}
18+
19+
@GetMapping
20+
public List<Product> getAllProducts() {
21+
return productService.getAllProducts();
22+
}
23+
24+
@GetMapping("/{productId}")
25+
public Product getProductById(@PathVariable long productId) {
26+
return productService.getProductById(productId);
27+
}
28+
29+
@PostMapping
30+
public Product addProduct(@RequestBody Product product) {
31+
return productService.updateProduct(product);
32+
}
33+
34+
@PutMapping("/{productId}")
35+
public Product updateProduct(@PathVariable long productId, @RequestBody Product product) {
36+
product.setId(productId);
37+
return productService.updateProduct(product);
38+
}
39+
40+
@DeleteMapping("/{productId}")
41+
public void deleteProduct(@PathVariable long productId) {
42+
productService.deleteProduct(productId);
43+
}
44+
45+
@GetMapping("/details/{productId}")
46+
public Product getProductDetails(@PathVariable long productId) {
47+
Product product = new Product();
48+
product.setId(productId);
49+
return productService.getProductDetails(product);
50+
}
51+
52+
@GetMapping("/price-check")
53+
public Product getProductWithPriceCheck(@RequestParam long productId) {
54+
return productService.getProductWithPriceCheck(productId);
55+
}
56+
57+
@PostMapping("/clear-cache")
58+
public void clearProductCache() {
59+
productService.clearProductCache();
60+
}
61+
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.bootcamptoprod.caching.repository;
2+
3+
4+
import com.bootcamptoprod.caching.Entity.Product;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
public interface ProductRepository extends JpaRepository<Product, Long> {
8+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.bootcamptoprod.caching.service;
2+
3+
import com.bootcamptoprod.caching.Entity.Product;
4+
import com.bootcamptoprod.caching.repository.ProductRepository;
5+
import org.springframework.cache.annotation.CacheEvict;
6+
import org.springframework.cache.annotation.CachePut;
7+
import org.springframework.cache.annotation.Cacheable;
8+
import org.springframework.cache.annotation.Caching;
9+
import org.springframework.scheduling.annotation.Scheduled;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.List;
13+
14+
@Service
15+
public class ProductService {
16+
17+
private final ProductRepository productRepository;
18+
19+
public ProductService(ProductRepository productRepository) {
20+
this.productRepository = productRepository;
21+
}
22+
23+
@Cacheable("products")
24+
public List<Product> getAllProducts() {
25+
return productRepository.findAll();
26+
}
27+
28+
@Cacheable(value = "products", key = "#productId", condition = "#productId > 0")
29+
public Product getProductById(long productId) {
30+
return productRepository.findById(productId).orElse(null);
31+
}
32+
33+
@CachePut(value = "products", key = "#product.id")
34+
public Product updateProduct(Product product) {
35+
Product updatedProduct = productRepository.save(product);
36+
return updatedProduct;
37+
}
38+
39+
@CacheEvict(value = "products", key = "#productId")
40+
public void deleteProduct(long productId) {
41+
productRepository.deleteById(productId);
42+
}
43+
44+
@Caching(
45+
cacheable = {@Cacheable(value = "products", key = "#product.id")},
46+
evict = {@CacheEvict(value = "recentProducts", allEntries = true)}
47+
)
48+
public Product getProductDetails(Product product) {
49+
// Logic to fetch detailed product information
50+
return productRepository.findById(product.getId()).orElse(null);
51+
}
52+
53+
@Cacheable(value = "products", key = "#productId", unless = "#result.price < 100")
54+
public Product getProductWithPriceCheck(long productId) {
55+
return productRepository.findById(productId).orElse(null);
56+
}
57+
58+
@CacheEvict(value = "products", allEntries = true)
59+
public void clearProductCache() {
60+
System.out.println("Product cache was cleared successfully");
61+
}
62+
63+
@CacheEvict(value = "products", allEntries = true)
64+
@Scheduled(fixedRateString = "43200000")
65+
public void clearProductsCache() {
66+
System.out.println("Clearing products cache");
67+
}
68+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
management.endpoints.web.exposure.include=*
2+
logging.level.org.springframework.cache=TRACE
3+
spring.jpa.defer-datasource-initialization=true

src/main/resources/data.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
INSERT INTO product (name, price, available) VALUES ('Product A', 150.0, true);
2+
INSERT INTO product (name, price, available) VALUES ('Product B', 200.0, true);
3+
INSERT INTO product (name, price, available) VALUES ('Product C', 80.0, false);
4+
INSERT INTO product (name, price, available) VALUES ('Product D', 70.0, false);
5+
INSERT INTO product (name, price, available) VALUES ('Product E', 54.0, true);
6+
INSERT INTO product (name, price, available) VALUES ('Product F', 68.0, true);

0 commit comments

Comments
 (0)