protected MetadataResolverCacheQueryResult locateAndCacheMetadataResolver(
        final SamlRegisteredService service,
        final CriteriaSet criteriaSet,
        final SamlRegisteredServiceCacheKey cacheKey) {

        val cachedEntry = cache
            .asMap()
            .values()
            .stream()
            .filter(Objects::nonNull)
            .map(Unchecked.function(res -> {
                val entity = res.getMetadataResolver().resolveSingle(criteriaSet);
                return Optional.ofNullable(entity)
                    .map(e -> MetadataResolverCacheQueryResult.builder().result(res).entityDescriptor(Optional.of(e)).build());
            }))
            .filter(Optional::isPresent)
            .flatMap(Optional::stream)
            .filter(MetadataResolverCacheQueryResult::isValid)
            .findFirst();

        if (cachedEntry.isPresent() && cachedEntry.get().isValid()) {
            return cachedEntry.get();
        }
        LOGGER.debug("Loading metadata resolver from the cache using [{}]", cacheKey.getCacheKey());
        val cacheResult = Objects.requireNonNull(cache.get(cacheKey));
        LOGGER.debug("Loaded and cached SAML metadata [{}] from [{}]",
            cacheResult.getMetadataResolver().getId(), service.getMetadataLocation());
        Assert.isTrue(cacheResult.isResolved(), "Metadata resolver cannot be found from the cache for " + service.getName());

        return MetadataResolverCacheQueryResult
            .builder()
            .entityDescriptor(Optional.empty())
            .result(cacheResult)
            .build();
    }
An adaptation of metadata resolver which handles the resolution of metadata resources
inside a cache. It basically is a fancy wrapper around a cache, and constructs the cache
semantics before processing the resolution of metadata for a SAML service.

@author Misagh Moayyed
@since 5.0.0
support/cas-server-support-saml-idp-metadata/src/main/java/org/apereo/cas/support/saml/services/idp/metadata/cache/SamlRegisteredServiceDefaultCachingMetadataResolver.java