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+ } ;
0 commit comments