-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Added a generic LRUCache interface and a default implementation #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
671e159
Skeleton
mikechu-optimizely 3d1ae15
LruCache initial implementation
mikechu-optimizely b987d9d
Add helper extensions
mikechu-optimizely 295fdd2
WIP Fill unit tests
mikechu-optimizely 50fd4e1
WIP corrections based on tests 2 more failing
mikechu-optimizely 22acf8e
Remove SetTimeout, fix for non-passing test
mikechu-optimizely d2e3373
Add copyright notices
mikechu-optimizely d758efb
Remove InternalsVisibleTo for testing
mikechu-optimizely da33c99
Add new line at end of files via .editorconfig
mikechu-optimizely 9fe246e
Readonly and remove excess methods/constructor
mikechu-optimizely 33dc181
Code review corrections
mikechu-optimizely 2f35de3
WIP code review changes
mikechu-optimizely 24737fe
Possibly better solution to cast
mikechu-optimizely e042d99
Move readonlys into constructor
mikechu-optimizely 6749182
Switch to using TimeSpan + refactors
mikechu-optimizely 2bac003
A few more refactors
mikechu-optimizely 03c9abf
Change underlying implementation of LRU
mikechu-optimizely 209c3bd
Attempt to fulfill a default Timespan
mikechu-optimizely 42a735d
Add logging; use 0s instead of descriptive consts
mikechu-optimizely 722780c
Remove Tuple from LRU Cache
mikechu-optimizely ea2f32a
Remove System.ValueTuple NuGet package
mikechu-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections; | ||
mikechu-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace OptimizelySDK.Tests.OdpTests | ||
{ | ||
public static class CollectionTestExtensions | ||
{ | ||
public static string[] ToKeyCollection(this ICollection keys) | ||
mikechu-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var keyArray = new string[keys.Count]; | ||
keys.CopyTo(keyArray, 0); | ||
return keyArray; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using NUnit.Framework; | ||
using OptimizelySDK.Odp; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace OptimizelySDK.Tests.OdpTests | ||
{ | ||
[TestFixture] | ||
public class LruCacheTest | ||
{ | ||
private readonly List<string> _segments1And2 = new List<string> | ||
mikechu-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
"segment1", | ||
"segment2", | ||
}; | ||
|
||
private readonly List<string> _segments3And4 = new List<string> | ||
{ | ||
"segment3", | ||
"segment4", | ||
}; | ||
|
||
private readonly List<string> _segments5And6 = new List<string> | ||
{ | ||
"segment5", | ||
"segment6", | ||
}; | ||
|
||
[Test] | ||
public void ShouldCreateSaveAndLookupOneItem() | ||
{ | ||
var cache = new LruCache<string>(); | ||
Assert.IsNull(cache.Lookup("key1")); | ||
|
||
cache.Save("key1", "value1"); | ||
Assert.AreEqual("value1", cache.Lookup("key1")); | ||
} | ||
|
||
[Test] | ||
public void ShouldSaveAndLookupMultipleItems() | ||
{ | ||
var cache = new LruCache<List<string>>(); | ||
|
||
cache.Save("user1", _segments1And2); | ||
cache.Save("user2", _segments3And4); | ||
cache.Save("user3", _segments5And6); | ||
|
||
var itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
Assert.AreEqual("user1", itemKeys[0]); | ||
Assert.AreEqual("user2", itemKeys[1]); | ||
Assert.AreEqual("user3", itemKeys[2]); | ||
|
||
Assert.AreEqual(_segments1And2, cache.Lookup("user1")); | ||
|
||
itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
// Lookup should move user1 to bottom of the list and push up others. | ||
Assert.AreEqual("user2", itemKeys[0]); | ||
Assert.AreEqual("user3", itemKeys[1]); | ||
Assert.AreEqual("user1", itemKeys[2]); | ||
|
||
Assert.AreEqual(_segments3And4, cache.Lookup("user2")); | ||
|
||
itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
// Lookup should move user2 to bottom of the list and push up others. | ||
Assert.AreEqual("user3", itemKeys[0]); | ||
Assert.AreEqual("user1", itemKeys[1]); | ||
Assert.AreEqual("user2", itemKeys[2]); | ||
|
||
Assert.AreEqual(_segments5And6, cache.Lookup("user3")); | ||
|
||
itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
// Lookup should move user3 to bottom of the list and push up others. | ||
Assert.AreEqual("user1", itemKeys[0]); | ||
Assert.AreEqual("user2", itemKeys[1]); | ||
Assert.AreEqual("user3", itemKeys[2]); | ||
} | ||
|
||
[Test] | ||
public void ShouldReorderListOnSave() | ||
{ | ||
var cache = new LruCache<List<string>>(); | ||
|
||
cache.Save("user1", _segments1And2); | ||
cache.Save("user2", _segments3And4); | ||
cache.Save("user3", _segments5And6); | ||
|
||
var itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
Assert.AreEqual("user1", itemKeys[0]); | ||
Assert.AreEqual("user2", itemKeys[1]); | ||
Assert.AreEqual("user3", itemKeys[2]); | ||
|
||
cache.Save("user1", _segments1And2); | ||
|
||
itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
// save should move user1 to bottom of the list and push up others. | ||
Assert.AreEqual("user2", itemKeys[0]); | ||
Assert.AreEqual("user3", itemKeys[1]); | ||
Assert.AreEqual("user1", itemKeys[2]); | ||
|
||
cache.Save("user2", _segments3And4); | ||
|
||
itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
// save should move user2 to bottom of the list and push up others. | ||
Assert.AreEqual("user3", itemKeys[0]); | ||
Assert.AreEqual("user1", itemKeys[1]); | ||
Assert.AreEqual("user2", itemKeys[2]); | ||
|
||
cache.Save("user3", _segments5And6); | ||
|
||
itemKeys = cache.ReadCurrentCache().Keys.ToKeyCollection(); | ||
// save should move user3 to bottom of the list and push up others. | ||
Assert.AreEqual("user1", itemKeys[0]); | ||
Assert.AreEqual("user2", itemKeys[1]); | ||
Assert.AreEqual("user3", itemKeys[2]); | ||
} | ||
|
||
[Test] | ||
public void ShouldHandleWhenCacheIsDisabled() | ||
{ | ||
var cache = new LruCache<List<string>>(0, LruCache<object>.DEFAULT_TIMEOUT_SECONDS); | ||
|
||
cache.Save("user1", _segments1And2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this possible if we can return |
||
cache.Save("user2", _segments3And4); | ||
cache.Save("user3", _segments5And6); | ||
|
||
Assert.IsNull(cache.Lookup("user1")); | ||
Assert.IsNull(cache.Lookup("user2")); | ||
Assert.IsNull(cache.Lookup("user3")); | ||
} | ||
|
||
[Test] | ||
public void ShouldHandleWhenItemsExpire() | ||
{ | ||
var cache = new LruCache<List<string>>(LruCache<object>.DEFAULT_MAX_SIZE, 1); | ||
|
||
cache.Save("user1", _segments1And2); | ||
|
||
Assert.AreEqual(_segments1And2, cache.Lookup("user1")); | ||
Assert.AreEqual(1, cache.ReadCurrentCache().Count); | ||
|
||
Thread.Sleep(1200); | ||
|
||
Assert.IsNull(cache.Lookup("user1")); | ||
Assert.AreEqual(0, cache.ReadCurrentCache().Count); | ||
} | ||
|
||
[Test] | ||
public void ShouldHandleWhenCacheReachesMaxSize() | ||
{ | ||
var cache = new LruCache<List<string>>(2, LruCache<object>.DEFAULT_TIMEOUT_SECONDS); | ||
|
||
cache.Save("user1", _segments1And2); | ||
cache.Save("user2", _segments3And4); | ||
cache.Save("user3", _segments5And6); | ||
|
||
Assert.AreEqual(2, cache.ReadCurrentCache().Count); | ||
|
||
Assert.AreEqual(_segments5And6, cache.Lookup("user3")); | ||
Assert.AreEqual(_segments3And4, cache.Lookup("user2")); | ||
Assert.IsNull(cache.Lookup("user1")); | ||
} | ||
|
||
[Test] | ||
public void ShouldHandleWhenCacheIsReset() { | ||
var cache = new LruCache<List<string>>(); | ||
|
||
cache.Save("user1", _segments1And2); | ||
cache.Save("user2", _segments3And4); | ||
cache.Save("user3", _segments5And6); | ||
|
||
Assert.AreEqual(_segments1And2, cache.Lookup("user1")); | ||
Assert.AreEqual(_segments3And4, cache.Lookup("user2")); | ||
Assert.AreEqual(_segments5And6, cache.Lookup("user3")); | ||
|
||
Assert.AreEqual(3, cache.ReadCurrentCache().Count); | ||
|
||
cache.Reset(); | ||
|
||
Assert.IsNull(cache.Lookup("user1")); | ||
Assert.IsNull(cache.Lookup("user2")); | ||
Assert.IsNull(cache.Lookup("user3")); | ||
|
||
Assert.AreEqual(0, cache.ReadCurrentCache().Count);} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
mikechu-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace OptimizelySDK.Odp | ||
{ | ||
public static class DateTimeExtension | ||
{ | ||
// Because we're only on .NET Framework 4.5 | ||
public static long ToUnixTimeMilliseconds(this DateTime dateTime) | ||
{ | ||
var unixTimeStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
return Convert.ToInt64(dateTime.Subtract(unixTimeStart).TotalMilliseconds); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace OptimizelySDK.Odp | ||
{ | ||
public interface ICache<T> | ||
mikechu-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
void Save(string key, T value); | ||
T Lookup(string key); | ||
void Reset(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.