Skip to content

Commit 104a41c

Browse files
scolladonjongpie
authored andcommitted
Added new Cacheable method Object get(String key, System.Type cacheBuilderClass) (#6)
* feat: add get key cachebuilder API * test improvements for containsAll, cache disabled spec, and implemented `validateKey` spec * build: set husky as executable, updated lock dependencies
1 parent fb14864 commit 104a41c

File tree

6 files changed

+413
-21
lines changed

6 files changed

+413
-21
lines changed

.husky/pre-commit

100644100755
File mode changed.

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ A flexible cache management system for Salesforce Apex developers. Built to be s
44

55
Learn more about the history & implementation of this repo in [the Joys of Apex article 'Iteratively Building a Flexible Caching System for Apex'](https://www.jamessimone.net/blog/joys-of-apex/iteratively-building-a-flexible-caching-system/)
66

7-
## Unlocked Package - `Nebula` Namespace - v1.0.0
7+
## Unlocked Package - `Nebula` Namespace - v1.0.1
88

9-
[![Install Unlocked Package (Nebula namespace) in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n2AQAQ)
10-
[![Install Unlocked Package (Nebula namespace) in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n2AQAQ)
9+
[![Install Unlocked Package (Nebula namespace) in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n6rQAA)
10+
[![Install Unlocked Package (Nebula namespace) in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n6rQAA)
1111

12-
## Unlocked Package - No Namespace - v1.0.0
12+
## Unlocked Package - No Namespace - v1.0.1
1313

14-
[![Install Unlocked Package (no namespace) in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n25QAA)
15-
[![Install Unlocked Package (no namespace) in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n25QAA)
14+
[![Install Unlocked Package (no namespace) in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n6hQAA)
15+
[![Install Unlocked Package (no namespace) in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y0000015n6hQAA)
1616

1717
---
1818

nebula-cache-manager/core/classes/CacheManager.cls

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public without sharing class CacheManager {
4141
Map<String, Boolean> contains(Set<String> keys);
4242
Boolean containsAll(Set<String> keys);
4343
Object get(String key);
44+
Object get(String key, System.Type cacheBuilderClass);
4445
Map<String, Object> get(Set<String> keys);
4546
Map<String, Object> getAll();
4647
Set<String> getKeys();
@@ -86,6 +87,7 @@ public without sharing class CacheManager {
8687
cacheTypeToMockPartitionProxy.put(cacheType, mockPartitionProxy);
8788
}
8889

90+
@TestVisible
8991
private static void validateKey(String key) {
9092
Matcher regexMatcher = ALPHANUMERIC_REGEX_PATTERN.matcher(key);
9193
if (regexMatcher.matches() == false) {
@@ -161,7 +163,7 @@ public without sharing class CacheManager {
161163

162164
public Boolean containsAll(Set<String> keys) {
163165
Map<String, Boolean> keyToContainsResult = this.contains(keys);
164-
if (keyToContainsResult == null || keyToContainsResult.isEmpty() == true) {
166+
if (keyToContainsResult.isEmpty() == true) {
165167
return false;
166168
}
167169

@@ -188,6 +190,19 @@ public without sharing class CacheManager {
188190
}
189191
}
190192

193+
public Object get(String key, System.Type cacheBuilderClass) {
194+
if (this.fallbackTransactionCache.contains(key)) {
195+
return this.fallbackTransactionCache.get(key);
196+
} else if (this.cachePartitionProxy.isAvailable() == false) {
197+
return this.fallbackTransactionCache.get(key, cacheBuilderClass);
198+
} else {
199+
// Cache.CacheBuilder.doLoad method can return null
200+
Object value = this.cachePartitionProxy.get(key, cacheBuilderClass);
201+
this.fallbackTransactionCache.put(key, value);
202+
return value;
203+
}
204+
}
205+
191206
public Map<String, Object> get(Set<String> keys) {
192207
Map<String, Object> keyToValue = this.cachePartitionProxy.get(keys);
193208
if (keyToValue == null) {
@@ -315,6 +330,10 @@ public without sharing class CacheManager {
315330
return this.platformCachePartition?.get(key);
316331
}
317332

333+
public virtual Object get(String key, System.Type cacheBuilderClass) {
334+
return this.platformCachePartition?.get(cacheBuilderClass, key);
335+
}
336+
318337
public virtual Map<String, Object> get(Set<String> keys) {
319338
return this.platformCachePartition?.get(keys);
320339
}
@@ -367,6 +386,15 @@ public without sharing class CacheManager {
367386
return this.keyToValue.get(key);
368387
}
369388

389+
public Object get(String key, System.Type cacheBuilderClass) {
390+
if (this.contains(key) == false) {
391+
Cache.CacheBuilder cacheBuilder = (Cache.CacheBuilder) cacheBuilderClass.newInstance();
392+
Object value = cacheBuilder.doLoad(key);
393+
this.put(key, value);
394+
}
395+
return this.get(key);
396+
}
397+
370398
public Map<String, Object> get(Set<String> keys) {
371399
Map<String, Object> matchingKeyToValue = new Map<String, Object>();
372400
for (String key : keys) {

0 commit comments

Comments
 (0)