so the cache has 2 parts, the cache itself and a m...
# suitescript
c
so the cache has 2 parts, the cache itself and a method ("the loader method") that populates the cache. So if i lookup "foo" in the cache and it doesn't have it, the loader method is called "getFoo" to retrieve that method and the cache automagically caches it for you
j
Hmm, I'm having trouble wrapping my head around this one. You don't have an example you could share? I'm imagining something like this:
Copy code
cache.getCache({ name: 'testcache' })
        .get({
            key: 'testkey',
            loader: function(value) {
                return 'somevalue';
            }
        })
In the netsuite example it seems like they are loading a file from the file cabinet and searching for a specific value in the file. That seems like it defeats the purpose of the cache if you have to load the file cabinet file very time anyway.
c
I haven't used it since i switched jobs so i dont have an example but the loader method is only called if the value doesn't exist in the cache and once the function returns the value, it automagically caches it for you behind the scenes
that look right
basically if "testkey" doesn't exist, the loader function is called w/ "testkey" and it does some operation and returns the value that "testkey" should represent and thats cached for you automagically so the next time it won't have to use the loader and will just return the value
j
Ok I got it. In case the cache gets removed after it's initially set the loader is called and repopulates it.
c
yeah the cache should always be populated through the loader method but thats the idea
so you're always just "getting" values from the cache and if it doesn't exist -> loader method .... otherwise it returns it. Don't manually put values in the cache
j
Is there any good reason for using the
Cache.put(options)
method to populate the cache even when you have a loader on the
.get(options)
method?
c
its so you CAN do manual population
im sure there are cases for it but most of the time the loader method is what should be used.. its a generic method so maybe there'sa weird cache thing you need to do so you could use put then
j
Got it, thanks.
c
yep np.. just make a simple script and give it a go. It took me longer than i'll admit to get it figured out.