use of io.quarkus.cache.CacheResult in project mega by Gepardec.
the class ZepServiceImpl method getAbsenceForEmployee.
@CacheResult(cacheName = "fehlzeitentype")
@Override
public List<FehlzeitType> getAbsenceForEmployee(Employee employee, LocalDate date) {
final ReadFehlzeitRequestType fehlzeitenRequest = new ReadFehlzeitRequestType();
fehlzeitenRequest.setRequestHeader(zepSoapProvider.createRequestHeaderType());
final Optional<ReadFehlzeitSearchCriteriaType> searchCriteria = getSearchCriteria(employee, date, this::createAbsenceSearchCriteria);
if (searchCriteria.isEmpty()) {
return null;
}
fehlzeitenRequest.setReadFehlzeitSearchCriteria(searchCriteria.get());
ReadFehlzeitResponseType fehlzeitResponseType = zepSoapPortType.readFehlzeit(fehlzeitenRequest);
if (fehlzeitResponseType != null && fehlzeitResponseType.getFehlzeitListe() != null && fehlzeitResponseType.getFehlzeitListe().getFehlzeit() != null) {
return fehlzeitResponseType.getFehlzeitListe().getFehlzeit();
}
return Collections.emptyList();
}
use of io.quarkus.cache.CacheResult in project quarkus by quarkusio.
the class CacheResultInterceptor method intercept.
@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Throwable {
/*
* io.smallrye.mutiny.Multi values are never cached.
* There's already a WARN log entry at build time so we don't need to log anything at run time.
*/
if (Multi.class.isAssignableFrom(invocationContext.getMethod().getReturnType())) {
return invocationContext.proceed();
}
CacheInterceptionContext<CacheResult> interceptionContext = getInterceptionContext(invocationContext, CacheResult.class, true);
if (interceptionContext.getInterceptorBindings().isEmpty()) {
// This should never happen.
LOGGER.warn(INTERCEPTOR_BINDING_ERROR_MSG);
return invocationContext.proceed();
}
CacheResult binding = interceptionContext.getInterceptorBindings().get(0);
AbstractCache cache = (AbstractCache) cacheManager.getCache(binding.cacheName()).get();
Object key = getCacheKey(cache, interceptionContext.getCacheKeyParameterPositions(), invocationContext.getParameters());
LOGGER.debugf("Loading entry with key [%s] from cache [%s]", key, binding.cacheName());
try {
ReturnType returnType = determineReturnType(invocationContext.getMethod().getReturnType());
if (returnType != ReturnType.NonAsync) {
Uni<Object> cacheValue = cache.get(key, new Function<Object, Object>() {
@Override
public Object apply(Object k) {
LOGGER.debugf("Adding %s entry with key [%s] into cache [%s]", UnresolvedUniValue.class.getSimpleName(), key, binding.cacheName());
return UnresolvedUniValue.INSTANCE;
}
}).onItem().transformToUni(new Function<Object, Uni<?>>() {
@Override
public Uni<?> apply(Object value) {
if (value == UnresolvedUniValue.INSTANCE) {
try {
return asyncInvocationResultToUni(invocationContext.proceed(), returnType).call(new Function<Object, Uni<?>>() {
@Override
public Uni<?> apply(Object emittedValue) {
return cache.replaceUniValue(key, emittedValue);
}
});
} catch (CacheException e) {
throw e;
} catch (Exception e) {
throw new CacheException(e);
}
} else {
return Uni.createFrom().item(value);
}
}
});
if (binding.lockTimeout() <= 0) {
return createAsyncResult(cacheValue, returnType);
}
cacheValue = cacheValue.ifNoItem().after(Duration.ofMillis(binding.lockTimeout())).recoverWithUni(new Supplier<Uni<?>>() {
@Override
public Uni<?> get() {
try {
return asyncInvocationResultToUni(invocationContext.proceed(), returnType);
} catch (CacheException e) {
throw e;
} catch (Exception e) {
throw new CacheException(e);
}
}
});
return createAsyncResult(cacheValue, returnType);
} else {
Uni<Object> cacheValue = cache.get(key, new Function<Object, Object>() {
@Override
public Object apply(Object k) {
try {
LOGGER.debugf("Adding entry with key [%s] into cache [%s]", key, binding.cacheName());
return invocationContext.proceed();
} catch (CacheException e) {
throw e;
} catch (Throwable e) {
throw new CacheException(e);
}
}
});
Object value;
if (binding.lockTimeout() <= 0) {
value = cacheValue.await().indefinitely();
} else {
try {
/*
* If the current thread started the cache value computation, then the computation is already finished
* since
* it was done synchronously and the following call will never time out.
*/
value = cacheValue.await().atMost(Duration.ofMillis(binding.lockTimeout()));
} catch (TimeoutException e) {
// TODO: Add statistics here to monitor the timeout.
return invocationContext.proceed();
}
}
return value;
}
} catch (CacheException e) {
if (e.getCause() != null) {
throw e.getCause();
} else {
throw e;
}
}
}
use of io.quarkus.cache.CacheResult in project registry.quarkus.io by quarkusio.
the class MetadataContentProvider method generateMetadata.
@CacheResult(cacheName = CacheNames.METADATA)
Metadata generateMetadata(ArtifactCoords artifact) {
Metadata newMetadata = new Metadata();
newMetadata.setGroupId(artifact.getGroupId());
newMetadata.setArtifactId(artifact.getArtifactId());
newMetadata.setVersion(artifact.getVersion());
Versioning versioning = new Versioning();
newMetadata.setVersioning(versioning);
Date lastUpdated = mavenCacheState.getLastUpdate();
versioning.setLastUpdatedTimestamp(lastUpdated);
Snapshot snapshot = new Snapshot();
versioning.setSnapshot(snapshot);
snapshot.setTimestamp(versioning.getLastUpdated().substring(0, 8) + "." + versioning.getLastUpdated().substring(8));
snapshot.setBuildNumber(1);
final String baseVersion = artifact.getVersion().substring(0, artifact.getVersion().length() - "SNAPSHOT".length());
addSnapshotVersion(versioning, snapshot, baseVersion, "pom", EMPTY_CLASSIFIER);
addSnapshotVersion(versioning, snapshot, baseVersion, "json", EMPTY_CLASSIFIER);
addSnapshotVersion(versioning, snapshot, baseVersion, "json", PlatformRelease.findQuarkusCores());
return newMetadata;
}
use of io.quarkus.cache.CacheResult in project registry.quarkus.io by quarkusio.
the class RegistryDescriptorContentProvider method provide.
@Override
@CacheResult(cacheName = CacheNames.DESCRIPTOR)
public Response provide(ArtifactCoords artifact, UriInfo uriInfo) throws Exception {
StringWriter sw = new StringWriter();
RegistryConfig registryConfig = getRegistryConfig(uriInfo);
RegistriesConfigMapperHelper.toJson(registryConfig, sw);
String result = sw.toString();
final String checksumSuffix = ArtifactParser.getChecksumSuffix(uriInfo.getPathSegments(), artifact);
String contentType = MediaType.APPLICATION_JSON;
if (ArtifactParser.SUFFIX_MD5.equals(checksumSuffix)) {
result = HashUtil.md5(result);
contentType = MediaType.TEXT_PLAIN;
} else if (ArtifactParser.SUFFIX_SHA1.equals(checksumSuffix)) {
result = HashUtil.sha1(result);
contentType = MediaType.TEXT_PLAIN;
}
return Response.ok(result).header(HttpHeaders.CONTENT_TYPE, contentType).build();
}
use of io.quarkus.cache.CacheResult in project registry.quarkus.io by quarkusio.
the class RepositoryMetadataResource method handleRepositoryMetadataRequest.
@GET
@Path("repository-metadata.{extension}")
@Operation(hidden = true)
@CacheResult(cacheName = CacheNames.METADATA)
public Response handleRepositoryMetadataRequest(@PathParam("extension") String extension) throws IOException {
RepositoryMetadata repositoryMetadata = new RepositoryMetadata();
repositoryMetadata.setVersion(RepositoryMetadata.MODEL_VERSION);
repositoryMetadata.setId(mavenConfig.getRegistryId());
repositoryMetadata.setUrl(mavenConfig.getRegistryUrl());
repositoryMetadata.setLayout(RepositoryMetadata.LAYOUT_MAVEN2);
repositoryMetadata.setPolicy(RepositoryMetadata.POLICY_SNAPSHOT);
String contentType = MediaType.APPLICATION_XML;
String content = writeMetadata(repositoryMetadata);
if (extension.endsWith(ArtifactParser.SUFFIX_SHA1)) {
content = HashUtil.sha1(content);
contentType = MediaType.TEXT_PLAIN;
}
return Response.ok(content).header(HttpHeaders.CONTENT_TYPE, contentType).build();
}
Aggregations