-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ristretto-based memory-limited cache (#13)
* try out ristretto with a bounded cache * fixed the test * --cacheUseRistretto and v0.0.22 * code inspection fix
- Loading branch information
Showing
9 changed files
with
239 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,89 @@ | ||
package infrastructure | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/dgraph-io/ristretto" | ||
"github.com/patrickmn/go-cache" | ||
"time" | ||
) | ||
|
||
type resultCache interface { | ||
Get(url string) (*URLCheckResult, bool) | ||
Set(url string, res *URLCheckResult) | ||
} | ||
|
||
type ristrettoCache struct { | ||
cache *ristretto.Cache | ||
defaultExpiration time.Duration | ||
} | ||
|
||
func (c ristrettoCache) Set(url string, res *URLCheckResult) { | ||
c.cache.SetWithTTL(url, res, approxSizeOf(url, res), c.defaultExpiration) | ||
} | ||
|
||
func (c ristrettoCache) Get(url string) (*URLCheckResult, bool) { | ||
value, found := c.cache.Get(url) | ||
|
||
if found { | ||
return value.(*URLCheckResult), true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
type defaultCache struct { | ||
cache *cache.Cache | ||
} | ||
|
||
func (c defaultCache) Set(url string, res *URLCheckResult) { | ||
c.cache.Set(url, res, cache.DefaultExpiration) | ||
} | ||
|
||
func (c defaultCache) Get(url string) (*URLCheckResult, bool) { | ||
value, found := c.cache.Get(url) | ||
|
||
if found { | ||
return value.(*URLCheckResult), true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
func newCache(settings cacheSettings) resultCache { | ||
if settings.cacheUseRistretto { | ||
return newRistrettoCache(settings) | ||
} else { | ||
return newDefaultCache(settings) | ||
} | ||
} | ||
|
||
func newRistrettoCache(settings cacheSettings) *ristrettoCache { | ||
// https://github.com/dgraph-io/ristretto#Config | ||
rc, err := ristretto.NewCache(&ristretto.Config{ | ||
NumCounters: settings.cacheNumCounters, // number of keys to track frequency of (~10x max links) | ||
MaxCost: settings.cacheMaxSize, // maximum cost of cache (in bytes) | ||
BufferItems: 64, // number of keys per Get buffer: as recommended | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &ristrettoCache{ | ||
cache: rc, | ||
defaultExpiration: settings.cacheExpirationInterval, | ||
} | ||
} | ||
|
||
func newDefaultCache(settings cacheSettings) *defaultCache { | ||
return &defaultCache{ | ||
cache: cache.New(settings.cacheExpirationInterval, settings.cacheCleanupInterval), | ||
} | ||
} | ||
|
||
func approxSizeOf(key string, res *URLCheckResult) int64 { | ||
bytes, err := json.Marshal(res) | ||
if err != nil { | ||
return 512 + int64(len(key)) // any number should suffice - approximate calculation | ||
} | ||
blob := string(bytes) | ||
return int64(len(blob) + len(key)) | ||
} |
This file contains 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 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
Oops, something went wrong.