-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.cfc
139 lines (109 loc) · 3.69 KB
/
Cache.cfc
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
component accessors = "true" implements = "lib.util.IContainer" {
property name = "timeToIdleSeconds" type = "numeric";
property name = "timeToLiveSeconds" type = "numeric";
Cache function init(required string name, string managerName) {
variables.name = arguments.name;
if(structKeyExists(arguments, "managerName")) {
// the named CacheManager
variables.cacheManager = createObject("java", "net.sf.ehcache.CacheManager").getCacheManager(arguments.managerName);
} else {
// the default/singleton CacheManager
variables.cacheManager = createObject("java", "net.sf.ehcache.CacheManager").getInstance();
}
// for anything more verbose, consider initializing the region prior to creating container
if(variables.cacheManager.cacheExists(arguments.name)) {
variables.cache = variables.cacheManager.getEhcache(javaCast("string", arguments.name));
} else {
variables.cache = variables.cacheManager.addCacheIfAbsent(javaCast("string", arguments.name));
}
return this;
}
void function clear() {
variables.cache.removeAll();
}
boolean function containsKey(required string key) {
return variables.cache.isKeyInCache(getKey(arguments.key));
}
void function destroy() {
variables.cacheManager.removeCache(variables.name);
}
any function get(required string key) {
local.element = getElement(arguments.key);
if(!isNull(local.element)) {
return local.element.getObjectValue();
}
}
any function getElement(required string key) {
return variables.cache.get(getKey(arguments.key));
}
numeric function getElementSize(required string key) {
local.element = variables.cache.get(getKey(arguments.key));
if(!isNull(local.element)) {
return local.element.getSerializedSize();
}
return 0;
}
any function getCache() {
return variables.cache;
}
any function getCacheManager() {
return variables.cacheManager;
}
private string function getKey(required string key) {
return lCase(arguments.key);
}
string function getName() {
return variables.name;
}
boolean function isEmpty() {
return variables.cacheManager.cacheExists(variables.name) ? variables.cache.getKeysWithExpiryCheck().size() == 0 : true;
}
string function keyList() {
return listSort(arrayToList(variables.cache.getKeysWithExpiryCheck()), "textnocase");
}
void function put(required string key, required any value) {
if(!structKeyExists(arguments, "timeToIdleSeconds")) {
if(structKeyExists(variables, "timeToIdleSeconds")) {
arguments.timeToIdleSeconds = variables.timeToIdleSeconds;
} else {
arguments.timeToIdleSeconds = variables.cache.getCacheConfiguration().getTimeToIdleSeconds();
}
}
if(!structKeyExists(arguments, "timeToLiveSeconds")) {
if(structKeyExists(variables, "timeToLiveSeconds")) {
arguments.timeToLiveSeconds = variables.timeToLiveSeconds;
} else {
arguments.timeToLiveSeconds = variables.cache.getCacheConfiguration().getTimeToLiveSeconds();
}
}
variables.cache.put(
createObject("java", "net.sf.ehcache.Element")
.init(
getKey(arguments.key),
arguments.value,
javaCast("int", arguments.timeToIdleSeconds),
javaCast("int", arguments.timeToLiveSeconds)
)
);
}
void function putAll(required struct values, boolean clear = false, boolean overwrite = false) {
if(arguments.clear) {
this.clear();
}
for(local.key in arguments.values) {
if(!containsKey(local.key) || arguments.overwrite) {
put(local.key, arguments.values[local.key]);
}
}
}
void function remove(required string key) {
variables.cache.remove(getKey(arguments.key));
}
struct function values() {
local.return = {};
for(local.key in keyList()) {
local.return[local.key] = get(local.key);
}
return local.return;
}
}