💃 Supercharge your org 🕺

The hidden power of Salesforce platform cache

Good morning, Salesforce Nerds! Ever feel like your Salesforce org is moving at the speed of a sloth wading through molasses? 🦥 

⏳️ Queries take forever

🐢 Complex calculations are sluggish

😵‍💫 Governor limits are just waiting to slap you in the face

Well, my fellow Salesforce nerd, let me introduce you to Salesforce Platform Cache - a technique to help you boost performance and optimize your code.

Let’s get it! 💪 

TABLE OF CONTENTS

YOUR ORG’S SHORT-TERM MEMORY

WHAT IS PLATFORM CACHE?

Salesforce Platform Cache is a memory-based caching layer that lets you store reusable data, reducing the need to query the database or recalculate values repeatedly.

Think of it as a super-efficient sticky note for your org - only instead of cluttering your desk, it speeds up your transactions and keeps your users happy. 🤗 

Before we dive into implementation details, let’s cover the key concepts that make Platform Cache tick 👇️ 

🔎 Org Cache vs. Session Cache

Public Storage vs. Personal Diary

🌐 Org Cache: Stores data globally across all users in your Salesforce org. Ideal for reference data, API responses, and common calculations.

🧔 Session Cache: Stores data specific to a single user’s session. Perfect for personalization, UI performance boosts, and temporary data storage during a user’s interaction.

📜 Cache Partitions

Sorting Your Data Like a Pro

🫙 A partition is like a dedicated storage bin for your cached data.

📦️ Partitions help organize cached data by different applications or use cases.

🎛️ You can define private (restricted to certain apps) or default (available to all apps) partitions.

🔑 Cache Keys and Values

The Art of Quick Lookups

🟢 Data in the cache is stored as key-value pairs, making it easy to retrieve later.

👍️ Keys should be unique and well-structured to prevent unintended overwrites.

CACHE ME IF YOU CAN

WHY USE PLATFORM CACHE?

Nobody likes a sluggish Salesforce org - slow queries, redundant calculations, and governor limits waiting to strike at the worst moment.  

That’s where Salesforce Platform Cache swoops in like a performance-boosting superhero, cutting down on expensive operations!

Here’s why you should care about Platform Cache:

💪 Performance Gains: Cut down on redundant queries and expensive computations.

🔥 Governor Limit Optimization: Reduce SOQL queries and CPU time by fetching data from memory.

🤩 Better User Experience: Speed up page loads and refreshes by caching frequently accessed data.

CACHE ME OUTSIDE HOW BOUT DAT

HOW TO USE PLATFORM CACHE

Enough talk! Let’s get hands-on with some simple Apex examples. 💯 

📝 Storing Data in Cache

Write Once, Read Many

// Define the key
String key = 'TopAccounts';

// Import the caching classes
Cache.OrgPartition orgCache = Cache.Org.getPartition('Default');

// Do the thing
List<Account> accounts = [SELECT Id, Name FROM Account ORDER BY Name LIMIT 10];
orgCache.put(key, accounts);

This stores the top 10 accounts in the cache, reducing the need for repeated SOQL queries.

🔖 Retrieving Cached Data

If It’s Not There, Fetch It!

// Define the key
String key = 'TopAccounts';

// Check the cache
List<Account> cachedAccounts = (List<Account>) orgCache.get(key);

// If nothing is found then hydrate the cache
if (cachedAccounts == null) {
    // Data not found in cache, fetch from database
    cachedAccounts = [SELECT Id, Name FROM Account ORDER BY Name LIMIT 10];
    orgCache.put(key, cachedAccounts);
}

This checks if the data exists in cache; if not, it queries Salesforce and updates the cache.

🧠 Using Session Cache

Short-Term Memory Gains

Cache.SessionPartition sessionCache = Cache.Session.getPartition('Default');

sessionCache.put('UserPreferences', 'DarkMode');

String userPref = (String) sessionCache.get('UserPreferences');

Great for personalizing user experiences based on preferences stored temporarily.

THE FINE PRINT

USE CASES & CONSIDERATIONS

Knowing how Platform Cache works is great, but understanding when to use it is where the real magic happens.

Let’s explore some practical scenarios where caching can supercharge your Salesforce implementation and keep your org running at lightning speed. ⚡️ 

🔻 Reducing API Calls

Why Call When You Can Recall?

  • Store API responses in Org Cache to minimize external callouts.

💡 Caching Picklist Values

Describe Calls? Ain’t Nobody Got Time for That!

  • Avoid unnecessary describe calls by caching field metadata.

💨 Accelerating Custom Reports

Faster Than a Lightning Component Refresh

  • Cache precomputed report data for faster rendering.

🏎️ Speeding Up Batch Jobs

SOQL Query Diet Plan

  • Store frequently accessed lookup values to avoid repetitive SOQL queries.

 Enhancing LWC Performance

Because Users Deserve Instant Gratification. Except Russ. 🖕 

  • Cache frequently used UI data in Session Cache for a smoother user experience.

But don’t go HAM just yet. Beware, there be dragons here. 🐉 Here are some things you’ll want to consider …

⏲️ Cache Expiry: Cached data is not permanent and can be evicted anytime (typically LRU - Least Recently Used policy applies).

📥️ Limited Space: Cache size is capped based on your org’s allocation, so use it wisely.

🚫 Not a Database Replacement: Platform Cache is great for performance but isn’t meant for long-term storage. Just don’t.

WRAPPING IT UP

CONCLUSION

Salesforce Platform Cache is a game-changer when it comes to boosting performance and reducing system strain. ❤️‍🔥 

By strategically using Org and Session Cache, you can supercharge your Apex code, optimize governor limits, and deliver a seamless user experience.

Do yourself a favor and give this an honest look! 👀 

SOUL FOOD

Today’s Principle

"War is 90% information."

Napoleon Bonaparte

and now....Salesforce Memes

What did you think about today's newsletter?

Login or Subscribe to participate in polls.