Skip to content

Commit 7a82bcf

Browse files
committed
initial commit for cachevict module to demonstate cache evict
1 parent 197eb76 commit 7a82bcf

File tree

17 files changed

+551
-14
lines changed

17 files changed

+551
-14
lines changed

cachevict/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Caching in Spring Boot RESTful Service: Part 2- Cache Eviction
2+
3+
This is an example to demonstrate cache eviction in a Spring Boot RESTful application published in http://www.springframework.guru

cachevict/pom.xml

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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>2.1.0.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.springframework</groupId>
12+
<artifactId>caching-restful-source-code</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>caching-restful-source-code</name>
15+
<description>Demonstrate cache evict in REST APIs</description>
16+
17+
<properties>
18+
<java.version>11</java.version>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<maven.compiler.source>11</maven.compiler.source>
21+
<maven.compiler.target>11</maven.compiler.target>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-cache</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jpa</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter-engine</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-junit-jupiter</artifactId>
50+
<version>2.22.0</version>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>javax.xml.bind</groupId>
56+
<artifactId>jaxb-api</artifactId>
57+
<version>2.3.1</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.sun.xml.bind</groupId>
61+
<artifactId>jaxb-core</artifactId>
62+
<version>2.3.0</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>com.sun.xml.bind</groupId>
67+
<artifactId>jaxb-impl</artifactId>
68+
<version>2.3.2</version>
69+
</dependency>
70+
71+
<dependency>
72+
<groupId>javax.activation</groupId>
73+
<artifactId>activation</artifactId>
74+
<version>1.1.1</version>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>org.mockito</groupId>
79+
<artifactId>mockito-core</artifactId>
80+
<version>2.23.4</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.h2database</groupId>
85+
<artifactId>h2</artifactId>
86+
<version>1.4.200</version>
87+
<!-- <scope>test</scope>-->
88+
</dependency>
89+
</dependencies>
90+
91+
<build>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-compiler-plugin</artifactId>
96+
<version>3.8.1</version>
97+
</plugin>
98+
<plugin>
99+
<artifactId>maven-clean-plugin</artifactId>
100+
<version>3.1.0</version>
101+
</plugin>
102+
<plugin>
103+
<artifactId>maven-resources-plugin</artifactId>
104+
<!-- <version>3.0.2</version>-->
105+
</plugin>
106+
<plugin>
107+
<artifactId>maven-surefire-plugin</artifactId>
108+
<version>2.22.2</version>
109+
</plugin>
110+
<plugin>
111+
<artifactId>maven-failsafe-plugin</artifactId>
112+
<!-- <version>2.22.2</version>-->
113+
</plugin>
114+
<plugin>
115+
<artifactId>maven-install-plugin</artifactId>
116+
<version>2.5.2</version>
117+
</plugin>
118+
<plugin>
119+
<artifactId>maven-deploy-plugin</artifactId>
120+
<version>2.8.2</version>
121+
</plugin>
122+
</plugins>
123+
</build>
124+
125+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.springframework;
2+
3+
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
8+
@EnableCaching
9+
@SpringBootApplication
10+
public class CachevictApplication {
11+
public static void main(String[] args) {
12+
// SpringApplication.run(CachevictApplication.class,args);
13+
14+
SpringApplication application = new SpringApplication(CachevictApplication.class);
15+
// ... customize application settings here
16+
application.run(args);
17+
18+
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.springframework.controller;
2+
3+
import com.springframework.model.Product;
4+
import com.springframework.service.ProductService;
5+
import org.slf4j.Logger.*;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping("/api/version1/")
15+
public class ProductController {
16+
private ProductService productService;
17+
18+
//private static final Logger logger = LoggerFactory.getL(ProductController.class);
19+
20+
@Autowired
21+
public ProductController(ProductService productService) {
22+
this.productService = productService;
23+
}
24+
25+
@PostMapping("product")
26+
public ResponseEntity<Product> saveAProduct(@RequestBody Product product){
27+
Product saveProduct = productService.addProduct(product);
28+
return new ResponseEntity<>(saveProduct,HttpStatus.OK);
29+
}
30+
31+
@GetMapping("products")
32+
public ResponseEntity<List<Product>> getAllProducts(){
33+
34+
return new ResponseEntity<List<Product>>(
35+
(List <Product>) productService.getAllProducts(),HttpStatus.OK);
36+
}
37+
38+
@GetMapping("product/{id}")
39+
public ResponseEntity<Product> getProductById(@PathVariable int id){
40+
Product retrievedProduct = productService.getProduct(id);
41+
return new ResponseEntity<Product>(retrievedProduct, HttpStatus.OK);
42+
}
43+
44+
@DeleteMapping("product/{id}")
45+
public ResponseEntity<Product> deleteProduct(@PathVariable("id") int id) {
46+
ResponseEntity responseEntity;
47+
Product deletedProduct = productService.deleteProductById(id);
48+
responseEntity = new ResponseEntity<Product>(deletedProduct, HttpStatus.OK);
49+
return responseEntity;
50+
}
51+
boolean deleteAProductById(@PathVariable() int id){
52+
productService.deleteProductById(id);
53+
return true;
54+
}
55+
56+
@PutMapping("product")
57+
public ResponseEntity<Product> updateProduct(@RequestBody Product product) {
58+
59+
//logger.info(".... Updating product Content of id: " + product.getId());
60+
Product updatedProduct = productService.updateProduct(product);
61+
return new ResponseEntity<>(updatedProduct, HttpStatus.OK);
62+
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.springframework.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class Product {
8+
9+
@Id
10+
private int id;
11+
private String PName;
12+
private float price;
13+
14+
public Product() {
15+
}
16+
17+
public Product(int id, String PName, float price) {
18+
this.id = id;
19+
this.PName = PName;
20+
this.price = price;
21+
}
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int id) {
28+
this.id = id;
29+
}
30+
31+
public String getPName() {
32+
return PName;
33+
}
34+
35+
public void setPName(String PName) {
36+
this.PName = PName;
37+
}
38+
39+
public float getPrice() {
40+
return price;
41+
}
42+
43+
public void setPrice(float price) {
44+
this.price = price;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.springframework.repository;
2+
3+
import com.springframework.model.Product;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface ProductRepository extends JpaRepository<Product, Integer> {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.springframework.service;
2+
3+
import com.springframework.model.Product;
4+
5+
import java.util.List;
6+
7+
public interface ProductService {
8+
Product addProduct(Product product);
9+
Product getProduct(int id);
10+
List<Product> getAllProducts();
11+
Product deleteProductById(int id);
12+
Product updateProduct(Product product);
13+
14+
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.springframework.service;
2+
3+
4+
import com.springframework.model.Product;
5+
import com.springframework.repository.ProductRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.cache.annotation.*;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
@CacheConfig(cacheNames = "product")
14+
@Service
15+
public class ProductServiceImpl implements ProductService{
16+
17+
private ProductRepository productRepository;
18+
19+
20+
public ProductServiceImpl(){}
21+
22+
@Autowired
23+
public void setProductRepository(ProductRepository productRepository){
24+
this.productRepository =productRepository;
25+
}
26+
27+
@Autowired
28+
public ProductServiceImpl(ProductRepository productRepository) {
29+
30+
this.productRepository = productRepository;
31+
}
32+
33+
@Caching(evict = { @CacheEvict(value = "allproductcache", allEntries = true),
34+
@CacheEvict(value = "productcache", key = "#product.id")
35+
})
36+
37+
@Override
38+
public Product addProduct(Product product) {
39+
return productRepository.save(product);
40+
}
41+
42+
@Cacheable(value = "productcache",key ="#id" )
43+
@Override
44+
public Product getProduct(int id) {
45+
System.out.println("Data retrieved from database\n");
46+
return productRepository.findById(id).orElse(null);
47+
}
48+
49+
@Cacheable(value = "allproductcache")
50+
@Override
51+
public List<Product> getAllProducts() {
52+
System.out.println("Data retrieved from database\n");
53+
return (List<Product>) productRepository.findAll();
54+
}
55+
56+
@Caching(evict = {
57+
@CacheEvict(value = "allproductcache", allEntries = true),
58+
@CacheEvict(value = "productcache", key = "#product.id")
59+
})
60+
@Override
61+
public Product deleteProductById(int id) {
62+
Product product = null;
63+
Optional optional = productRepository.findById(id);
64+
if (optional.isPresent()) {
65+
product = productRepository.findById(id).get();
66+
productRepository.deleteById(id);
67+
}
68+
System.out.println("Product deleted in database\n ");
69+
return product;
70+
}
71+
72+
@CachePut(key = "#product.id")
73+
@Override
74+
public Product updateProduct(Product product){
75+
Product updateProduct = null;
76+
Optional optional = productRepository.findById(product.getId());
77+
if (optional.isPresent()){
78+
Product getProduct = productRepository.findById(product.getId()).get();
79+
getProduct.setPName(product.getPName());
80+
getProduct.setPrice(product.getPrice());
81+
updateProduct = addProduct(getProduct);
82+
}
83+
System.out.println("Product updated\n");
84+
return updateProduct;
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.springframework;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class CachevictApplicationTest {
8+
9+
@Test
10+
void contextLoads(){
11+
12+
}
13+
14+
15+
}

0 commit comments

Comments
 (0)