-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathEntityCacheOptions.cs
More file actions
110 lines (97 loc) · 4.33 KB
/
EntityCacheOptions.cs
File metadata and controls
110 lines (97 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
namespace Azure.DataApiBuilder.Config.ObjectModel;
/// <summary>
/// Entity specific cache configuration.
/// Properties are nullable to support DAB CLI merge config
/// expected behavior.
/// </summary>
public record EntityCacheOptions
{
/// <summary>
/// Default ttl value for an entity.
/// </summary>
public const int DEFAULT_TTL_SECONDS = 5;
/// <summary>
/// Default cache level for an entity.
/// Placeholder cache level value used when the entity does not explicitly set a level.
/// This value is stored on the EntityCacheOptions object but is NOT used at runtime
/// for resolution — GetEntityCacheEntryLevel() falls through to GlobalCacheEntryLevel()
/// (which infers the level from the runtime Level2 configuration) when UserProvidedLevelOptions is false.
/// </summary>
public const EntityCacheLevel DEFAULT_LEVEL = EntityCacheLevel.L1L2;
/// <summary>
/// The L2 cache provider we support.
/// </summary>
public const string L2_CACHE_PROVIDER = "redis";
/// <summary>
/// Whether the cache should be used for the entity.
/// When null, indicates the user did not explicitly set this property, and the entity
/// should inherit the runtime-level cache enabled setting.
/// Using Enabled.HasValue (rather than a separate UserProvided flag) ensures correct
/// behavior regardless of whether the object was created via JsonConstructor or with-expression.
/// </summary>
[JsonPropertyName("enabled")]
public bool? Enabled { get; init; }
/// <summary>
/// The number of seconds a cache entry is valid before eligible for cache eviction.
/// </summary>
[JsonPropertyName("ttl-seconds")]
public int? TtlSeconds { get; init; }
/// <summary>
/// The cache levels to use for a cache entry.
/// </summary>
[JsonPropertyName("level")]
public EntityCacheLevel? Level { get; init; }
[JsonConstructor]
public EntityCacheOptions(bool? Enabled = null, int? TtlSeconds = null, EntityCacheLevel? Level = null)
{
this.Enabled = Enabled;
if (TtlSeconds is not null)
{
this.TtlSeconds = TtlSeconds;
UserProvidedTtlOptions = true;
}
else
{
this.TtlSeconds = DEFAULT_TTL_SECONDS;
}
if (Level is not null)
{
this.Level = Level;
UserProvidedLevelOptions = true;
}
else
{
this.Level = DEFAULT_LEVEL;
}
}
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write ttl-seconds
/// property and value to the runtime config file.
/// When user doesn't provide the ttl-seconds property/value, which signals DAB to use the default,
/// the DAB CLI should not write the default value to a serialized config.
/// This is because the user's intent is to use DAB's default value which could change
/// and DAB CLI writing the property and value would lose the user's intent.
/// This is because if the user were to use the CLI created config, a ttl-seconds
/// property/value specified would be interpreted by DAB as "user explicitly set ttl."
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(TtlSeconds))]
public bool UserProvidedTtlOptions { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write the Level option
/// property and value to the runtime config file.
/// When user doesn't provide the level property/value, which signals DAB to use the default,
/// the DAB CLI should not write the default value to a serialized config.
/// This is because the user's intent is to use DAB's default value which could change
/// and DAB CLI writing the property and value would lose the user's intent.
/// This is because if the user were to use the CLI created config, a level
/// property/value specified would be interpreted by DAB as "user explicitly set level."
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(Level))]
public bool UserProvidedLevelOptions { get; init; } = false;
}