Junior — Middle
What does the expiration date of cached data mean and how can it be determined?
sobes.tech AI
Answer from AI
The expiration time of cached data is the period after which the data in the cache is considered outdated and should be updated or removed. This helps prevent the use of stale information and manage memory usage.
In C#, when working with cache (for example, MemoryCache), the expiration time can be set using the following properties:
- AbsoluteExpiration — a fixed time when the cache expires.
- SlidingExpiration — a period of inactivity after which the item is removed if not accessed.
Example of setting an expiration time:
var cache = MemoryCache.Default;
var policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10) // cache will expire in 10 minutes
};
cache.Set("key", "value", policy);
Thus, the expiration time determines how long data will be stored in the cache before it is updated or removed.