Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. package net.seibertmedia.confluence.appcenter.cache;
  2.  
  3. import java.util.List;
  4. import java.util.concurrent.TimeUnit;
  5.  
  6. import com.atlassian.cache.CacheLoader;
  7. import com.atlassian.cache.CacheSettings;
  8. import com.atlassian.cache.CacheSettingsBuilder;
  9. import com.atlassian.confluence.user.ConfluenceUser;
  10.  
  11. import lombok.Builder;
  12. import lombok.Getter;
  13. import lombok.ToString;
  14. import net.seibertmedia.confluence.appcenter.category.CategoryDto;
  15. import net.seibertmedia.confluence.appcenter.dto.AppDto;
  16.  
  17. /**
  18.  * Compilation of all used cache keys in the plugin.
  19.  * If there is a new cache necessary, add the key here.
  20.  */
  21. @Getter
  22. @Builder
  23. @ToString(of = "cacheKey")
  24. public class CacheDefinition<K, V> {
  25.     /**
  26.      * The default uses a local cache, because the current implementations would require a major rewrite of the cache usage.
  27.      * A replication via copy is not possible because lists of DTOs are stored and lists cannot be replicated via copy across a cluster.
  28.      * So to have a caching in cluster the default defines local caches and it defines invalidation messages, if any future cache is defined as remote.
  29.      */
  30.     private static final CacheSettings DEFAULT_CACHE_SETTINGS = new CacheSettingsBuilder()
  31.             .local()
  32.             .replicateViaInvalidation()
  33.             .expireAfterWrite(1, TimeUnit.DAYS)
  34.             .flushable()
  35.             .maxEntries(1000)
  36.             .build();
  37.  
  38.     /**
  39.      * Indicator if cache content is filtered by profile allCaches of a user
  40.      */
  41.     enum ProfileDependentCacheContent {
  42.         YES,
  43.         NO
  44.     }
  45.  
  46.     /**
  47.      * Apps for a specific user (cached by user)
  48.      */
  49.     static final CacheDefinition<ConfluenceUser, List<AppDto>> USER_APPS = new CacheDefinitionBuilder<ConfluenceUser, List<AppDto>>()
  50.             .cacheKey("net.seibertmedia.confluence.appcenter.apps.user.v2")
  51.             .keyType(ConfluenceUser.class)
  52.             .containsApps(true)
  53.             .valueIsDependentOnProfile(ProfileDependentCacheContent.YES)
  54.             .build();
  55.  
  56.     /**
  57.      * Recommended apps for a specific user (cached by user)
  58.      */
  59.     static final CacheDefinition<ConfluenceUser, List<AppDto>> RECOMMENDED_APPS = new CacheDefinitionBuilder<ConfluenceUser, List<AppDto>>()
  60.             .cacheKey("net.seibertmedia.confluence.appcenter.apps.recommended.v2")
  61.             .keyType(ConfluenceUser.class)
  62.             .containsApps(true)
  63.             .valueIsDependentOnProfile(ProfileDependentCacheContent.YES)
  64.             .build();
  65.  
  66.     /**
  67.      * Mandatory apps for a specific user (cached by user)
  68.      */
  69.     static final CacheDefinition<ConfluenceUser, List<AppDto>> MANDATORY_APPS = new CacheDefinitionBuilder<ConfluenceUser, List<AppDto>>()
  70.             .cacheKey("net.seibertmedia.confluence.appcenter.apps.mandatory.v2")
  71.             .keyType(ConfluenceUser.class)
  72.             .containsApps(true)
  73.             .valueIsDependentOnProfile(ProfileDependentCacheContent.YES)
  74.             .build();
  75.  
  76.     /**
  77.      * Visibility restricted apps for a specific user (cached by user)
  78.      */
  79.     static final CacheDefinition<ConfluenceUser, List<AppDto>> VISIBILITY_RESTRICTED_APPS = new CacheDefinitionBuilder<ConfluenceUser, List<AppDto>>()
  80.             .cacheKey("net.seibertmedia.confluence.appcenter.apps.visibility.restricted.v2")
  81.             .keyType(ConfluenceUser.class)
  82.             .containsApps(true)
  83.             .valueIsDependentOnProfile(ProfileDependentCacheContent.YES)
  84.             .build();
  85.  
  86.     /**
  87.      * All apps (constant key)
  88.      */
  89.     static final CacheDefinition<String, List<AppDto>> ALL_APPS = new CacheDefinitionBuilder<String, List<AppDto>>()
  90.             .cacheKey("net.seibertmedia.confluence.appcenter.allapps.v2")
  91.             .keyType(String.class)
  92.             .containsApps(true)
  93.             .valueIsDependentOnProfile(ProfileDependentCacheContent.NO)
  94.             .cacheSettings(new CacheSettingsBuilder(DEFAULT_CACHE_SETTINGS).maxEntries(1).build())
  95.             .build();
  96.  
  97.     /**
  98.      * All categories (cached by their ID)
  99.      */
  100.     static final CacheDefinition<Long, CategoryDto> CATEGORIES = new CacheDefinitionBuilder<Long, CategoryDto>()
  101.             .cacheKey("net.seibertmedia.confluence.appcenter.categories.v2")
  102.             .keyType(Long.class)
  103.             .valueIsDependentOnProfile(ProfileDependentCacheContent.NO)
  104.             .build();
  105.  
  106.     /**
  107.      * App icons (cached by app id)
  108.      * Still requires a {@link CacheLoader} to be set.
  109.      * Because this cache defines a loader, the cluster cache may be remote, since each node can load the app icon itself without using <code>put</code>.
  110.      */
  111.     static final CacheDefinitionBuilder<String, byte[]> APP_ICONS_CACHE_BUILDER = new CacheDefinitionBuilder<String, byte[]>()
  112.             .cacheKey("net.seibertmedia.confluence.appcenter.apps.icons.v2")
  113.             .keyType(String.class)
  114.             .valueIsDependentOnProfile(ProfileDependentCacheContent.NO)
  115.             .cacheSettings(new CacheSettingsBuilder(DEFAULT_CACHE_SETTINGS)
  116.                     .remote()
  117.                     .maxEntries(500)
  118.                     .build());
  119.  
  120.     private final String cacheKey;
  121.     private final Class<K> keyType;
  122.     private final boolean containsApps;
  123.     private final ProfileDependentCacheContent valueIsDependentOnProfile;
  124.     private final CacheLoader<K, V> cacheLoader;
  125.     @Builder.Default
  126.     private CacheSettings cacheSettings = DEFAULT_CACHE_SETTINGS;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement