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
+ */
1
9
public class OrgCache implements ICacheAdaptor
2
10
{
3
11
public class AccessViolationException extends Exceptions .DeveloperException {} // this looks like a config exception, but actually the system should be built
4
12
// in such a way that it's never possible to get this exception
5
13
6
- @testVisible
14
+ @testVisible
7
15
private final static String CAN_ACCESS_CACHE_PERMISSION = ' ProcessesCanAccessCache' ;
8
16
9
- public Boolean hasAccessToCache
17
+ private Boolean hasAccessToCache
10
18
{
11
19
get
12
20
{
@@ -19,86 +27,143 @@ public class OrgCache implements ICacheAdaptor
19
27
private set ;
20
28
}
21
29
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
+ }
104
169
}
0 commit comments