Caching mechanism in scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 06:34 PM
Hi All,
Is there any Cache mechanism available in scripts?
I want to use Cache in Client Script and Script Include.
Thanks
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 06:40 PM
Hi,
In client scripts there's not.
Script include, which is server side, you could possible try and store the value in a system property (that can be cached as a side note -- and be accessible somewhere else).
Ideally, system properties aren't change that often though.
Can you give a bit more information as to what you're trying to do, your use case?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 08:58 PM
Hi Allen,
I have choice list which are static.
I want to store in cache so that if user selects the same item, I can get it from cache.
cache["key1"] = {"value1", "name1"};
cache["key2"] = {"value2", "name2"};
cache["key3"] = {"value3", "name3"};
So that if user selects "key2" , I can get it from cache instead of queuing again.
Please let me know if any info needed.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2022 03:05 PM
I found alternative approach "gs.getSession();"
https://community.servicenow.com/community?id=community_article&sys_id=db2b54a0db283cd04819fb24399619a8
Any comments, please
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2022 08:15 AM
On the Server Side it depends what the scope of your "caching" is meant to be. In some situations, you may only need to cache something for the duration of a single HTTP request. That is, from the point the browser makes the request, through all the server-side scripts, workflow activities, business rules, script includes, etc. until the response is provided back to the browser. In this situation, you can "cache" things in the global namespace -- or on objects that already exist in the global namespace. (Global namespace here is not referring to the ServiceNow notion of Global vs Scoped applications but rather the JavaScript notion of the top-most namespace for variables, like "window" in browser JavaScript.)
Take a look at this Script Include:
var CachingDemo = Class.create();
CachingDemo.prototype = {
initialize: function() {
this.id = Math.random().toString().substr(2,7);
if (!CachingDemo.cachedOrigID)
CachingDemo.cachedOrigID = this.id;
},
getID: function() {
return this.id;
},
getOrigID: function() {
return CachingDemo.cachedOrigID;
},
type: 'CachingDemo'
};
Now, observe the output when re-using it multiple times in a background script:
var cd1 = new CachingDemo();
gs.info(cd1.getID() + '\t' + cd1.getOrigID())
var cd2 = new CachingDemo();
gs.info(cd2.getID() + '\t' + cd2.getOrigID())
var cd3 = new CachingDemo();
gs.info(cd3.getID() + '\t' + cd3.getOrigID())
Output:
*** Script: 6399908 6399908
*** Script: 8330375 6399908
*** Script: 6559680 6399908
Notice that even though the CachingDemo is instantiated 3 separate times (cd1, cd2, cd3) and each class has its own unique id, all 3 classes are able to access cachedOrigID that was first assigned. While this example shows multiple uses of CachingDemo in a single background script, this same mechanism would work even if you were instantiating CachingDemo once from a Business Rule, then another time from another Business Rule, and another time from a Script Include or other server-side script.