Skip to content

Commit 252ba9e

Browse files
[FSSDK-11177] Cache with remove interface addition
1 parent 70a880d commit 252ba9e

File tree

11 files changed

+57
-15
lines changed

11 files changed

+57
-15
lines changed

OptimizelySDK.NetStandard20/OptimizelySDK.NetStandard20.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@
375375
<Compile Include="..\OptimizelySDK\Utils\Validator.cs">
376376
<Link>Utils\Validator.cs</Link>
377377
</Compile>
378+
<Compile Include="..\OptimizelySDK\Utils\ICacheWithRemove.cs">
379+
<Link>Utils\ICacheWithRemove.cs</Link>
380+
</Compile>
378381
<Compile Include="..\OptimizelySDK\Event\BatchEventProcessor.cs">
379382
<Link>Event\BatchEventProcessor.cs</Link>
380383
</Compile>

OptimizelySDK.Tests/CmabTests/DefaultCmabServiceTest.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using OptimizelySDK.Odp;
2727
using OptimizelySDK.OptimizelyDecisions;
2828
using OptimizelySDK.Tests.Utils;
29+
using OptimizelySDK.Utils;
2930
using AttributeEntity = OptimizelySDK.Entity.Attribute;
3031

3132
namespace OptimizelySDK.Tests.CmabTests
@@ -461,7 +462,7 @@ public void ConstructorAcceptsAnyICacheImplementation()
461462

462463
Assert.IsNotNull(cache);
463464
Assert.AreSame(fakeCache, cache);
464-
Assert.IsInstanceOf<ICache<CmabCacheEntry>>(cache);
465+
Assert.IsInstanceOf<ICacheWithRemove<CmabCacheEntry>>(cache);
465466
}
466467

467468
[Test]
@@ -586,9 +587,9 @@ public void LockStripingDistribution()
586587
"Different user/rule combinations should generally use different locks");
587588
}
588589

589-
private static ICache<CmabCacheEntry> GetInternalCache(DefaultCmabService service)
590+
private static ICacheWithRemove<CmabCacheEntry> GetInternalCache(DefaultCmabService service)
590591
{
591-
return Reflection.GetFieldValue<ICache<CmabCacheEntry>, DefaultCmabService>(service,
592+
return Reflection.GetFieldValue<ICacheWithRemove<CmabCacheEntry>, DefaultCmabService>(service,
592593
"_cmabCache");
593594
}
594595

@@ -598,7 +599,7 @@ private static ICmabClient GetInternalClient(DefaultCmabService service)
598599
"_cmabClient");
599600
}
600601

