Search code examples
asp.net-coredistributed-caching

IDistributedCache SQL Server delete expired records


I am using IDistributedCache with SQL Server in my ASP.Net Core 2.2 API.

I noticed that the expired records remain in the table. Is there any way to delete expired records from the cache table?


Solution

  • The SqlServerCache periodically checks for deleted cache items and deletes them, it uses this SQL command.

    DELETE FROM {0} WHERE @UtcNow > ExpiresAtTime 
    

    Please check you DB table for values of ExpiresAtTime.

    Source of SqlServerCache class: https://github.com/dotnet/aspnetcore/blob/bec278eabea54f63da15e10e654bdfa4168a2479/src/Caching/SqlServer/src/SqlServerCache.cs

    Source of DatabaseOperations class, containing DeleteExpiredCacheItems method: https://github.com/dotnet/aspnetcore/blob/bec278eabea54f63da15e10e654bdfa4168a2479/src/Caching/SqlServer/src/DatabaseOperations.cs