C#C
C#3y ago
SWEETPONY

✅ strange behavior with dictionary keys

I have following code:
public class UISchemaGenerator
{
    private record CacheKey(
        Either<CustomFieldSet, CustomField.CustomField> Type,
        CultureInfo Culture);

    private readonly ConcurrentDictionary<CacheKey, UISchemaSpecification> _cache = new();

    public UISchemaSpecification Generate(
        Either<CustomFieldSet, CustomField.CustomField> eitherField,
        MandatorySecurityScope scope = default,
        CultureInfo culture = default)
    {
        culture ??= CultureInfo.CurrentCulture;

        return eitherField.Match(
            ifA: cfs => _cache.GetOrAdd(
                new CacheKey(cfs, culture),
                _ => InternalGenerator(cfs, scope, culture)),
            ifB: cf => _cache.GetOrAdd(
                new CacheKey(cf, culture),
                _ => InternalGenerator(cf, culture)));
    }


CustomField.CustomField is object with some properties
problem: if value of one of the properties will change I will not call InternalGenerator, I will use old value from cache but actually property value has changed
Was this page helpful?