|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev) |
| 3 | + * Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/cache-manager/blob/main/LICENSE) |
| 4 | + * |
| 5 | + * PMD False Positives: |
| 6 | + * - CognitiveComplexity: It is a library and we tried to put everything into ONE class |
| 7 | + * - PropertyNamingConventions: It was intentional to make the lib more fluent and readable |
| 8 | + * - FieldNamingConventions: It was intentional to make the lib more fluent and readable |
| 9 | +**/ |
| 10 | +@SuppressWarnings('PMD.CognitiveComplexity, PMD.PropertyNamingConventions, PMD.FieldNamingConventions') |
| 11 | +public with sharing class CacheManager { |
| 12 | + public interface Cacheable { |
| 13 | + Boolean contains(String key); |
| 14 | + Set<String> getKeys(); |
| 15 | + Object get(String key); |
| 16 | + void put(String key, Object value); |
| 17 | + void remove(String key); |
| 18 | + } |
| 19 | + |
| 20 | + public final static Cacheable ApexTransaction = new ApexTransactionCache(); |
| 21 | + |
| 22 | + public final static Cacheable SOQLOrgCache { |
| 23 | + get { return getOrgCache('SOQL'); } |
| 24 | + } |
| 25 | + |
| 26 | + public final static Cacheable SOQLSessionCache { |
| 27 | + get { return getSessionCache('SOQL'); } |
| 28 | + } |
| 29 | + |
| 30 | + // Implementation |
| 31 | + |
| 32 | + private enum CacheType { ORG, SESSION } |
| 33 | + |
| 34 | + private final static Map<CacheType, Map<String, Cacheable>> CACHE_MAP = new Map<CacheType, Map<String, Cacheable>>{ |
| 35 | + CacheType.ORG => new Map<String, Cacheable>(), |
| 36 | + CacheType.SESSION => new Map<String, Cacheable>() |
| 37 | + }; |
| 38 | + |
| 39 | + private static Cacheable getOrgCache(String partitionName) { |
| 40 | + if (!CACHE_MAP.get(CacheType.ORG).containsKey(partitionName)) { |
| 41 | + CACHE_MAP.get(CacheType.ORG).put(partitionName, new OrgPlatformCache(partitionName)); |
| 42 | + } |
| 43 | + return CACHE_MAP.get(CacheType.ORG).get(partitionName); |
| 44 | + } |
| 45 | + |
| 46 | + private static Cacheable getSessionCache(String partitionName) { |
| 47 | + if (!CACHE_MAP.get(CacheType.SESSION).containsKey(partitionName)) { |
| 48 | + CACHE_MAP.get(CacheType.SESSION).put(partitionName, new SessionPlatformCache(partitionName)); |
| 49 | + } |
| 50 | + return CACHE_MAP.get(CacheType.SESSION).get(partitionName); |
| 51 | + } |
| 52 | + |
| 53 | + private static void validateKey(String key) { |
| 54 | + if (!Pattern.compile('^[a-zA-Z0-9]+$').matcher(key).matches()) { |
| 55 | + throw new IllegalArgumentException('Key must be alphanumeric, received key: ' + key); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private class ApexTransactionCache implements Cacheable { |
| 60 | + private final Map<String, Object> TRANSACTION_CACHE = new Map<String, Object>(); |
| 61 | + |
| 62 | + public Boolean contains(String key) { |
| 63 | + return this.TRANSACTION_CACHE.containsKey(key); |
| 64 | + } |
| 65 | + |
| 66 | + public Set<String> getKeys() { |
| 67 | + return TRANSACTION_CACHE.keySet(); |
| 68 | + } |
| 69 | + |
| 70 | + public Object get(String key) { |
| 71 | + return this.TRANSACTION_CACHE.get(key); |
| 72 | + } |
| 73 | + |
| 74 | + public void put(String key, Object value) { |
| 75 | + validateKey(key); |
| 76 | + this.TRANSACTION_CACHE.put(key, value); |
| 77 | + } |
| 78 | + |
| 79 | + public void remove(String key) { |
| 80 | + this.TRANSACTION_CACHE.remove(key); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private abstract class PlatformCache implements Cacheable { |
| 85 | + private Cache.Partition platformCachePartition; |
| 86 | + |
| 87 | + public PlatformCache(String partitionName) { |
| 88 | + this.platformCachePartition = getPartition(partitionName); |
| 89 | + } |
| 90 | + |
| 91 | + protected abstract Cache.Partition getPartition(String partitionName); |
| 92 | + |
| 93 | + public Boolean contains(String key) { |
| 94 | + return this.platformCachePartition.contains(key); |
| 95 | + } |
| 96 | + |
| 97 | + public Set<String> getKeys() { |
| 98 | + return this.platformCachePartition.getKeys(); |
| 99 | + } |
| 100 | + |
| 101 | + public Object get(String key) { |
| 102 | + return this.platformCachePartition.get(key); |
| 103 | + } |
| 104 | + |
| 105 | + public void remove(String key) { |
| 106 | + this.platformCachePartition.remove(key); |
| 107 | + } |
| 108 | + |
| 109 | + public void put(String key, Object value) { |
| 110 | + validateKey(key); |
| 111 | + this.platformCachePartition.put(key, value); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private class OrgPlatformCache extends PlatformCache { |
| 116 | + public OrgPlatformCache(String partitionName) { |
| 117 | + super(partitionName); |
| 118 | + } |
| 119 | + |
| 120 | + public override Cache.Partition getPartition(String partitionName) { |
| 121 | + return Cache.Org.getPartition(partitionName); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private class SessionPlatformCache extends PlatformCache { |
| 126 | + public SessionPlatformCache(String partitionName) { |
| 127 | + super(partitionName); |
| 128 | + } |
| 129 | + |
| 130 | + public override Cache.Partition getPartition(String partitionName) { |
| 131 | + return Cache.Session.getPartition(partitionName); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments