001/** 002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.components.vault.jcache; 017 018import static java.util.concurrent.TimeUnit.SECONDS; 019 020import javax.cache.configuration.Configuration; 021import javax.cache.configuration.FactoryBuilder; 022import javax.cache.configuration.MutableCacheEntryListenerConfiguration; 023import javax.cache.configuration.MutableConfiguration; 024import javax.cache.expiry.CreatedExpiryPolicy; 025import javax.cache.expiry.Duration; 026import javax.enterprise.context.ApplicationScoped; 027import javax.inject.Inject; 028 029import org.eclipse.microprofile.config.inject.ConfigProperty; 030import org.talend.sdk.components.vault.configuration.Documentation; 031 032@ApplicationScoped 033public class CacheConfigurationFactory { 034 035 @Inject 036 @Documentation("Should JCache MBeans be registered.") 037 @ConfigProperty(name = "talend.vault.cache.jcache.cache.management", defaultValue = "true") 038 private Boolean cacheManagement; 039 040 @Inject 041 @Documentation("Should JCache statistics be enabled.") 042 @ConfigProperty(name = "talend.vault.cache.jcache.cache.statistics", defaultValue = "true") 043 private Boolean cacheStatistics; 044 045 @Inject 046 @Documentation("JCache expiry for decrypted values (ms).") 047 @ConfigProperty(name = "talend.vault.cache.jcache.cache.expiry", defaultValue = "3600") 048 private Long cacheExpiry; 049 050 @Inject 051 @Documentation("JCache max size per cache.") 052 @ConfigProperty(name = "talend.vault.cache.jcache.maxCacheSize", defaultValue = "100000") 053 private Integer maxCacheSize; // not strict constraint - for perf - but we ensure it is bound to avoid issues 054 055 public <K, T> Configuration<K, T> createConfiguration(final CacheSizeManager<K, T> listener) { 056 return new MutableConfiguration<K, T>() 057 .setStoreByValue(false) 058 .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(SECONDS, cacheExpiry))) 059 .setManagementEnabled(cacheManagement) 060 .setStatisticsEnabled(cacheStatistics) 061 .addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration<>( 062 new FactoryBuilder.SingletonFactory<>(listener), null, false, false)); 063 } 064 065 public int maxSize() { 066 return maxCacheSize; 067 } 068}