Skip to content

[question] Why does DataCache.GetAndChange not change the item in all possible cases? #855

@ixje

Description

@ixje

I'm looking at

public TValue GetAndChange(TKey key, Func<TValue> factory = null)

The name of the function suggests "You get the current item according to a key, and replace that with a new item".

The following is a piece of code where we already found the item in the current cache. The above description matches the if branch but not the else branch of the code below.

if (dictionary.TryGetValue(key, out Trackable trackable))
{
if (trackable.State == TrackState.Deleted)
{
if (factory == null) throw new KeyNotFoundException();
trackable.Item = factory();
trackable.State = TrackState.Changed;
}
else if (trackable.State == TrackState.None)
{
trackable.State = TrackState.Changed;
}

Why are we not updating trackable.Item with factory() like the function name suggests?

To summarise what the function does
We update trackable.item if:

  • the item was found in the cache and its previous state was DELETED
  • the item was not found in cache and not in the real DB

We do not update trackable.item if:

  • the item was found in the cache, but it's previous state is CHANGED or ADDED
  • the item was not found in cache, but found in the DB.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionUsed in questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions