Search in sources :

Example 1 with IdMCacheConfiguration

use of eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration in project CzechIdMng by bcvsolutions.

the class DefaultIdmCacheManager method cacheValue.

@Override
public boolean cacheValue(String cacheName, Object key, Object value) {
    Assert.hasText(cacheName, EMPTY_NAME_MSG);
    Assert.notNull(key, EMPTY_KEY_MSG);
    // 
    final Cache<Object, Object> cache = jCacheManager.getCache(cacheName);
    final IdMCacheConfiguration configuration = cacheConfigurations.get(cacheName);
    // We can cast here safely, because DistributedIdMCacheConfiguration only allows Serializable types
    final Object toCache = isConfigLocalOnly(configuration) ? new CacheObjectWrapper<>(value) : new SerializableCacheObjectWrapper<>((Serializable) value);
    if (cache != null) {
        cache.put(key, toCache);
    }
    return cache != null && Objects.equals(cache.get(key), toCache);
}
Also used : Serializable(java.io.Serializable) IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration)

Example 2 with IdMCacheConfiguration

use of eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration in project CzechIdMng by bcvsolutions.

the class DefaultIdmCacheManager method getValue.

@Override
public ValueWrapper getValue(String cacheName, Object key) {
    Assert.hasText(cacheName, EMPTY_NAME_MSG);
    Assert.notNull(key, EMPTY_KEY_MSG);
    // 
    Cache<Object, Object> cache = jCacheManager.getCache(cacheName);
    IdMCacheConfiguration cacheConfiguration = cacheConfigurations.get(cacheName);
    // 
    if (cache == null) {
        return null;
    }
    // 
    return toValueWrapper(cacheConfiguration, cache.get(key));
}
Also used : IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration)

Example 3 with IdMCacheConfiguration

use of eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration in project CzechIdMng by bcvsolutions.

the class ClusteredEhCacheConfiguration method ehCacheManager.

/**
 * Defines clustered {@link CacheManager} using Terracotta server.
 *
 * @param terracotaUrl a list of IP addresses with ports (IP_ADDR:PORT)
 * @param terracotaResourceName name of server resource to connect
 * @param terracotaResourcePoolName name od server resource pool name
 * @param terracotaResourcePoolSize size of server resource pool in MB
 * @param idMCacheConfigurations a list of {@link IdMCacheConfiguration} defined in container
 * @return CacheManager with distributed capabilities
 */
@Bean
@Qualifier("jCacheManager")
@ConditionalOnProperty(value = TERRACOTA_URL_PROPERTY)
@ConditionalOnMissingBean
public CacheManager ehCacheManager(@Value("${" + TERRACOTA_URL_PROPERTY + "}") String terracotaUrl, @Value("${" + TERRACOTA_RESOURCE_NAME_PROPERTY + "}") String terracotaResourceName, @Value("${" + TERRACOTA_RESOURCE_POOL_NAME_PROPERTY + "}") String terracotaResourcePoolName, @Value("${" + TERRACOTA_RESOURCE_POOL_SIZE_PROPERTY + "}") int terracotaResourcePoolSize, @Autowired List<IdMCacheConfiguration> idMCacheConfigurations) {
    CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder().with(ClusteringServiceConfigurationBuilder.cluster(parseServerAddresses(terracotaUrl), "default").autoCreate(server -> server.defaultServerResource(terracotaResourceName).resourcePool(terracotaResourcePoolName, terracotaResourcePoolSize, MemoryUnit.MB, terracotaResourceName))).withSerializer(CacheObjectWrapper.class, CacheWrapperSerializer.class).withSerializer(SerializableCacheObjectWrapper.class, SerializableCacheWrapperSerializer.class);
    PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true);
    // create caches using IdMCacheConfiguration instances
    if (!CollectionUtils.isEmpty(idMCacheConfigurations)) {
        for (IdMCacheConfiguration config : idMCacheConfigurations) {
            cacheManager.createCache(config.getCacheName(), toConcreteConfiguration(config, terracotaResourcePoolName));
        }
    }
    // get CacheManager (Jcache) with above updated configuration
    final EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider();
    return ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), cacheManager.getRuntimeConfiguration());
}
Also used : Arrays(java.util.Arrays) ExpiryPolicyBuilder(org.ehcache.config.builders.ExpiryPolicyBuilder) ResourcePoolsBuilder(org.ehcache.config.builders.ResourcePoolsBuilder) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheConfiguration(org.ehcache.config.CacheConfiguration) Value(org.springframework.beans.factory.annotation.Value) ClusteringServiceConfigurationBuilder(org.ehcache.clustered.client.config.builders.ClusteringServiceConfigurationBuilder) EhcacheCachingProvider(org.ehcache.jsr107.EhcacheCachingProvider) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Order(org.springframework.core.annotation.Order) MemoryUnit(org.ehcache.config.units.MemoryUnit) SerializableCacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.SerializableCacheObjectWrapper) CacheManagerBuilder(org.ehcache.config.builders.CacheManagerBuilder) Caching(javax.cache.Caching) IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration) PersistentCacheManager(org.ehcache.PersistentCacheManager) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ClusteredResourcePoolBuilder(org.ehcache.clustered.client.config.builders.ClusteredResourcePoolBuilder) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) CacheConfigurationBuilder(org.ehcache.config.builders.CacheConfigurationBuilder) CollectionUtils(org.springframework.util.CollectionUtils) CacheManager(javax.cache.CacheManager) Bean(org.springframework.context.annotation.Bean) CacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.CacheObjectWrapper) SerializableCacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.SerializableCacheObjectWrapper) CacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.CacheObjectWrapper) PersistentCacheManager(org.ehcache.PersistentCacheManager) IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration) EhcacheCachingProvider(org.ehcache.jsr107.EhcacheCachingProvider) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 4 with IdMCacheConfiguration

use of eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration in project CzechIdMng by bcvsolutions.

the class InMemoryEhCacheConfiguration method ehCacheManager.

/**
 * Defines in-memory cache manager.
 *
 * @param idMCacheConfigurations {@link List} of {@link IdMCacheConfiguration} defined in container
 * @return CacheManager with on-heap capabilities
 */
@Bean
@Qualifier("jCacheManager")
@ConditionalOnMissingBean
public CacheManager ehCacheManager(@Autowired List<IdMCacheConfiguration> idMCacheConfigurations) {
    CacheManagerBuilder<?> localCacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder();
    if (!CollectionUtils.isEmpty(idMCacheConfigurations)) {
        for (IdMCacheConfiguration config : idMCacheConfigurations) {
            localCacheManagerBuilder = localCacheManagerBuilder.withCache(config.getCacheName(), toConcreteConfiguration(config));
        }
    }
    // get CacheManager (Jcache) with above updated configuration
    final EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider();
    return ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), localCacheManagerBuilder.build(true).getRuntimeConfiguration());
}
Also used : IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration) EhcacheCachingProvider(org.ehcache.jsr107.EhcacheCachingProvider) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

IdMCacheConfiguration (eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration)4 EhcacheCachingProvider (org.ehcache.jsr107.EhcacheCachingProvider)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 Bean (org.springframework.context.annotation.Bean)2 CacheObjectWrapper (eu.bcvsolutions.idm.core.api.config.cache.domain.CacheObjectWrapper)1 SerializableCacheObjectWrapper (eu.bcvsolutions.idm.core.api.config.cache.domain.SerializableCacheObjectWrapper)1 Serializable (java.io.Serializable)1 InetSocketAddress (java.net.InetSocketAddress)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 CacheManager (javax.cache.CacheManager)1 Caching (javax.cache.Caching)1 PersistentCacheManager (org.ehcache.PersistentCacheManager)1 ClusteredResourcePoolBuilder (org.ehcache.clustered.client.config.builders.ClusteredResourcePoolBuilder)1 ClusteringServiceConfigurationBuilder (org.ehcache.clustered.client.config.builders.ClusteringServiceConfigurationBuilder)1 CacheConfiguration (org.ehcache.config.CacheConfiguration)1 CacheConfigurationBuilder (org.ehcache.config.builders.CacheConfigurationBuilder)1 CacheManagerBuilder (org.ehcache.config.builders.CacheManagerBuilder)1