8000 feat: Added a generic LRUCache interface and a default implementation by mikechu-optimizely · Pull Request #311 · optimizely/csharp-sdk · GitHub
[go: up one dir, main page]

Skip to content

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 21 commits into from
Aug 17, 2022
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
671e159
Skeleton
mikechu-optimizely Aug 5, 2022
3d1ae15
LruCache initial implementation
mikechu-optimizely Aug 5, 2022
b987d9d
Add helper extensions
mikechu-optimizely Aug 5, 2022
295fdd2
WIP Fill unit tests
mikechu-optimizely Aug 5, 2022
50fd4e1
WIP corrections based on tests 2 more failing
mikechu-optimizely Aug 5, 2022
22acf8e
Remove SetTimeout, fix for non-passing test
mikechu-optimizely Aug 8, 2022
d2e3373
Add copyright notices
mikechu-optimizely Aug 8, 2022
d758efb
Remove InternalsVisibleTo for testing
mikechu-optimizely Aug 8, 2022
da33c99
Add new line at end of files via .editorconfig
mikechu-optimizely Aug 8, 2022
9fe246e
Readonly and remove excess methods/constructor
mikechu-optimizely Aug 8, 2022
33dc181
Code review corrections
mikechu-optimizely Aug 10, 2022
2f35de3
WIP code review changes
mikechu-optimizely Aug 10, 2022
24737fe
Possibly better solution to cast
mikechu-optimizely Aug 10, 2022
e042d99
Move readonlys into constructor
mikechu-optimizely Aug 10, 2022
6749182
Switch to using TimeSpan + refactors
mikechu-optimizely Aug 11, 2022
2bac003
A few more refactors
mikechu-optimizely Aug 11, 2022
03c9abf
Change underlying implementation of LRU
mikechu-optimizely Aug 12, 2022
209c3bd
Attempt to fulfill a default Timespan
mikechu-optimizely Aug 16, 2022
42a735d
Add logging; use 0s instead of descriptive consts
mikechu-optimizely Aug 16, 2022
722780c
Remove Tuple from LRU Cache
mikechu-optimizely Aug 16, 2022
ea2f32a
Remove System.ValueTuple NuGet package
mikechu-optimizely Aug 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Readonly and remove excess methods/constructor
  • Loading branch information
mikechu-optimizely committed Aug 8, 2022
commit 9fe246e47268c99e6ac674fff1885fa867e68f79
39 changes: 3 additions & 36 deletions OptimizelySDK/Odp/LruCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,19 @@ public class LruCache<T> : ILruCache<T>

private readonly ILogger _logger;
private readonly object _mutex = new object();
private int _maxSize;
private long _timeoutMilliseconds;
private OrderedDictionary _orderedDictionary = new OrderedDictionary();
private readonly int _maxSize;
private readonly long _timeoutMilliseconds;
private readonly OrderedDictionary _orderedDictionary = new OrderedDictionary();

public LruCache() : this(DEFAULT_MAX_SIZE, DEFAULT_TIMEOUT_SECONDS, null) { }

public LruCache(ILogger logger) :
this(DEFAULT_MAX_SIZE, DEFAULT_TIMEOUT_SECONDS, logger) { }


public LruCache(int maxSize, int timeoutSeconds, ILogger logger = null)
{
_maxSize = maxSize < 0 ? default : maxSize;
_timeoutMilliseconds = (timeoutSeconds < 0) ? 0 : (timeoutSeconds * 1000L);
_logger = logger ?? new DefaultLogger();
}

public void SetMaxSize(int size)
{
lock (_mutex)
{
if (_orderedDictionary.Count > 0)
{
if (size >= _orderedDictionary.Count)
{
_maxSize = size;
}
else
{
_logger.Log(LogLevel.WARN,
"Cannot set max cache size less than current size.");
}
}
else
{
var sizeToSet = size;
if (size < 0)
{
sizeToSet = 0;
}

_maxSize = sizeToSet;
}
}
}

public void Save(string key, T value)
{
if (_maxSize == 0)
Expand Down
0