2021-03-14 18:13:28 +03:00
+++
noatcards = True
isdraft = False
+++
# Database caching, what to cache
2021-03-21 13:55:58 +03:00
## Introduction
2021-03-14 18:13:28 +03:00
There are multiple levels you can cache that fall into two general categories: database queries and objects:
- Row level
- Query-level
- Fully-formed serializable objects
- Fully-rendered HTML
Generaly, you should try to avoid file-based caching, as it makes cloning and auto-scaling more difficult.
2021-03-21 13:55:58 +03:00
## Caching at the database query level
2021-03-14 18:13:28 +03:00
Whenever you query the database, hash the query as a key and store the result to the cache. This approach suffers from expiration issues:
- Hard to delete a cached result with complex queries
- If one piece of data changes such as a table cell, you need to delete all cached queries that might include the changed cell
2021-03-21 13:55:58 +03:00
## Caching at the object level
2021-03-14 18:13:28 +03:00
See your data as an object, similar to what you do with your application code. Have your application assemble the dataset from the database into a class instance or a data structure(s) :
- Remove the object from cache if its underlying data has changed
- Allows for asynchronous processing: workers assemble objects by consuming the latest cached object
Suggestions of what to cache:
- User sessions
- Fully rendered web pages
- Activity streams
- User graph data