Skip to main content

Caching data

Through caching, Altogic allows you to efficiently reuse previously retrieved or computed data which can significantly improve response times of your services. Using cache nodes, you can cache new data, update, delete or get cached data.

Caching or updating cached data

You can cache a single object or a list of objects. While caching a value, you need to provide a unique identifier (cache key). This identifier is used to access cached values. The next time you would like to get your cached value, you need to specify this unique identifier.

If a cache key is already in use and you try to cache a value using this key, Altogic replaces the old cached value with the new one. In short, caching a value either creates a new entry in the cache if the cache key does not exist or replaces the existing value if the cache key exists.

The below service example gets user information from the database using the user's unique identifier (passed from the endpoint's path as userId). After getting the user data caches its value again using the user identifier as the cache key.

info

You can also use the cache object functionality of Create Single Object, Get Single object by Id, Get Single Object by Query, Update Object Fields by Id, Update Single Object by Id nodes to cache objects, and update cached objects.

With Create Single Object, Get Single object by Id, and Get Single Object by Query nodes, you can cache object data.

With Update Object Fields by Id, and Update Single Object by Id nodes you can update cached data.

caution

There is a limit on the size of the objects that you can cache. For free environments each cache key can hold max 5kB of data. If you try to cache an object which is more than this limit then you will receive an cache_key_size_exceeded error.

Getting cached data

You can access your cached data using its cache key. In the previous step, we have cached the user info. However, we can add an improvement to this service to first check the cache memory. If the object exists in cache memory, return its value; otherwise, get it from the database and then cache and return its value.

info

You can also use the cache object functionality of Get Single object by Id, and Get Single Object by Query nodes to retrieve cached objects.

Deleting cached data

Like caching a value or getting a cached value, you use the cache key for deleting a cached value.

info

You can also use the remove cached object functionality of Delete Single Object by Id node to remove objects from the cache.