Skip to content

Commit

Permalink
Added new Cacheable method `Object get(String key, System.Type cacheB…
Browse files Browse the repository at this point in the history
…uilderClass)` (#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
  • Loading branch information
scolladon authored and jongpie committed Feb 22, 2023
1 parent fb14864 commit 104a41c
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 21 deletions.
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ A flexible cache management system for Salesforce Apex developers. Built to be s

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/)

## Unlocked Package - `Nebula` Namespace - v1.0.0
## Unlocked Package - `Nebula` Namespace - v1.0.1

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

## Unlocked Package - No Namespace - v1.0.0
## Unlocked Package - No Namespace - v1.0.1

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

---

Expand Down
30 changes: 29 additions & 1 deletion nebula-cache-manager/core/classes/CacheManager.cls
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public without sharing class CacheManager {
Map<String, Boolean> contains(Set<String> keys);
Boolean containsAll(Set<String> keys);
Object get(String key);
Object get(String key, System.Type cacheBuilderClass);
Map<String, Object> get(Set<String> keys);
Map<String, Object> getAll();
Set<String> getKeys();
Expand Down Expand Up @@ -86,6 +87,7 @@ public without sharing class CacheManager {
cacheTypeToMockPartitionProxy.put(cacheType, mockPartitionProxy);
}

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

public Boolean containsAll(Set<String> keys) {
Map<String, Boolean> keyToContainsResult = this.contains(keys);
if (keyToContainsResult == null || keyToContainsResult.isEmpty() == true) {
if (keyToContainsResult.isEmpty() == true) {
return false;
}

Expand All @@ -188,6 +190,19 @@ public without sharing class CacheManager {
}
}

public Object get(String key, System.Type cacheBuilderClass) {
if (this.fallbackTransactionCache.contains(key)) {
return this.fallbackTransactionCache.get(key);
} else if (this.cachePartitionProxy.isAvailable() == false) {
return this.fallbackTransactionCache.get(key, cacheBuilderClass);
} else {
// Cache.CacheBuilder.doLoad method can return null
Object value = this.cachePartitionProxy.get(key, cacheBuilderClass);
this.fallbackTransactionCache.put(key, value);
return value;
}
}

public Map<String, Object> get(Set<String> keys) {
Map<String, Object> keyToValue = this.cachePartitionProxy.get(keys);
if (keyToValue == null) {
Expand Down Expand Up @@ -315,6 +330,10 @@ public without sharing class CacheManager {
return this.platformCachePartition?.get(key);
}

public virtual Object get(String key, System.Type cacheBuilderClass) {
return this.platformCachePartition?.get(cacheBuilderClass, key);
}

public virtual Map<String, Object> get(Set<String> keys) {
return this.platformCachePartition?.get(keys);
}
Expand Down Expand Up @@ -367,6 +386,15 @@ public without sharing class CacheManager {
return this.keyToValue.get(key);
}

public Object get(String key, System.Type cacheBuilderClass) {
if (this.contains(key) == false) {
Cache.CacheBuilder cacheBuilder = (Cache.CacheBuilder) cacheBuilderClass.newInstance();
Object value = cacheBuilder.doLoad(key);
this.put(key, value);
}
return this.get(key);
}

public Map<String, Object> get(Set<String> keys) {
Map<String, Object> matchingKeyToValue = new Map<String, Object>();
for (String key : keys) {
Expand Down
Loading

0 comments on commit 104a41c

Please sign in to comment.