Skip to content

Cache purge tweaking #2173

@denisborovnev

Description

@denisborovnev

Hi,

We encountered high memory usage by Dapper and mostly due to DynamicParameters.paramReaderCache.

We have multi-tenant application and to overcome parameters sniffing problem we add to some of our queries comment like -- TenantId = XX UserId = YY. I know that we can use RECOMPILE hint, but we intentionally use our approach to let sql server cache execution plans. Also some queries are generated dynamically based on user selection (filters, columns etc.).
All these leads to a huge amount of different queries and cache entries.

The same about SqlMapper._queryCache, but it is not as big as paramReaderCache because Dapper purges queries that have been executed only once (if I'm not mistaken about it).

Like a workaround we can replace such code

var parameters = new DynamicParameters();
parameters.AddDynamicParams(new { TenantId = tenantId, UserId = userId });

with

var parameters = new DynamicParameters();
parameters.Add("TenantId", tenantId);
parameters.Add("UserId", userId);

But we don't want to restrict our usage of library.

I would be happy try to add ability to configure cache purging policy that wouldn't make any changes to existing behavior by default but would allow to configure some limits for cache size.

But before trying to implement it I would like to ask if you are open to such changes and if you have any suggestions how to do it in the best way.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions