-
Notifications
You must be signed in to change notification settings - Fork 10
/
PersistenceConfigurationBuilder.cs
101 lines (94 loc) · 4.57 KB
/
PersistenceConfigurationBuilder.cs
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
using LaunchDarkly.Sdk.Client.Internal.DataStores;
using LaunchDarkly.Sdk.Client.Subsystems;
namespace LaunchDarkly.Sdk.Client.Integrations
{
/// <summary>
/// Contains methods for configuring the SDK's persistent storage behavior.
/// </summary>
/// <remarks>
/// <para>
/// The persistent storage mechanism allows the SDK to immediately access the last known flag data
/// for the user, if any, if it was started offline or has not yet received data from LaunchDarkly.
/// </para>
/// <para>
/// By default, the SDK uses a persistence mechanism that is specific to each platform, as
/// described in <see cref="Storage(IComponentConfigurer{IPersistentDataStore})"/>. To use a custom persistence
/// implementation, or to customize related properties defined in this class, create a builder with
/// <see cref="Components.Persistence"/>, change its properties with the methods of this class, and
/// pass it to <see cref="ConfigurationBuilder.Persistence(PersistenceConfigurationBuilder)"/>.
/// </para>
/// </remarks>
/// <example>
/// <code>
/// var config = Configuration.Builder(sdkKey)
/// .Persistence(
/// Components.Persistence().MaxCachedUsers(5)
/// )
/// .Build();
/// </code>
/// </example>
public sealed class PersistenceConfigurationBuilder
{
/// <summary>
/// Default value for <see cref="MaxCachedContexts(int)"/>: 5.
/// </summary>
public const int DefaultMaxCachedContexts = 5;
/// <summary>
/// Passing this value (or any negative number) to <see cref="MaxCachedContexts(int)"/>
/// means there is no limit on cached user data.
/// </summary>
public const int UnlimitedCachedContexts = -1;
private IComponentConfigurer<IPersistentDataStore> _storeFactory = null;
private int _maxCachedContexts = DefaultMaxCachedContexts;
internal PersistenceConfigurationBuilder() { }
/// <summary>
/// Sets the storage implementation.
/// </summary>
/// <remarks>
/// By default, the SDK uses a persistence mechanism that is specific to each platform: on Android and
/// iOS it is the native preferences store, and in the .NET Standard implementation for desktop apps
/// it is the <c>System.IO.IsolatedStorage</c> API. You may use this method to specify a custom
/// implementation using a factory object.
/// </remarks>
/// <param name="persistentDataStoreFactory">a factory for the custom storage implementation, or
/// <see langword="null"/> to use the default implementation</param>
/// <returns>the builder</returns>
public PersistenceConfigurationBuilder Storage(IComponentConfigurer<IPersistentDataStore> persistentDataStoreFactory)
{
_storeFactory = persistentDataStoreFactory;
return this;
}
/// <summary>
/// Sets the maximum number of users to store flag data for.
/// </summary>
/// <remarks>
/// <para>
/// A value greater than zero means that the SDK will use persistent storage to remember the last
/// known flag values for up to that number of unique user keys. If the limit is exceeded, the SDK
/// discards the data for the least recently used user.
/// </para>
/// <para>
/// A value of zero means that the SDK will not use persistent storage; it will only have whatever
/// flag data it has received since the current <c>LdClient</c> instance was started.
/// </para>
/// <para>
/// A value of <see cref="UnlimitedCachedContexts"/> or any other negative number means there is no
/// limit. Use this mode with caution, as it could cause the size of mobile device preferences to
/// grow indefinitely if your application uses many different user keys on the same device.
/// </para>
/// </remarks>
/// <param name="maxCachedContexts"></param>
/// <returns>the builder</returns>
public PersistenceConfigurationBuilder MaxCachedContexts(int maxCachedContexts)
{
_maxCachedContexts = maxCachedContexts;
return this;
}
internal PersistenceConfiguration Build(LdClientContext clientContext) =>
new PersistenceConfiguration(
_storeFactory is null ? PlatformSpecific.LocalStorage.Instance :
_storeFactory.Build(clientContext),
_maxCachedContexts
);
}
}