Skip to content

[Enhancement] Option to Only Update Expiration on IDistributedCache.Refresh (for use with ASPNET Core Session) #65

@kenbailey

Description

@kenbailey

The ASPNET Core 8 session handling is calling a IDistributedCache.Refresh at the end of the request lifecycle if there were no Session.SetString(...) calls. If there was a Session.SetString(...) call, then ASPNET Core appears to not call the IDistributedCache.Refresh. This package uses a call to GetCacheItem(key, includeValue: false) for the refresh, but is also updating the expiration on the GetCacheItem call as well.

My particular use case is for ASPNET Core Session handling and the sliding expiration is quite long (30 mins). As such, I would prefer to not have two sliding window update DB calls per request, but just the single one for performance reasons.

I'm wondering if you would accept a PR that would provide an configuration option for UpdateExpirationOnGet? It could be defaulted to true to keep existing functionality.

There would only need to be a couple of changes to GetCacheItem and GetCacheItemAsync as follows:

if (_updateOnGetCacheItem && (UpdateExpirationOnGet || !includeValue))
{
    var updateCacheItem = new CommandDefinition(
        SqlCommands.UpdateCacheItemSql,
        new ItemIdUtcNow { Id = key, UtcNow = utcNow });
    connection.Execute(updateCacheItem);
}

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions