Refactor caches (#2775)

* added static usage of cache in order access it easier
* added mandatory (in hawkbit-core) registration - always tenant aware caches shall be used - hawkbit depends on it
* added per cache and tenant name configuration
* (not really realted to caches) but in order to be easier evicted entities after commit handlers are now statically accessed

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-10-24 16:42:40 +03:00
committed by GitHub
parent dc8211c64b
commit 98daa696de
48 changed files with 380 additions and 670 deletions

View File

@@ -1,71 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.autoconfigure.cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.eclipse.hawkbit.tenancy.cache.TenantCacheManager;
import org.eclipse.hawkbit.tenancy.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* A configuration for configuring the spring {@link CacheManager} for specific multi-tenancy caching. The caches between
* tenants must not interfere each other.
* <p/>
* This is done by providing a special {@link TenantCacheManager} which generates a cache name included the current tenant.
*/
@Configuration
@EnableCaching
public class CacheAutoConfiguration {
/**
* @return the default cache manager bean if none other cache manager is existing.
*/
@Bean
@ConditionalOnMissingBean
@Primary
TenantCacheManager cacheManager(
@Qualifier("directCacheManager") final CacheManager directCacheManager, final TenantAware tenantAware) {
return new TenantAwareCacheManager(directCacheManager, tenantAware);
}
/**
* A separate configuration of the direct cache manager for the {@link TenantAwareCacheManager} that it can get overridden by another
* configuration.
*/
@Configuration
@EnableConfigurationProperties(CacheProperties.class)
static class DirectCacheManagerConfiguration {
/**
* @return the direct cache manager to access without tenant aware check, cause in sometimes it's necessary to access the cache
* directly without having the current tenant, e.g. initial creation of tenant
*/
@Bean(name = "directCacheManager")
@ConditionalOnMissingBean(name = "directCacheManager")
public CacheManager directCacheManager(final CacheProperties cacheProperties) {
final CaffeineCacheManager cacheManager = new CaffeineCacheManager();
if (cacheProperties.getTtl() > 0) {
cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(cacheProperties.getTtl(), cacheProperties.getTtlUnit()));
}
return cacheManager;
}
}
}

View File

@@ -1,38 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.autoconfigure.cache;
import java.util.concurrent.TimeUnit;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties for configuring the cache within a cluster. The TTL (time to live) is used for the lifetime limit of data in caches.
* After lifetime the data gets reloaded out of the database.
*/
@Data
@ConfigurationProperties("hawkbit.cache")
public class CacheProperties {
/**
* TTL for cached entries in millis.
*/
private int ttl = 10_000; // default TTL - 10 seconds
/**
* Initial delay in millis
*/
private int initialDelay;
public final TimeUnit getTtlUnit() {
return TimeUnit.MILLISECONDS;
}
}

View File

@@ -1,5 +1,4 @@
org.eclipse.hawkbit.autoconfigure.artifact.PropertyBasedArtifactUrlResolverAutoConfiguration
org.eclipse.hawkbit.autoconfigure.cache.CacheAutoConfiguration
org.eclipse.hawkbit.autoconfigure.repository.event.EventPublisherAutoConfiguration
org.eclipse.hawkbit.autoconfigure.repository.JpaRepositoryAutoConfiguration
org.eclipse.hawkbit.autoconfigure.scheduling.AsyncConfigurerAutoConfiguration