forked from cesarParra/expression
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentCache.cls
More file actions
92 lines (79 loc) · 2.99 KB
/
Copy pathEnvironmentCache.cls
File metadata and controls
92 lines (79 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public with sharing class EnvironmentCache {
private static Boolean isStandardFunctionCacheDisabled = false;
private static final Map<Environment, Map<Expr.FunctionCall, Object>> functionCallCache =
new Map<Environment, Map<Expr.FunctionCall, Object>>();
private static final Map<Environment, Map<Expr.Query, Object>> queryCacheByEnvironment =
new Map<Environment, Map<Expr.Query, Object>>();
public static void disableCache() {
isStandardFunctionCacheDisabled = true;
}
public static void enableCache() {
isStandardFunctionCacheDisabled = false;
}
public static void cacheStandardFunctionCall(
Environment env,
Expr.FunctionCall function,
Object result
) {
cacheFunctionCall(!isStandardFunctionCacheDisabled, env, function, result);
}
public static void cacheCustomMetadataFunctionCall(
Boolean shouldCacheConfiguration,
Environment env,
Expr.FunctionCall function,
Object result
) {
cacheFunctionCall(shouldCacheConfiguration, env, function, result);
}
private static void cacheFunctionCall(
Boolean shouldCache,
Environment env,
Expr.FunctionCall function,
Object result
) {
if (!shouldCache) {
return;
}
Map<Expr.FunctionCall, Object> cachedFunctions = functionCallCache.get(env) ?? new Map<Expr.FunctionCall, Object>();
cachedFunctions.put(function, result);
functionCallCache.put(env, cachedFunctions);
}
public static Boolean isFunctionCallCached(
Environment env,
Expr.FunctionCall function
) {
Map<Expr.FunctionCall, Object> cachedFunctions = functionCallCache.get(env);
return cachedFunctions != null && cachedFunctions.containsKey(function);
}
public static Object getCachedFunctionCall(
Environment env,
Expr.FunctionCall function
) {
Map<Expr.FunctionCall, Object> cachedFunctions = functionCallCache.get(env);
if (cachedFunctions != null) {
return cachedFunctions.get(function);
}
return null;
}
public static void cacheQueryResult(Environment env, Expr.Query query, Object result) {
if (isStandardFunctionCacheDisabled) {
return;
}
if (!queryCacheByEnvironment.containsKey(env)) {
queryCacheByEnvironment.put(env, new Map<Expr.Query, Object>());
}
queryCacheByEnvironment.get(env).put(query, result);
}
public static Boolean isQueryResultCached(Environment env, Expr.Query query) {
if (!queryCacheByEnvironment.containsKey(env)) {
return false;
}
return queryCacheByEnvironment.get(env).containsKey(query);
}
public static Object getCachedQueryResult(Environment env, Expr.Query query) {
if (!queryCacheByEnvironment.containsKey(env)) {
return null;
}
return queryCacheByEnvironment.get(env).get(query);
}
}