Created
January 15, 2023 17:35
-
-
Save oofnivek/a9c236cd0cfed867142c5c598168fde9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
public class BookController { | |
@Autowired | |
BookService bookService; | |
@Autowired | |
CacheStore<Book> bookCache; | |
@GetMapping("/") | |
public String index(){ | |
return "OK"; | |
} | |
@GetMapping(value = "/book/{id}") | |
public Book getBook(@PathVariable String id){ | |
// get book from cache | |
Book cached = bookCache.get(id); | |
if(cached != null){ | |
return cached; | |
} | |
// get book from service | |
Book book = bookService.getBookById(id); | |
// add book to cache | |
bookCache.add(id, book); | |
return book; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment