Skip to content

Commit

Permalink
Ability to view micronaut-cache value from key/parameter (#647)
Browse files Browse the repository at this point in the history
* Implement new endpoint to retrieve a cache entry

* Add tests for the new endpoint

* Add documentation
  • Loading branch information
guillermocalvo authored Aug 16, 2023
1 parent 594be28 commit 6e7b507
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ public Mono<Map<String, Object>> getCache(@NotBlank @Selector String name) {
.onErrorResume((Throwable e) -> Mono.empty());
}

/**
* Returns a cache entry as a {@link Mono}, given a cache and a key within the provided cache.
*
* @param name The name of the cache
* @param key the key within the cache to retrieve
* @return The cache entry as a {@link Mono}
*/
@Read
public Mono<Map<String, Object>> getCacheEntry(@NotBlank @Selector String name, @NotBlank @Selector String key) {
return Mono.just(name)
.map(cacheManager::getCache)
.map(SyncCache::async)
.map(asyncCache -> asyncCache.get(key, Object.class))
.flatMap(Mono::fromFuture)
.flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))
.map(value -> Map.of("key", key, "value", value))
.onErrorResume(e -> Mono.empty());
}

/**
* Invalidates all the caches.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.micronaut.context.env.Environment
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpStatus
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.Specification

Expand Down Expand Up @@ -117,6 +118,22 @@ class CachesEndpointSpec extends Specification {
result.caffeine.stats.evictionCount == 0
result.caffeine.stats.evictionWeight == 0

when:
response = client.toBlocking().exchange("/caches/foo-cache/foo1", Map)
result = response.body()

then:
response.code() == HttpStatus.OK.code
result.key == "foo1"
result.value == "value1"

when:
client.toBlocking().exchange("/caches/foo-cache/no-such-key", Map)

then:
HttpClientResponseException error = thrown()
error.status == HttpStatus.NOT_FOUND

cleanup:
client.close()
embeddedServer?.close()
Expand Down
8 changes: 8 additions & 0 deletions src/main/docs/guide/endpoint.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ example, to access the configuration of the cache 'book-cache':
$ curl http://localhost:8080/caches/book-cache
----

To retrieve a specific cache entry within a single cache, include both cache name and key in your GET request.
For example, to access the entry under key '123' in cache 'book-cache':

[source,bash]
----
$ curl http://localhost:8080/caches/book-cache/123
----

To invalidate a specific cache entry within a single cache, send a DELETE request to the named cache URL with the desired key.

NOTE: This only works for caches which have keys of type `String`.
Expand Down

0 comments on commit 6e7b507

Please sign in to comment.