Automatic Caching in API Maker

Automatic Caching

What Is Automatic Caching in API Maker...?

  • Automatic caching like a memory boost for your APIs.
  • Automatic caching in API Maker is a feature that helps your APIs perform faster by storing responses in Redis. Instead of hitting the database every time a request is made, API Maker delivers the same data from the cache, saving time and resources.
  • When we modify any data of table in any way, API Maker will reset cache of that table.
  • API Maker’s automatic caching is easy to use and works out-of-the-box for every API type. It supports all API categories – built-in database tables, schema APIs, custom APIs, system APIs, and even third-party endpoints.
  • When someone makes a request, API Maker can serve the response from redis cache instead of fetching it fresh from the database. That makes things a lot faster.-
  • This feature is especially useful for data that doesn't change often, like a list of categories or product names.

Why Caching Is Important

  • Caching copies frequently requested data in memory, yielding near-instant retrieval instead of slower disk/database fetches.
  • Serving data from cache offloads work from your primary database, improving scalability and user experience.
  • When you update or delete a record, API Maker automatically clears the cache for that data right away. This means you always get fresh results, no old or outdated data, and no need to clear the cache yourself.
  • In case of enterprise projects, when we have caching issue, it is very hard for developer to fix because he/she needs to check all the places where caching is happening and need to reset carefully. So developer needs to have great knowledge of business requirement to fix this type of caching issue, with API Maker you will never have these type of cache or stale data issue.

API Maker Implements Caching

  • Behind the scenes, API Maker uses Redis, a high-speed memory store, to handle caching efficiently.
  • When caching is enabled, API Maker saves your API responses in Redis. You don’t need to write any extra code, just turn it on by setting enableCaching: true flat in configuration, and it works.

Automatic and Manual Cache Management in API Maker

  • One of the best parts of API Maker’s caching is automation. When your data changes, like updating a user profile or deleting a record, the cache is reset automatically. No manual clearing, no custom logic needed.
  • But we can have custom logic to reset cache manually or periodically if we want using below option
    • Manually reset the cache using built-in system APIs like resetCustomApiCache.
  • Developer can force a cache reset and refresh data by including a special header in your API request.

Flexible Caching Control at Every Level in API Maker

You can control caching behavior at different levels:

  • Instance level
  • Database level
  • Collection or table level
  • You enable caching simply by setting the enableCaching flag to true in the API’s configuration. For example, to cache an entire database’s collections you would set:

Just use the enableCaching: true | false setting based on your requirements.

{
    // In the database settings (or .env override)
    enableCaching: true
}

Why Automatic caching Matters in API Maker...?

  • Any API you build can use caching (database, custom, system, or third-party).
  • This lets you choose whether to cache everything or just specific parts.
  • You don’t have to write cache-clear code.
  • If needed, you can force a cache refresh.
  • Cuts latency by serving previously fetched data.
  • Reduces server load and bandwidth.
  • endless high request volume more efficiently.

Redis Configuration in API Maker

  • API Maker uses Redis to store cached data at high speed. By default, it connects to Redis on host 127.0.0.1 and port 6390. These settings come from the redisExternal section in the package.json file, but you can easily change them using am__ environment variables or your config files.
  • The maxCharsResToCache setting defines the maximum size of a response that can be cached. By default, it’s 1,000,000 characters. Anything larger won’t be cached to avoid using too much memory.
  • These settings give you control over how Redis caching behaves. In most cases, you simply leave defaults in place, and API Maker takes care of using Redis to store and expire cached responses under the hood.

Visual Example

sql
┌──────────────────┐    request    ┌─────────────┐
│1. GET /api/users │ ------------->│ Cache check │
└──────────────────┘               └────┬────────┘
└── First time: fetch from DB → store in cache
└── Subsequent: serve from cache.
  • Sometimes, automatic caching isn't enough, you need full control. That’s where API Maker’s System Cache Reset API comes in handy. It lets you manually reset the cache for specific APIs, collections, or third-party endpoints.

Automatic Caching How it works diagram

Example of Reset Cache for a Database API

await g.sys.cache.resetCacheDB({
    instance: "INSTANCE_NAME",
    database: "DATABASE_NAME",
    collection: "COLLECTION_NAME"
});

Example of API call clears the Redis cache for the named custom API

  • For instance, to manually reset a custom API’s cache from code you can do:
await g.sys.cache.resetCacheCustomApis("CUSTOM_API_NAME");
  • API call clears the Redis cache for the named custom APIs.

  • This resets the cached results for that database table/collection. Both of these require no custom cache logic, API Maker provides them for you.


FAQ's

1. What happens when the data changes? Do I have to clear the cache manually?

No manual work required. When a record in the database is updated or deleted, API Maker automatically resets the affected cache entries. This ensures clients never see outdated data. You don’t need to write any invalidation code yourself.

2. How can I manually force a cache refresh?

You have two options. One is to include the special header x-am-cache-control in your API request, which forces API Maker to skip or clear the cache for that call. The other is to call the provided system cache APIs (in code or an API script) as shown above (e.g. g.sys.cache.resetCacheCustomApis or resetCacheDB). These let you programmatically clear caches whenever you need.

3. How long does a cached entry live?

Each cache entry has a TTL (time-to-live) you can configure. By default, API Maker sets this to 7200 seconds (2 hours). After the TTL expires, the next request will fetch fresh data and re-cache it. You can adjust redisValueExpireInSeconds in the configuration to change this duration.


Automatic caching in API Maker is designed to be transparent and hassle-free. With just a flip of a setting, your APIs gain in-memory caching (backed by Redis) and faster response times – all while the platform handles cache invalidation and management behind the scenes. This lets you build high-performance, scalable APIs without any code for caching implementation.