Skip to content

Ability to set CacheSettings depending on the result of the valueFactory #215

@celluj34

Description

@celluj34

What problem does the feature solve?

I would like to be able to set the CacheSettings depending on the result of some logic that's run in the valueFactory of the GetOrSetAsync method. This would function similarly to the IMemoryCache.GetOrCreateAsync provided by Microsoft.

The reason this is important to me is, when an exception is thrown, or some logic fails inside the valueFactory, I want the data to not be cached.

How would you use/interact with the feature? (if applicable)

var result = await _cache.GetOrSetAsync<EntityResult<DataModel>>(key,
  async entry =>
  {
    try
    {
      var data = await GetData(model);

      entry.AbsoluteExpirationRelativeToNow = _cacheTimeSpan;

      return new EntityResult<DataModel>(data);
    }
    catch (Exception ex)
    {
      entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromTicks(1);

      return new EntityResult<DataModel>().AddError(ex);
    }
  });

How would you call it in your own code?

See code sample above

Are there constraints that would need to be in place with this feature?

None that can think of - it may be a breaking change, or change how the stacks work.

If I misunderstand how I'm supposed to use this, please let me know. We only do it this way because of some bugs we had where the valueFactorys were being run more than once when the database methods threw an exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions