---
schema_version: "1"
id: cf609867dcb3362ccfeb38977676a21b5e8f95806838edaa43f441bd938e41fa
title: "ApexGuru Antipattern: SOQL Without Platform Cache"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/apexguru_antipattern_soql_without_platform_cache.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_without_platform_cache.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 42200530a972c196fcd6b59feaf166b96722dd2c13a79d1497f3fbc6aba13d98
description: Running the same SOQL query repeatedly during a transaction or across requests can slow down performance and put an unnecessary load on the database. Instead, use Platform Cache to eliminate the need for custom settings or persistent storage.
products:
  - Cross Cloud
release: "264"
---
# ApexGuru Antipattern: SOQL Without Platform Cache

Running the same SOQL query repeatedly during a transaction or across requests can slow down performance and put an unnecessary load on the database. Instead, use Platform Cache to eliminate the need for custom settings or persistent storage.

## Scope

Detection only

Cache query results for faster data reuse across sessions or your entire org using [Platform Cache](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_cache_namespace_overview.htm). This approach is particularly helpful for frequently used queries on semistatic data invoked by various flows or trigger paths.

## Detection Example

```java
// Code without Cache
List<BranchContact__c> conlist = [SELECT Id,BillingState FROM Contact];

// Code with Cache
String cacheQuery = 'SELECT Id,BillingState FROM Contact';
conlist = BrContactPlatformCacheUtility.fetchFromCache('BrContactObjectCache', 'uid_query', cacheQuery);

// Platform Cache implementation
public class BrContactPlatformCacheUtility implements Cache.CacheBuilder {
   public static String query;
   public static String key;

   // Method to fetch from Cache
   public static List<sObject> fetchFromCache(String cacheName, String key, String query) {
      List<sObject> lstSObjValue = new List<sObject>();
      try {
         Cache.OrgPartition orgPartitionVar = Cache.Org.getPartition(cacheName);
         BrContactPlatformCacheUtility.query = query;
         BrContactPlatformCacheUtility.key = key;
         lstSObjValue = (List<sObject>)
orgPartitionVar.get(BrContactPlatformCacheUtility.class, key);
      catch (Exception ex) {
         // Handle Exception
      }
   return lstSObjValue;
   }

   // Method to Clear the Cache
   public static void clearCacheKey(String cacheName, String key) {
      Cache.OrgPartition orgPartitionVar = Cache.Org.getPartition(cacheName);
orgPartitionVar.remove(BrContactPlatformCacheUtility.class, key);
   }
}
```