Skip to content

Commit 81c1a3c

Browse files
authored
RM: Adding cache wrapper script include (#1000)
1 parent b86b3d4 commit 81c1a3c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var CacheHelper = Class.create();
2+
CacheHelper.prototype = {
3+
4+
logIt : false,
5+
6+
initialize: function(log) {
7+
this.logIt = log;
8+
},
9+
10+
/**
11+
* Adds data to the cache
12+
*
13+
* @param {string} cacheName : cache name
14+
* @param {string} key : unique cache key
15+
* @param {any} data : any data to be cached
16+
*/
17+
addToCache: function(cacheName, key, data) {
18+
this._validateCacheName(cacheName);
19+
this._validateCacheKey(key);
20+
21+
GlideCacheManager.put(cacheName, key, data);
22+
},
23+
24+
/**
25+
* Removes data from cache
26+
*
27+
* @param {string} cacheName : cache name
28+
* @param {string} key : unique cache key
29+
* @returns cached data
30+
*/
31+
getFromCache: function(cacheName, key) {
32+
this._validateCacheName(cacheName);
33+
this._validateCacheKey(key);
34+
35+
var data = GlideCacheManager.get(cacheName, key);
36+
return data;
37+
},
38+
39+
removeFromCache: function(cacheName) {
40+
this._validateCacheName(cacheName);
41+
42+
GlideCacheManager.flush(cacheName);
43+
},
44+
45+
/**
46+
* Either gets the data from cache or calls the callback functions, get the data and then adds it to the cache
47+
* @param {string} cacheName : cache name
48+
* @param {string} key : unique cache key
49+
* @param {function} dataCallBack : call back function that returns the data to be cached
50+
* @returns data from the cache or based on call back function
51+
*/
52+
getOrAddToCache: function(cacheName, key, dataCallBack) {
53+
this._validateCacheName(cacheName);
54+
this._validateCacheKey(key);
55+
56+
var data = GlideCacheManager.get(cacheName, key);
57+
58+
if (gs.nil(data)) {
59+
data = dataCallBack();
60+
GlideCacheManager.put(cacheName, key, data);
61+
if(this.logIt) gs.debug("Data from source.");
62+
return data;
63+
}
64+
65+
if(this.logIt) gs.debug("Data from cache.");
66+
return data;
67+
},
68+
69+
_validateCacheName :function(cacheName){
70+
if(!cacheName) throw new Error("cacheName is required");
71+
},
72+
73+
_validateCacheKey :function(cacheKey){
74+
if(!cacheKey) throw new Error("cacheKey is required");
75+
},
76+
77+
type: 'CacheHelper'
78+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# BenchmarkRunner
2+
Just a wrapper around GlideCacheManager with methods to enable validation of Cache key and data and ability to use the GlideCacheManager easily.
3+
4+
## Example server-side call (background script)
5+
```javascript
6+
var cacheName = "rahman_test";
7+
var cacheKey = "1";
8+
9+
var helper = new CacheHelper(false);
10+
11+
// Either get the data from cache or add it
12+
var data = helper.getOrAddToCache(cacheName, cacheKey, function(){
13+
gs.log("This will be called if the data is not in the cache. The second time will not be called.");
14+
15+
// This will be called if the data is not in cache!!!
16+
var data = {
17+
name: "rahman",
18+
}
19+
20+
return data;
21+
})
22+
23+
gs.log(JSON.stringify(data));
24+
25+
//helper.removeFromCache(cacheName)

0 commit comments

Comments
 (0)