-
Notifications
You must be signed in to change notification settings - Fork 0
Keep owner directory searches fresh after record changes #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2012-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.samples.petclinic.owner; | ||
|
|
||
| import org.springframework.samples.petclinic.system.CacheOperations; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| /** | ||
| * Keeps front-desk owner-directory projections synchronized after aggregate changes. | ||
| */ | ||
| @Service | ||
| public class OwnerDirectorySynchronizer { | ||
|
|
||
| private final CacheOperations cacheOperations; | ||
|
|
||
| public OwnerDirectorySynchronizer(CacheOperations cacheOperations) { | ||
| this.cacheOperations = cacheOperations; | ||
| } | ||
|
|
||
| public void ownerAggregateChanged() { | ||
| this.cacheOperations.evict(this.cacheOperations.ownerSearchCache()); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright 2012-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.samples.petclinic.system; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * Lightweight operational endpoints for checking and refreshing owner-search data. | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/manage/cache/owner-search") | ||
| class CacheDiagnosticsController { | ||
|
|
||
| private final CacheOperations cacheOperations; | ||
|
|
||
| CacheDiagnosticsController(CacheOperations cacheOperations) { | ||
| this.cacheOperations = cacheOperations; | ||
| } | ||
|
|
||
| @GetMapping | ||
| CacheOperations.CacheStatus status() { | ||
| return this.cacheOperations.ownerSearchStatus(); | ||
| } | ||
|
|
||
| @DeleteMapping | ||
| Map<String, Object> evict() { | ||
| boolean evicted = this.cacheOperations.evictOwnerSearch(); | ||
| return Map.of("cache", CacheOperations.OWNER_SEARCH_CACHE, "evicted", evicted); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright 2012-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.samples.petclinic.system; | ||
|
|
||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| /** | ||
| * Operational boundary for inspecting and invalidating application caches. | ||
| */ | ||
| @Service | ||
| public class CacheOperations { | ||
|
|
||
| public static final String OWNER_SEARCH_CACHE = "ownerSearch"; | ||
|
|
||
| private final CacheManager cacheManager; | ||
|
|
||
| public CacheOperations(CacheManager cacheManager) { | ||
| this.cacheManager = cacheManager; | ||
| } | ||
|
|
||
| /** | ||
| * Invalidate all cached owner-directory searches. | ||
| * @return {@code true} when the configured cache was available | ||
| */ | ||
| public boolean evictOwnerSearch() { | ||
| return evict(ownerSearchCache()); | ||
| } | ||
|
|
||
| /** | ||
| * Evict every entry from the supplied cache. | ||
| * @param cache the cache selected by the caller | ||
| * @return {@code true} when a cache was supplied | ||
| */ | ||
| public boolean evict(Cache cache) { | ||
| if (cache == null) { | ||
| return false; | ||
| } | ||
| cache.clear(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a cache-miss search overlaps a mutation, the search can read the old database state, the mutation can commit and execute this clear, and the search's Useful? React with 👍 / 👎. |
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Report whether the owner-search cache is currently available. | ||
| */ | ||
| public CacheStatus ownerSearchStatus() { | ||
| return new CacheStatus(OWNER_SEARCH_CACHE, ownerSearchCache() != null); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the cache used by the owner directory. | ||
| */ | ||
| public Cache ownerSearchCache() { | ||
| return this.cacheManager.getCache(OWNER_SEARCH_CACHE); | ||
| } | ||
|
|
||
| public record CacheStatus(String name, boolean available) { | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2012-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.samples.petclinic.owner; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.samples.petclinic.system.CacheOperations; | ||
|
|
||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| class OwnerDirectorySynchronizerTests { | ||
|
|
||
| @Test | ||
| void ownerAggregateChangesInvalidateCachedSearches() { | ||
| CacheOperations cacheOperations = mock(CacheOperations.class); | ||
| Cache ownerSearchCache = mock(Cache.class); | ||
| given(cacheOperations.ownerSearchCache()).willReturn(ownerSearchCache); | ||
| OwnerDirectorySynchronizer synchronizer = new OwnerDirectorySynchronizer(cacheOperations); | ||
|
|
||
| synchronizer.ownerAggregateChanged(); | ||
|
|
||
| verify(cacheOperations).evict(ownerSearchCache); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each distinct
(lastName, Pageable)supplied toGET /ownersnow creates a permanent cache entry, but the configured Caffeine cache has neither a maximum size nor an expiry policy. Because every entry retains a full page of eager owner/pet/visit aggregates, ordinary varied searches—or requests deliberately using unique search strings and page numbers—can grow the heap without bound until the application is restarted or the entire cache is manually cleared. Configure an eviction limit/TTL or restrict which searches are cached.Useful? React with 👍 / 👎.