Adrian
Adrian
CC#
Created by Adrian on 4/7/2025 in #help
State of caching in EFCore 2025
Hi. Sorry if the question is banal but I have application using EFCore with postgresql for data access. In my situation whenever I do operation that touches user profile (read/write) I would like to cache the entire user for 10 minutes and then whenever I make changes also apply them to cache and actual db after that, refreshing the 10 minute cache... I was wondering if there's elegant way of doing this 2025? what im using is the IMemoryCache and since virtually every request starts with UserId/UserProfile I have a helper method to get that?
//want some stuff from user
string cacheKey = $"user:{userId}";

if (!_cache.TryGetValue(cacheKey, out User user))
{}
//get my stuff
//want some stuff from user
string cacheKey = $"user:{userId}";

if (!_cache.TryGetValue(cacheKey, out User user))
{}
//get my stuff
Is this the correct approach or there are some more automatic ways of setting this up 2025?
9 replies