Skip to content

Commit 9ef348b

Browse files
committed
feat: Added /cache endpoint that shows cache counts
1 parent 555b314 commit 9ef348b

File tree

2 files changed

+96
-56
lines changed

2 files changed

+96
-56
lines changed

src/main/java/com/redis/cache/demo/MovieController.java

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import static com.redis.cache.demo.SearchService.FIELD_VOTE_COUNT;
1111

1212
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.Map;
1315

1416
import org.springframework.beans.factory.annotation.Autowired;
1517
import org.springframework.stereotype.Controller;
@@ -18,64 +20,76 @@
1820
import org.springframework.web.bind.annotation.PathVariable;
1921
import org.springframework.web.bind.annotation.RequestParam;
2022

23+
import com.redis.cache.RedisCache;
24+
import com.redis.cache.RedisCacheManager;
25+
2126
@Controller
2227
public class MovieController {
2328

24-
@Autowired
25-
MovieService movieService;
26-
27-
@Autowired
28-
SearchService searchService;
29-
30-
@GetMapping("/movie/{id}")
31-
public String movie(Model model, @PathVariable("id") Integer id) {
32-
model.addAttribute("movie", movieService.fetchMovie(id));
33-
return "movie";
34-
}
35-
36-
@GetMapping({ "/movies/popular", "/" })
37-
public String getPopularMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page)
38-
throws Exception {
39-
// Add the movies to the model
40-
model.addAttribute("movies", movieService.fetchPopularMovies(page));
41-
model.addAttribute("page", page);
42-
// Return the view name
43-
return "popular";
44-
}
45-
46-
@GetMapping("/movies/top-rated")
47-
public String getTopRatedMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page)
48-
throws Exception {
49-
50-
// Add the movies to the model
51-
model.addAttribute("movies", movieService.fetchTopRatedMovies(page));
52-
model.addAttribute("page", page);
53-
54-
// Return the view name
55-
return "top-rated";
56-
}
57-
58-
@GetMapping("/movies/upcoming")
59-
public String getUpcomingMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page)
60-
throws Exception {
61-
// Add the movies to the model
62-
model.addAttribute("movies", movieService.fetchUpcomingMovies(page));
63-
model.addAttribute("page", page);
64-
65-
// Return the view name
66-
return "upcoming";
67-
}
68-
69-
@GetMapping("/movies/search")
70-
public String search(Model model, @RequestParam(name = "query", defaultValue = "") String query) {
71-
// Add the movies to the model
72-
model.addAttribute("movies", searchService.searchMovies(query));
73-
model.addAttribute("query", query);
74-
model.addAttribute("fields", Arrays.asList(FIELD_ID, FIELD_POPULARITY, FIELD_RELEASE_DATE, FIELD_RUNTIME,
75-
FIELD_STATUS, FIELD_TAGLINE, FIELD_VOTE_AVERAGE, FIELD_VOTE_COUNT));
76-
77-
// Return the view name
78-
return "search";
79-
}
29+
@Autowired
30+
MovieService movieService;
31+
32+
@Autowired
33+
SearchService searchService;
34+
35+
@Autowired
36+
RedisCacheManager cacheManager;
37+
38+
@GetMapping("/movie/{id}")
39+
public String movie(Model model, @PathVariable("id") Integer id) {
40+
model.addAttribute("movie", movieService.fetchMovie(id));
41+
return "movie";
42+
}
43+
44+
@GetMapping({ "/movies/popular", "/" })
45+
public String getPopularMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) throws Exception {
46+
// Add the movies to the model
47+
model.addAttribute("movies", movieService.fetchPopularMovies(page));
48+
model.addAttribute("page", page);
49+
// Return the view name
50+
return "popular";
51+
}
52+
53+
@GetMapping("/movies/top-rated")
54+
public String getTopRatedMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) throws Exception {
55+
56+
// Add the movies to the model
57+
model.addAttribute("movies", movieService.fetchTopRatedMovies(page));
58+
model.addAttribute("page", page);
59+
60+
// Return the view name
61+
return "top-rated";
62+
}
63+
64+
@GetMapping("/movies/upcoming")
65+
public String getUpcomingMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) throws Exception {
66+
// Add the movies to the model
67+
model.addAttribute("movies", movieService.fetchUpcomingMovies(page));
68+
model.addAttribute("page", page);
69+
70+
// Return the view name
71+
return "upcoming";
72+
}
73+
74+
@GetMapping("/movies/search")
75+
public String search(Model model, @RequestParam(name = "query", defaultValue = "") String query) {
76+
// Add the movies to the model
77+
model.addAttribute("movies", searchService.searchMovies(query));
78+
model.addAttribute("query", query);
79+
model.addAttribute("fields", Arrays.asList(FIELD_ID, FIELD_POPULARITY, FIELD_RELEASE_DATE, FIELD_RUNTIME, FIELD_STATUS,
80+
FIELD_TAGLINE, FIELD_VOTE_AVERAGE, FIELD_VOTE_COUNT));
81+
82+
// Return the view name
83+
return "search";
84+
}
85+
86+
@GetMapping("/cache")
87+
public String cacheInfo(Model model) {
88+
Map<String, Long> cacheInfo = new HashMap<>();
89+
RedisCache cache = (RedisCache) cacheManager.getCache("movie");
90+
cacheInfo.put("movie", cache.getCount());
91+
model.addAttribute("cacheInfo", cacheInfo);
92+
return "cache";
93+
}
8094

8195
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
+<html xmlns:th="http://www.thymeleaf.org">
3+
+
4+
+<!-- <head th:insert="~{fragments/base :: head}"></head> -->
5+
+
6+
+<head>
7+
+ <title>Cache Info</title>
8+
+</head>
9+
+<body>
10+
+ <h1>Cache Information</h1>
11+
+ <table>
12+
+ <thead>
13+
+ <tr>
14+
+ <th>Cache Name</th>
15+
+ <th>Count</th>
16+
+ </tr>
17+
+ </thead>
18+
+ <tbody>
19+
+ <tr th:each="entry : ${cacheInfo}">
20+
+ <td th:text="${entry.key}"></td>
21+
+ <td th:text="${entry.value}"></td>
22+
+ </tr>
23+
+ </tbody>
24+
+ </table>
25+
+</body>
26+
+</html>

0 commit comments

Comments
 (0)