Change Tracker is a library for efficient HTTP caching using database change tracking. It implements 304 Not Modified responses by generating ETags based on database timestamps, reducing server load while ensuring clients always receive current data.
ChangeTracker monitors database changes and generates ETags that combine:
- Assembly write time (when your application was built)
- Database timestamp (last data modification time)
- Custom suffix (optional runtime context)
When a client requests data with a cached ETag, the server compares it with the current state.
- If unchanged: it returns 304 Not Modified and the client uses its cached copy.
- If changed: fresh data is returned with a new ETag.
ETags follow this format:
{AssemblyWriteTime}-{DbTimeStamp}-{Suffix}- Read-heavy applications where data changes less frequently than it's read
- APIs serving semi-static data that changes periodically
- Applications needing reduced server load without compromising data freshness
- PostgreSQL docs
builder.Services
.AddTracker()
.AddNpgsqlProvider<DatabaseContext>();Controller action:
[HttpGet]
[Track(["roles"])]
public ActionResult<IEnumerable<Role>> GetAll() => dbContext.Roles.ToList();Minimal api:
app.MapGet("/roles/getall", (DatabaseContext dbContext) =>
{
return dbContext.Roles.ToList();
})
.WithTracking(options =>
{
options.Tables = ["roles"];
});Middleware:
app.UseTracker(options =>
{
options.Tables = ["roles"];
options.Filter = (httpContext) => httpContext.Request.Path.Value.Contains("/api/roles/getall");
});- Open your application in a browser
- Open Developer Tools (F12)
- Navigate to the Network tab
- Refresh the page
Cached responses will show:
- Status: 304 Not Modified
- Request Header: if-none-match (with ETag value)
- Response Header: etag (current ETag)
To test the full request pipeline:
- Open Developer Tools -> Network tab
- Check "Disable cache" in the toolbar
- Refresh the page
This prevents the browser from sending if-none-match, forcing a cache miss and full server execution.