I am using springBootVersion = 1.5.2.RELEASE and gradle version = 5.4.1.
I am passing token(consider an account) in request, There might be multiple channels associated to an account(requested token), I want to fetch all channels associated to request account and store them into List.
If user not perform any activity for 2 minutes for that user only Cache should be expired.
In order to fetch channelIds and store them into List, I have used Spring boot @Cacheable annotation but It is not working expected as it is fetching existing list data.
Please consider a dummy scenario to understand.
If I request token = 123, API fetching all channels associated this token and storing them into list which is fine.
but when I request token = 987 still API returning token = 123 channels, API supposed to return channels associated to token = 987
Here is code snippet
Main Spring Boot Runner Class
@SpringBootApplication
@EnableCaching
@ComponentScan(basePackages = { "some packages to scan" })
public class ChannelApiApplication {
public static void main(String[] args) {
SpringApplication.run(ChannelApiApplication.class, args);
}
}
UserController.java
@RestController
public class UserController {
public ResponseEntity<AccountsAssociatedChannels> getAccountsAssociatedChannels(
@PathVariable Integer userId,
@RequestHeader("Cookie") String btCookie,
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam(value = "pageSize", required = false, defaultValue = "25") int pageSize,
UriComponentsBuilder uriBuilder, HttpServletResponse response, HttpServletRequest request)
throws Exception {
try {
List<AccountAssociatedChannels> accountResponse = userService.getAccountsAssociatedChannels(userId,
btCookie, page, pageSize);
UserService.java
@Service
@Configurable
public class UserService {
@Autowired
ChannelServiceClient channelServiceClient;
public List<AccountAssociatedChannels> getAccountsAssociatedChannels(Integer userId, String btCookie, int page,
int pageSize) throws Exception {
// Calling Rest API to get channels associated to btCookie
List<MyChannelResponse> myChannelResponse = channelServiceClient.getMyChannelResponse(btCookie, page,
pageSize, null);
ChannelServiceClient.java
@Service
public class ChannelServiceClient {
@Cacheable(value = "channelIds", key = "#root.args[0]")
public List<MyChannelResponse> getMyChannelResponse(String btCookie, Integer page, Integer pageSize, String nextLinkURL)
throws Exception {
List<MyChannelResponse> channelIds = new ArrayList<>();
// Fetch all channelIds associated user and store into list.
while (true) {
// invoke getChannels() method to get channels
MyChannelResponses myChannelResponse = getChannels(btCookie, page, pageSize, nextLinkURL);
List<PaginationLinks> links = myChannelResponse.getLinks();
channelIds.addAll(myChannelResponse.getChannels());
//Some logic
}
} // getMyChannelResponse() method close
@Cacheable(value = "mychannel", key = "#root.args[0]")
public MyChannelResponses getChannels(String btCookie, Integer page, Integer pageSize, String uri)
throws Exception {
try {
log.debug("Entering Cookie: " + btCookie + " to get channels.");
// Calling Spring Boot Rest API to get channel associated btCookie
} catch (Exception e) {
// throwing some exception
}
}// getChannels() method close
}
Thank you.
token?btCookiewhich is coming in request.