All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.telicent.attribute.store.CachedAttributeStore Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Copyright (C) Telicent Ltd
 *
 * 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
 *
 * http://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 io.telicent.attribute.store;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.telicent.jena.abac.AttributeValueSet;
import io.telicent.jena.abac.Hierarchy;
import io.telicent.jena.abac.attributes.Attribute;
import io.telicent.jena.abac.core.AttributesStore;

import java.time.Duration;
import java.util.Set;

/**
 * Simple cached wrapper around existing Attribute Store.
 */
public class CachedAttributeStore implements AttributesStore {
    final Cache userAttributeCache;
    final Cache hierarchyCache;
    final AttributesStore underlyingStore;

    /**
     * Create new Cached Attribute Store
     * @param underlyingStore Attribute store to reference
     * @param cacheExpiryTime How long to hold cached entries
     */
    public CachedAttributeStore(AttributesStore underlyingStore, Duration cacheExpiryTime) {
        this.underlyingStore = underlyingStore;
        userAttributeCache = Caffeine.newBuilder().recordStats().expireAfterWrite(cacheExpiryTime).build();
        hierarchyCache = Caffeine.newBuilder().recordStats().expireAfterWrite(cacheExpiryTime).build();
    }

    @Override
    public AttributeValueSet attributes(String user) {
        return userAttributeCache.get(user, underlyingStore::attributes);
    }

    @Override
    public Set users() {
        return underlyingStore.users();
    }

    @Override
    public boolean hasHierarchy(Attribute attribute) {
        Hierarchy hierarchy = getHierarchy(attribute);
        return hierarchy != null && !hierarchy.values().isEmpty();
    }

    @Override
    public Hierarchy getHierarchy(Attribute attribute) {
        return hierarchyCache.get(attribute, underlyingStore::getHierarchy);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy