Skip to content

Commit 5d00e7f

Browse files
Removed need to pass the namespace in to get the qualified keys
Added docs to more of the cache classes
1 parent 48259c6 commit 5d00e7f

File tree

6 files changed

+266
-140
lines changed

6 files changed

+266
-140
lines changed

framework/default/ortoo-core/default/classes/fflib-extension/caching/BulkObjectCache.cls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ public with sharing class BulkObjectCache
301301
return this;
302302
}
303303

304-
private String createFullyQualifiedKey( String subKey )
304+
private String createFullyQualifiedKey( String key )
305305
{
306-
return cacheWrapper.createFullyQualifiedKey( PackageUtils.NAMESPACE_PREFIX, PARTITION_NAME, subkey );
306+
return cacheWrapper.createFullyQualifiedKey( PARTITION_NAME, key );
307307
}
308308

309309
private class SobjectIdGetter implements IdGetter

framework/default/ortoo-core/default/classes/fflib-extension/caching/CachedSoqlExecutor.cls

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public inherited sharing class CachedSoqlExecutor //NOPMD: incorrect report of t
1212

1313
ICacheAdaptor cacheWrapper = new OrgCache(); // by default, configure the cache to use the org version
1414

15-
private final static String SOQL_PARTITION_NAME = 'core';
15+
private final static String PARTITION_NAME = 'core';
1616
private final static Integer CACHE_LIFESPAN_SECONDS = 28800; // TODO: soft setting / option
1717

