mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-14 02:13:10 -06:00
Use two constructors for default and custom cleanupPeriod
Add code comments Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
parent
0dbe9e3b27
commit
657a62b400
|
|
@ -26,22 +26,23 @@ type txnsMap map[string]*util.JSONResponse
|
||||||
|
|
||||||
// Cache represents a temporary store for response entries.
|
// Cache represents a temporary store for response entries.
|
||||||
// Entries are evicted after a certain period, defined by cleanupPeriod.
|
// 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 {
|
type Cache struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
txnsMaps [2]txnsMap
|
txnsMaps [2]txnsMap
|
||||||
cleanupPeriod time.Duration
|
cleanupPeriod time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Cache object, starts cacheCleanService.
|
// New is a wrapper which calls NewWithCleanupPeriod with DefaultCleanupPeriod as argument.
|
||||||
// Takes cleanupPeriod (in minutes) as argument, in case of 0 DefaultCleanupPeriod is used instead.
|
func New() *Cache {
|
||||||
// Returns a reference to newly created Cache.
|
return NewWithCleanupPeriod(DefaultCleanupPeriod)
|
||||||
func New(cleanupPeriodInMins int) *Cache {
|
|
||||||
t := Cache{txnsMaps: [2]txnsMap{make(txnsMap), make(txnsMap)}}
|
|
||||||
|
|
||||||
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
|
t.cleanupPeriod = cleanupPeriod
|
||||||
|
|
||||||
// Start clean service as the Cache is created
|
// 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.
|
// 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.
|
// 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) {
|
func cacheCleanService(t *Cache) {
|
||||||
ticker := time.Tick(t.cleanupPeriod)
|
ticker := time.Tick(t.cleanupPeriod)
|
||||||
for range ticker {
|
for range ticker {
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ var (
|
||||||
|
|
||||||
// TestCache creates a New Cache and tests AddTransaction & FetchTransaction
|
// TestCache creates a New Cache and tests AddTransaction & FetchTransaction
|
||||||
func TestCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
fakeTxnCache := New(0)
|
fakeTxnCache := New()
|
||||||
fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse)
|
fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse)
|
||||||
|
|
||||||
// Add entries for noise.
|
// Add entries for noise.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue