Use two constructors for default and custom cleanupPeriod

Add code comments

Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
Anant Prakash 2018-05-15 15:47:29 +05:30
parent 0dbe9e3b27
commit 657a62b400
No known key found for this signature in database
GPG key ID: C5D399F626523045
2 changed files with 12 additions and 10 deletions

View file

@ -26,22 +26,23 @@ type txnsMap map[string]*util.JSONResponse
// Cache represents a temporary store for response entries.
// Entries are evicted after a certain period, defined by cleanupPeriod.
// This works by keeping two maps of entries, and cycling the maps after the cleanupPeriod.
type Cache struct {
sync.RWMutex
txnsMaps [2]txnsMap
cleanupPeriod time.Duration
}
// New creates a new Cache object, starts cacheCleanService.
// Takes cleanupPeriod (in minutes) as argument, in case of 0 DefaultCleanupPeriod is used instead.
// Returns a reference to newly created Cache.
func New(cleanupPeriodInMins int) *Cache {
t := Cache{txnsMaps: [2]txnsMap{make(txnsMap), make(txnsMap)}}
// New is a wrapper which calls NewWithCleanupPeriod with DefaultCleanupPeriod as argument.
func New() *Cache {
return NewWithCleanupPeriod(DefaultCleanupPeriod)
}
cleanupPeriod := time.Duration(cleanupPeriodInMins) * time.Minute
if cleanupPeriod == 0 {
cleanupPeriod = DefaultCleanupPeriod
}
// NewWithCleanupPeriod creates a new Cache object, starts cacheCleanService.
// Takes cleanupPeriod as argument.
// Returns a reference to newly created Cache.
func NewWithCleanupPeriod(cleanupPeriod time.Duration) *Cache {
t := Cache{txnsMaps: [2]txnsMap{make(txnsMap), make(txnsMap)}}
t.cleanupPeriod = cleanupPeriod
// Start clean service as the Cache is created
@ -75,6 +76,7 @@ func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) {
// cacheCleanService is responsible for cleaning up entries after cleanupPeriod.
// It guarantees that an entry will be present in cache for at least cleanupPeriod & at most 2 * cleanupPeriod.
// This cycles the txnMaps forward, i.e. back map is assigned the front and front is assigned an empty map.
func cacheCleanService(t *Cache) {
ticker := time.Tick(t.cleanupPeriod)
for range ticker {

View file

@ -30,7 +30,7 @@ var (
// TestCache creates a New Cache and tests AddTransaction & FetchTransaction
func TestCache(t *testing.T) {
fakeTxnCache := New(0)
fakeTxnCache := New()
fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse)
// Add entries for noise.