4

I'm using org cache to store the results of a call to the google maps geocode api. Just noticed, the caching doesn't seem to work in a Sandbox.

You can see how I'm storing/accessing here:

public static Location getGeocode(String query){
        if (query != null) {
            String q = query.toLowerCase().replaceAll('[^a-zA-Z0-9]', '');
            System.debug(q);
            Cache.OrgPartition orgPart = Cache.Org.getPartition('local.GoogleMaps');
            if (orgPart != null){
                if (orgPart.contains(q)) {
                    return (Location)orgPart.get(q);
                }
            }
            ... 
            ... calling google maps, parsing the response
            ...
            center = Location.newInstance((Decimal)loc.get('lat'), (Decimal)loc.get('lng'));
            orgPart.put(q, center);
            return center;
        }
    return null;
}

So works as expected in prod, we don't get charged twice if someone looks up the same string too quickly.

But in the sandbox. Nothing gets added to the cache and I still have 0% cache usage.

This method is called via a @AuraEnabled(Cacheable=true) method, and in the sandbox, caching is turned off. But I didn't think that'd disabled the platform cache for direct calls, right?

If I open an Execute Anonymous window in the dev console, I can add something to the cache and then access it again with another statement, so It seems the cache works.

Any ideas? Asking more for knowledge/documentation purposes!

2
  • 1
    I don't have any documantation to support it but we found that saving apex classes can cause the cache to lose data. Our cache code has always been unstable in sandboxes in which there is constant development. Is your sandbox a shared sandbox? – gNerb Apr 16 '20 at 19:04
  • 1
    Can confirm. Saving an Apex class does indeed reset the platform cache. Saw this behavior consistently when working in a sandbox. – Mehdi Maujood Jun 3 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.