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
09-03-2022 03:37 PM
Before you read on, be warned: this can lead you down the path where at the end you will be remembered as the person who brought down Prod. I'm assuming you don't want to do that. So be very careful.
There is a class that can help you with server side caching: GlideCacheManager
. In order to cache something, first one needs to be created by calling its addPrivateCacheable
method:
GlideCacheManager.addPrivateCacheable('<cache name>');
Afte that you can add key-value pairs to the cache:
GlideCacheManager.put('<cache name>', '<key>', '<value>');
To read a cached value:
GlideCacheManager.get('<cache name>', '<key>');
To remove a key-value from cache:
GlideCacheManager.prefixFlush('<cache name>', '<key>');
To clean up the cache:
GlideCacheManager.flush('<cache name>');
And here you need to be very careful: always check that the cache already exist before putting something in there. Don't overdo the quantity of the data added to cache. Keep some last usage time or something in every cache you create (but create as few caches as possible) and clean up caches not used for a while using some scheduled process. I would also be careful to only place stuff in cache that are not meant for everybody do see (like HR data) - the private cache might be read by someone unauthorized.
Again, it is a powerful tool, but can get you hurt.
As for a client side, you could use standard web technologies, like SharedWorker. Set one up, load all scripts needed and communicate with it from any page of the same site.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2022 12:31 PM
Thank you Janos for your time.
One follow up question.
What is the difference between gs.getSession().putClientData() and GlideCacheManager?
when to use what?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2022 08:55 AM
You're welcome.
The former in a user session - as the name implies, it will only work for the user that created the session and it goes away with the session. The latter lives on the instance level and it goes away when the cache is flushed or the creator destroys it. That means that whatever you are caching can only be reused by the specific user vs. reused globally. I suppose the instance level cache can come in handy if you have a complicated ACL script that queries for the same small set of data a lot. You might gain performance if you cache that set of data.
But a bid difference between the two is also where they live. The former is automatically transported to the client side, the latter only lives server side.