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
(COMMON): Basic (Least Recently Used) LRU caching system #420
Conversation
Also I have not added the ability to "Peek" (get a keys value without moving it to the front of the cache) or List all entires as I personally feel this goes against the point of of a LRU cache as you are accessing it and it was recently used Happy to add if people want this functionality |
Codecov Report
@@ Coverage Diff @@
## master #420 +/- ##
==========================================
+ Coverage 41.12% 41.33% +0.21%
==========================================
Files 164 182 +18
Lines 38678 41950 +3272
==========================================
+ Hits 15906 17340 +1434
- Misses 21639 23167 +1528
- Partials 1133 1443 +310
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome stuff! Great first caching algo with many more to come (FIFO/LIFO etc). Just a few minor nits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no nits, except what Thrasher has suggested. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making those changes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So nice and clean. Very nice 🎉 Just a few minor observations and requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making those changes! Also confirmed it works concurrently. Looking forward to seeing its use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last minor nits just associated with comments to keep golint happy and to match them up
common/cache/cache.go
Outdated
return false | ||
} | ||
|
||
// Contain checks if cache contains key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Contains
common/cache/cache.go
Outdated
return l.lru.getNewest() | ||
} | ||
|
||
// Contain checks if cache contains key if not adds to cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// ContainsOrAdd checks if cache contains key and if not, adds it to the cache
common/cache/cache.go
Outdated
return l.lru.getOldest() | ||
} | ||
|
||
// Get looks up a key's value from the cache. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns the newest k,v from the LRU
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an awesome addition well done!
…rp#420) * started adding basic lru cache system * Added basic LRU cache including Add Get Remove Contains ContainsOrAdd Clear * wording changes on comments * removed exported var's in strut as they are not required * Added README * README updates * rm line :D * swapped to mutex over rwmutex updated comments * unexported getNewest & getOldest * unexported getNewest & getOldest * Updated comments and cited references in source * updated comments
PR Description
This PR adds a basic least recently used cache system that will be used as part of #409 (and others parts if people find it useful)
But as I learned my lesson with #383 and its 15,000 changes I decided to break it up into smaller PR's for additional non-directly related functionality
LRU is a type of caching style that stores elements and moves the most recently accessed to the front and disgards the least used if it would grow beyond capacity
There is both a concurrent safe( with mutex)
or called directly for non-concurrent safe version:
Obviously up to user to decide on requirements as if they need it to be goroutine safe or not and performance of mutexs
Type of change
Please delete options that are not relevant and add an
x
in[]
as item is complete.How has this been tested
Checklist