14

Can anyone provide a good example scenario for using "Org level platform cache" versus custom settings? I can understand when you would use session cache but I am still trying to come up with an scenario in which Org level platform cache would be preferred over custom settings

25

The two types of data stores are used for entirely different things. The platform cache is temporary cache, much like what a web browser uses to look up recently used data, while custom settings are more of a permanent cache, which basically means it won't go away until someone tells it to.

Custom settings are great for simple types of data that should retain their values indefinitely. For example, the next auto-number value for triggers to use, or the user's preferred default email template, or the encryption key for some custom encryption methods used to protect some data. These are things you probably don't want the system to forget, so they belong in custom settings. Note that custom settings only hold a handful of data types that must be defined beforehand.

Platform cache is used for complex types of data that can reconstructed if they're lost. For example, if you have a custom object that defines various menu items in a hierarchy, you might want to render this menu just once, stick it into some platform cache, and use it indefinitely until it's invalidated. This allows the page to load faster when there's a cache hit, improving overall performance for the majority of the time. It also has several other severe considerations, including no support for concurrency, persistence, and so on.

Since the cache might grow stale or be overwritten at any time, you should avoid placing any data in there that you cannot easily reconstruct. The cache, however, does support any type of serialized data that you can use, including collections, custom classes, and so on. This means that you get to skip almost all of the CPU and database time you'd incur by rebuilding the object every time your page is loaded from scratch. This can even reduce the View State of your page, because the data can be pulled directly from cache.

Org wide cache, while probably more difficult to conceptualize than the session cache, can still benefit users that share a common data set. Keep in mind that session cache refers to a single user, while org wide cache refers to anyone in the system. You can still break down org wide cache into smaller pieces using keys and/or partitions to share the cache across various users. For example, you might render menus for each profile, and you could store them in various keys like "local.00eXXXXXXXXXXXX.menu", allowing each profile to have their own unique menu that could be cached.

In general, you just need to ask yourself a two simple questions: "Do I need this data to persist in the database? (Yes: Custom Settings)" and "Do I just need this data to improve application performance? (Yes: Platform Cache)". There's usually always one right answer. Once you decide to use cache, then you can ask "Would this data best serve one user? (Yes: Session Cache)" and "Could this data be used across multiple users? (Yes: Org Cache)".

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.