601-
private sealed class FakeCache : ICache<CmabCacheEntry>
602+
private sealed class FakeCache : ICacheWithRemove<CmabCacheEntry>
602603
{
603604
public void Save(string key, CmabCacheEntry value) { }
604605

OptimizelySDK.Tests/OptimizelyFactoryTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using OptimizelySDK.Notifications;
3030
using OptimizelySDK.Odp;
3131
using OptimizelySDK.Tests.ConfigTest;
32+
using OptimizelySDK.Utils;
3233
using OptimizelySDK.Tests.EventTest;
3334
using OptimizelySDK.Tests.Utils;
3435

@@ -308,7 +309,7 @@ public void NewDefaultInstanceUsesConfiguredCmabCache()
308309
var cmabService = Reflection.GetFieldValue<ICmabService, DecisionService>(decisionService, "CmabService");
309310
Assert.IsInstanceOf<DefaultCmabService>(cmabService);
310311

311-
var cache = Reflection.GetFieldValue<ICache<CmabCacheEntry>, DefaultCmabService>((DefaultCmabService)cmabService, "_cmabCache") as LruCache<CmabCacheEntry>;
312+
var cache = Reflection.GetFieldValue<ICacheWithRemove<CmabCacheEntry>, DefaultCmabService>((DefaultCmabService)cmabService, "_cmabCache") as LruCache<CmabCacheEntry>;
312313
Assert.IsNotNull(cache);
313314
Assert.AreEqual(cacheSize, cache.MaxSizeForTesting);
314315
Assert.AreEqual(cacheTtl, cache.TimeoutForTesting);

OptimizelySDK/Cmab/CmabConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
using System;
18-
using OptimizelySDK.Odp;
18+
using OptimizelySDK.Utils;
1919

2020
namespace OptimizelySDK.Cmab
2121
{
@@ -40,7 +40,7 @@ public CmabConfig(int? cacheSize = null, TimeSpan? cacheTtl = null)
4040
/// Initializes a new instance of the CmabConfig class with a custom cache implementation.
4141
/// </summary>
4242
/// <param name="customCache">Custom cache implementation for CMAB decisions.</param>
43-
public CmabConfig(ICache<CmabCacheEntry> customCache)
43+
public CmabConfig(ICacheWithRemove<CmabCacheEntry> customCache)
4444
{
4545
CustomCache = customCache ?? throw new ArgumentNullException(nameof(customCache));
4646
CacheSize = null;
@@ -63,6 +63,6 @@ public CmabConfig(ICache<CmabCacheEntry> customCache)
6363
/// Gets the custom cache implementation for CMAB decisions.
6464
/// If provided, CacheSize and CacheTtl will be ignored.
6565
/// </summary>
66-
public ICache<CmabCacheEntry> CustomCache { get; }
66+
public ICacheWithRemove<CmabCacheEntry> CustomCache { get; }
6767
}
6868
}

OptimizelySDK/Cmab/DefaultCmabService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
using OptimizelySDK;
2424
using OptimizelySDK.Entity;
2525
using OptimizelySDK.Logger;
26-
using OptimizelySDK.Odp;
2726
using OptimizelySDK.OptimizelyDecisions;
27+
using OptimizelySDK.Utils;
2828
using AttributeEntity = OptimizelySDK.Entity.Attribute;
2929

3030
namespace OptimizelySDK.Cmab
@@ -89,7 +89,7 @@ public class DefaultCmabService : ICmabService
8989
/// </summary>
9090
private const int NUM_LOCK_STRIPES = 1000;
9191

92-
private readonly ICache<CmabCacheEntry> _cmabCache;
92+
private readonly ICacheWithRemove<CmabCacheEntry> _cmabCache;
9393
private readonly ICmabClient _cmabClient;
9494
private readonly ILogger _logger;
9595
private readonly object[] _locks;
@@ -100,7 +100,7 @@ public class DefaultCmabService : ICmabService
100100
/// <param name="cmabCache">Cache for storing CMAB decisions.</param>
101101
/// <param name="cmabClient">Client for fetching decisions from the CMAB prediction service.</param>
102102
/// <param name="logger">Logger for recording service operations.</param>
103-
public DefaultCmabService(ICache<CmabCacheEntry> cmabCache,
103+
public DefaultCmabService(ICacheWithRemove<CmabCacheEntry> cmabCache,
104104
ICmabClient cmabClient,
105105
ILogger logger)
106106
{

OptimizelySDK/Odp/ICache.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ public interface ICache<T>
2222
void Save(string key, T value);
2323
T Lookup(string key);
2424
void Reset();
25-
void Remove(string key);
2625
}
2726
}

OptimizelySDK/Odp/LruCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace OptimizelySDK.Odp
2424
{
25-
public class LruCache<T> : ICache<T> where T : class
25+
public class LruCache<T> : ICacheWithRemove<T> where T : class
2626
{
2727
/// <summary>
2828
/// The maximum number of elements that should be stored

OptimizelySDK/Optimizely.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private void InitializeComponents(IEventDispatcher eventDispatcher = null,
289289
if (cmabService == null)
290290
{
291291
var config = cmabConfig ?? new CmabConfig();
292-
ICache<CmabCacheEntry> cache;
292+
ICacheWithRemove<CmabCacheEntry> cache;
293293

294294
if (config.CustomCache != null)
295295
{

OptimizelySDK/OptimizelyFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#if USE_CMAB
4343
using OptimizelySDK.Cmab;
44+
using OptimizelySDK.Utils;
4445
#endif
4546

4647

@@ -102,7 +103,7 @@ public static void SetCmabCacheConfig(int cacheSize, TimeSpan cacheTtl)
102103
/// Sets a custom cache implementation for CMAB.
103104
/// </summary>
104105
/// <param name="customCache">Custom cache implementation.</param>
105-
public static void SetCmabCustomCache(ICache<CmabCacheEntry> customCache)
106+
public static void SetCmabCustomCache(ICacheWithRemove<CmabCacheEntry> customCache)
106107
{
107108
CmabConfiguration = new CmabConfig(customCache);
108109
}

OptimizelySDK/OptimizelySDK.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<Compile Include="Properties\AssemblyInfo.cs"/>
184184
<Compile Include="Utils\ConfigParser.cs"/>
185185
<Compile Include="Utils\Schema.cs"/>
186+
<Compile Include="Utils\ICacheWithRemove.cs"/>
186187
<Compile Include="Bucketing\UserProfileService.cs"/>
187188
<Compile Include="Bucketing\DecisionService.cs"/>
188189
<Compile Include="ProjectConfig.cs"/>

0 commit comments

Comments
 (0)