Search in sources :

Example 1 with CacheResult

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();
}
Also used : ReadFehlzeitSearchCriteriaType(de.provantis.zep.ReadFehlzeitSearchCriteriaType) ReadFehlzeitResponseType(de.provantis.zep.ReadFehlzeitResponseType) ReadFehlzeitRequestType(de.provantis.zep.ReadFehlzeitRequestType) CacheResult(io.quarkus.cache.CacheResult)

Example 2 with CacheResult

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;
        }
    }
}
Also used : CacheException(io.quarkus.cache.CacheException) TimeoutException(io.smallrye.mutiny.TimeoutException) CacheException(io.quarkus.cache.CacheException) Uni(io.smallrye.mutiny.Uni) Function(java.util.function.Function) CacheResult(io.quarkus.cache.CacheResult) Supplier(java.util.function.Supplier) TimeoutException(io.smallrye.mutiny.TimeoutException) AroundInvoke(javax.interceptor.AroundInvoke)

Example 3 with CacheResult

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;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Date(java.util.Date) CacheResult(io.quarkus.cache.CacheResult)

Example 4 with CacheResult

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();
}
Also used : RegistryConfig(io.quarkus.registry.config.RegistryConfig) StringWriter(java.io.StringWriter) CacheResult(io.quarkus.cache.CacheResult)

Example 5 with CacheResult

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();
}
Also used : RepositoryMetadata(org.sonatype.nexus.repository.metadata.model.RepositoryMetadata) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) CacheResult(io.quarkus.cache.CacheResult) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Aggregations

CacheResult (io.quarkus.cache.CacheResult)10 ReadFehlzeitRequestType (de.provantis.zep.ReadFehlzeitRequestType)2 ReadFehlzeitResponseType (de.provantis.zep.ReadFehlzeitResponseType)2 ReadFehlzeitSearchCriteriaType (de.provantis.zep.ReadFehlzeitSearchCriteriaType)2 RegistryConfig (io.quarkus.registry.config.RegistryConfig)2 Employee (com.gepardec.mega.domain.model.Employee)1 Project (com.gepardec.mega.domain.model.Project)1 ProjectEntry (com.gepardec.mega.domain.model.monthlyreport.ProjectEntry)1 DateUtils (com.gepardec.mega.domain.utils.DateUtils)1 DateUtils.getFirstDayOfCurrentMonth (com.gepardec.mega.domain.utils.DateUtils.getFirstDayOfCurrentMonth)1 DateUtils.getLastDayOfCurrentMonth (com.gepardec.mega.domain.utils.DateUtils.getLastDayOfCurrentMonth)1 EmployeeMapper (com.gepardec.mega.service.mapper.EmployeeMapper)1 ProjectEntryMapper (com.gepardec.mega.zep.mapper.ProjectEntryMapper)1 User (com.redhat.cloud.notifications.recipients.User)1 ITUserRequest (com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest)1 ITUserResponse (com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)1 FehlzeitType (de.provantis.zep.FehlzeitType)1 KategorieListeType (de.provantis.zep.KategorieListeType)1 KategorieType (de.provantis.zep.KategorieType)1 MitarbeiterType (de.provantis.zep.MitarbeiterType)1