1818
/**
@@ -123,7 +123,7 @@ public inherited sharing class CachedSoqlExecutor //NOPMD: incorrect report of t
123123
*/
124124
public void clearAllCache()
125125
{
126-
String fullSoqlPartitionName = cacheWrapper.createFullyQualifiedPartitionName( PackageUtils.NAMESPACE_PREFIX, SOQL_PARTITION_NAME );
126+
String fullSoqlPartitionName = cacheWrapper.createFullyQualifiedPartitionName( PARTITION_NAME );
127127
for ( String thisKey : cacheWrapper.getKeys() )
128128
{
129129
String qualifiedKey = qualifiedKey( thisKey );
@@ -142,6 +142,6 @@ public inherited sharing class CachedSoqlExecutor //NOPMD: incorrect report of t
142142

143143
private String qualifiedKey( String subkey )
144144
{
145-
return cacheWrapper.createFullyQualifiedKey( PackageUtils.NAMESPACE_PREFIX, SOQL_PARTITION_NAME, subkey );
145+
return cacheWrapper.createFullyQualifiedKey( PARTITION_NAME, subkey );
146146
}
147147
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* Defines an interface that can be used to wrap / adapt a caching mechanism.
3+
*
4+
* Specifically designed to support the wrapping of the static Apex methods for:
5+
* * Cache.Org / Cache.OrgPartition
6+
* * Cache.Session / Cache.SessionPartition
7+
*
8+
* This allows for the types of cache to be used interchangably / and dynamically
9+
*/
110
public interface ICacheAdaptor
211
{
312
Boolean hasAccessToCache();
@@ -7,6 +16,6 @@ public interface ICacheAdaptor
716
Set<String> getKeys();
817
Boolean contains( String key );
918
void remove( String key );
10-
String createFullyQualifiedPartitionName( String namespace, String partitionName );
11-
String createFullyQualifiedKey( String namespace, String partitionName, String subKey );
19+
String createFullyQualifiedPartitionName( String partitionName );
20+
String createFullyQualifiedKey( String partitionName, String key );
1221
}

framework/default/ortoo-core/default/classes/fflib-extension/caching/NullCache.cls

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
/**
2+
* An implementation of the ICacheAdaptor that is benign.
3+
*
4+
* Allows for the use of code that will automatically interact with a cache and be able to switch that off dynamically and simply.
5+
*
6+
* All users are assumed to be allowed to use the cache, but it describes itself as 'not a cache' and effectively does nothing.
7+
*/
18
public class NullCache implements ICacheAdaptor
29
{
310
public Boolean hasAccessToCache()
411
{
5-
return false;
12+
return true;
613
}
714

815
public Boolean isACache()
@@ -33,13 +40,13 @@ public class NullCache implements ICacheAdaptor
3340
{
3441
}
3542

36-
public String createFullyQualifiedPartitionName( String namespace, String partitionName )
43+
public String createFullyQualifiedPartitionName( String partitionName )
3744
{
38-
return namespace + '.' + partitionName;
45+
return partitionName;
3946
}
4047

41-
public String createFullyQualifiedKey( String namespace, String partitionName, String subKey )
48+
public String createFullyQualifiedKey( String partitionName, String subKey )
4249
{
43-
return namespace + '.' + partitionName + '.' + subkey;
50+
return partitionName + '.' + subkey;
4451
}
4552
}
Lines changed: 149 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
/**
2+
* An implementation of the ICacheAdaptor that utilises the Org Level Platform Cache.
3+
*
4+
* Due to the shared nature of the cache, all interactions with the cache require the user to have the
5+
* custom permission as defined in 'CAN_ACCESS_CACHE_PERMISSION'.
6+
*
7+
* Attempting to access the cache without this permission will result in an OrgCache.AccessViolationException
8+
*/
19
public class OrgCache implements ICacheAdaptor
210
{
311
public class AccessViolationException extends Exceptions.DeveloperException {} // this looks like a config exception, but actually the system should be built
412
// in such a way that it's never possible to get this exception
513

6-
@testVisible
14+
@testVisible
715
private final static String CAN_ACCESS_CACHE_PERMISSION = 'ProcessesCanAccessCache';
816

9-
public Boolean hasAccessToCache
17+
private Boolean hasAccessToCache
1018
{
1119
get
1220
{
@@ -19,86 +27,143 @@ public class OrgCache implements ICacheAdaptor
1927
private set;
2028
}
2129

22-
public Boolean hasAccessToCache()
23-
{
24-
return this.hasAccessToCache;
25-
}
26-
27-
public Boolean isACache()
28-
{
29-
return true;
30-
}
31-
32-
public Object get( String key )
33-
{
34-
if ( ! hasAccessToCache )
35-
{
36-
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
37-
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
38-
.addContext( 'method', 'get' )
39-
.addContext( 'key', key );
40-
}
41-
42-
return Cache.Org.get( key );
43-
}
44-
45-
public void put( String key, Object value, Integer lifespan )
46-
{
47-
if ( ! hasAccessToCache )
48-
{
49-
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
50-
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
51-
.addContext( 'method', 'put' )
52-
.addContext( 'key', key )
53-
.addContext( 'value', value );
54-
}
55-
56-
Cache.Org.put( key, value, lifespan, Cache.Visibility.NAMESPACE, true ); // immutable outside of namespace
57-
}
58-
59-
public Set<String> getKeys()
60-
{
61-
if ( ! hasAccessToCache )
62-
{
63-
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
64-
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
65-
.addContext( 'method', 'getKeys' );
66-
}
67-
68-
return Cache.Org.getKeys();
69-
}
70-
71-
public Boolean contains( String key )
72-
{
73-
if ( ! hasAccessToCache )
74-
{
75-
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
76-
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
77-
.addContext( 'method', 'contains' )
78-
.addContext( 'key', key );
79-
}
80-
return Cache.Org.contains( key );
81-
}
82-
83-
public void remove( String key )
84-
{
85-
if ( ! hasAccessToCache )
86-
{
87-
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
88-
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
89-
.addContext( 'method', 'remove' )
90-
.addContext( 'key', key );
91-
}
92-
Cache.Org.remove( key );
93-
}
94-
95-
public String createFullyQualifiedPartitionName( String namespace, String partitionName )
96-
{
97-
return Cache.OrgPartition.createFullyQualifiedPartition( namespace, partitionName );
98-
}
99-
100-
public String createFullyQualifiedKey( String namespace, String partitionName, String subKey )
101-
{
102-
return Cache.OrgPartition.createFullyQualifiedKey( namespace, partitionName, subkey );
103-
}
30+
/**
31+
* States if this user has access to the cache - I.E. has the custom permission defined in CAN_ACCESS_CACHE_PERMISSION
32+
*
33+
* @return Boolean States if the current user has access to the cache
34+
*/
35+
public Boolean hasAccessToCache()
36+
{
37+
return this.hasAccessToCache;
38+
}
39+
40+
/**
41+
* States that this is a cache
42+
*
43+
* @return Boolean True, stating that this instance represents a true cache
44+
*/
45+
public Boolean isACache()
46+
{
47+
return true;
48+
}
49+
50+
/**
51+
* Retrieve the cache entry with the given key.
52+
*
53+
* If the user does not have access to the cache, will throw an AccessViolationException.
54+
* If the entry does not exist in the cache, will return null
55+
*
56+
* @param String The key for the object to retrieve
57+
* @return Object The cached object, if it exists
58+
*/
59+
public Object get( String key )
60+
{
61+
Contract.requires( key != null, 'get called with a null key' );
62+
63+
if ( ! hasAccessToCache )
64+
{
65+
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
66+
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
67+
.addContext( 'method', 'get' )
68+
.addContext( 'key', key );
69+
}
70+
71+
return Cache.Org.get( key );
72+
}
73+
74+
/**
75+
* Put the stated value into the stated key for the specified duration (in seconds)
76+
*
77+
* If the user does not have access to the cache, will throw an AccessViolationException.
78+
*
79+
* @param String The key to use for the storage of the object
80+
* @param Object The object to store
81+
* @param Integer The lifespan of the object within the cache, in seconds
82+
* @return Object The cached object, if it exists
83+
*/
84+
public void put( String key, Object value, Integer lifespan )
85+
{
86+
Contract.requires( key != null, 'put called with a null key' );
87+
Contract.requires( lifespan != null, 'put called with a null lifespan' );
88+
Contract.requires( lifespan >= 0, 'put called with a negaitve lifespan' );
89+
90+
if ( ! hasAccessToCache )
91+
{
92+
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
93+
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
94+
.addContext( 'method', 'put' )
95+
.addContext( 'key', key )
96+
.addContext( 'value', value );
97+
}
98+
99+
Cache.Org.put( key, value, lifespan, Cache.Visibility.NAMESPACE, true ); // immutable outside of namespace
100+
}
101+
102+
/**
103+
* Retrieve a set of the current keys of objects stored in this cache.
104+
*
105+
* If the user does not have access to the cache, will throw an AccessViolationException.
106+
*
107+
* @return Set<String> The keys that currently exist in the cache
108+
*/
109+
public Set<String> getKeys()
110+
{
111+
if ( ! hasAccessToCache )
112+
{
113+
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
114+
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
115+
.addContext( 'method', 'getKeys' );
116+
}
117+
118+
return Cache.Org.getKeys();
119+
}
120+
121+
/**
122+
* States if the cache currently contains an object in the given key.
123+
*
124+
* If the user does not have access to the cache, will throw an AccessViolationException.
125+
*
126+
* @param String The key for the object to look for
127+
* @return Boolean Whether the key exists in the cache
128+
*/
129+
public Boolean contains( String key )
130+
{
131+
if ( ! hasAccessToCache )
132+
{
133+
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
134+
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
135+
.addContext( 'method', 'contains' )
136+
.addContext( 'key', key );
137+
}
138+
return Cache.Org.contains( key );
139+
}
140+
141+
/**
142+
* Instucts the cache to remove the object at the given key
143+
*
144+
* If the user does not have access to the cache, will throw an AccessViolationException.
145+
*
146+
* @param String The key to remove
147+
*/
148+
public void remove( String key )
149+
{
150+
if ( ! hasAccessToCache )
151+
{
152+
throw new AccessViolationException( Label.ortoo_core_org_cache_access_violation )
153+
.setErrorCode( FrameworkErrorCodes.CACHE_ACCESS_VIOLATION )
154+
.addContext( 'method', 'remove' )
155+
.addContext( 'key', key );
156+
}
157+
Cache.Org.remove( key );
158+
}
159+
160+
public String createFullyQualifiedPartitionName( String partitionName )
161+
{
162+
return Cache.OrgPartition.createFullyQualifiedPartition( PackageUtils.NAMESPACE_PREFIX, partitionName );
163+
}
164+
165+
public String createFullyQualifiedKey( String partitionName, String subKey )
166+
{
167+
return Cache.OrgPartition.createFullyQualifiedKey( PackageUtils.NAMESPACE_PREFIX, partitionName, subkey );
168+
}
104169
}

0 commit comments

Comments
 (0)