I'm saving the cache for pages like this. Cache depends on username
, pageNum
, and pageSize
.
public class UsersLocationsWithUsernameKeyGen implements CacheKeyGenerator {
@Override
public Object generate(Method method, Object... methodParams) {
return new CompositeCacheKey(
methodParams[0],
((PageRequest) methodParams[1]).getPageNumber(),
((PageRequest) methodParams[1]).getPageSize(),
((PageRequest) methodParams[1]).getSort());
}
}
This cache generator used to create key for next method.
@GET
@Path("/createdBy/{username}")
@CacheResult(cacheName = "locations", keyGenerator = UsersLocationsWithUsernameKeyGen.class)
public Response getUserLocations(@PathParam("username") String username, PageRequestFromQueryParam pageRequest) {
Page<Location> userLocations = locationService.getUserLocations(username, pageRequest.convertToSpringDataPageRequest());
return Response.ok(userLocations).build();
}
It creates a cache from pagination parameters. Then If I will create some Location for user, then all this data won't be valid anymore and I need to invalidate it for all cached pages.
@POST
@CacheInvalidate(cacheName = "locations", keyGenerator = UsersLocationsKeyGen.class)
public Response createLocation(Location location) {
Location createdLocation = locationService.createLocation(location);
return Response.ok(createdLocation).build();
}
I thought that this could work like this, but it's not working.
public class UsersLocationsKeyGen implements CacheKeyGenerator {
@Override
public Object generate(Method method, Object... methodParams) {
return new CompositeCacheKey(((Location) methodParams[0]).getCreatorUsername(), "*", "*", "*");
}
}
So I need to create some kind of pattern. Do delete keys and values with this pattern. Is it possible?