Depends on the scale. With few users, you'd better query database directly. With enough users (when it makes sense to introduce complexity), I would adopt Change Data Capture pattern. A small process reads/follows database logs (journal) and when a new post or comment is added, updates the cache asynchronously.