Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Created January 15, 2023 17:35
Show Gist options
  • Save oofnivek/a9c236cd0cfed867142c5c598168fde9 to your computer and use it in GitHub Desktop.
Save oofnivek/a9c236cd0cfed867142c5c598168fde9 to your computer and use it in GitHub Desktop.
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