-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLStorageProvider.cs
More file actions
60 lines (50 loc) · 2.26 KB
/
Copy pathSQLStorageProvider.cs
File metadata and controls
60 lines (50 loc) · 2.26 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
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using FeatureOne.Cache;
using FeatureOne.Core;
using FeatureOne.Core.Stores;
using FeatureOne.Json;
[assembly: InternalsVisibleTo("FeatureOne.SQL.Tests")]
namespace FeatureOne.SQL.StorageProvider
{
public class SQLStorageProvider : IStorageProvider
{
internal readonly IDbRepository repository;
internal readonly IToggleDeserializer deserializer;
internal readonly ICache cache;
internal readonly CacheSettings cacheSettings;
public SQLStorageProvider(SQLConfiguration configuration, ICache cache = null, IConditionDeserializer conditionDeserializer = null)
{
this.cacheSettings = configuration?.CacheSettings ?? new CacheSettings();
this.repository = new DbRepository(configuration);
this.deserializer = new ToggleDeserializer(conditionDeserializer ?? new ConditionDeserializer());
this.cache = cache ?? new FeatureCache();
}
public SQLStorageProvider(IDbRepository repository, IToggleDeserializer deserializer, ICache cache, CacheSettings cacheSettings)
{
this.repository = repository;
this.deserializer = deserializer ?? new ToggleDeserializer(new ConditionDeserializer());
this.cache = cache ?? new FeatureCache();
this.cacheSettings = cacheSettings ?? new CacheSettings();
}
public IFeature[] GetByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
DbRecord[] dbFeatures = null;
if (cacheSettings.EnableCache)
dbFeatures = (DbRecord[])cache?.Get(name);
if (dbFeatures == null)
dbFeatures = repository.GetByName(name);
if (cacheSettings.EnableCache)
{
var policy = cacheSettings.Expiry.GetPolicy();
cache.Add(name, dbFeatures, policy);
}
return dbFeatures != null && dbFeatures.Any()
? dbFeatures.Where(x => !string.IsNullOrEmpty(x.Toggle)).Select(f => new Feature(f.Name, deserializer.Deserialize(f.Toggle))).ToArray()
: Array.Empty<IFeature>();
}
}
}