Skip to content

Commit

Permalink
nsqdlookupd: Add cache for nsqlookupd find producers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyxning committed Nov 1, 2019
1 parent f0c92d8 commit b796df8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ github.com/julienschmidt/httprouter 6aacfd5ab513e34f7e64ea9627ab9670371b34e7
github.com/judwhite/go-svc/svc 63c12402f579f0bdf022653c821a1aa5d7544f01
k8s.io/apimachinery/pkg/util/cache 05b5762916b3dbc115737640b04ce9f7c003c793
github.com/vmihailenco/msgpack 88d11f2b24498888c4eccac691122a0f38880f9a
github.com/patrickmn/go-cache 46f407853014144407b6c2ec7ccc76bf67958d93
18 changes: 17 additions & 1 deletion nsqlookupd/registration_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/patrickmn/go-cache"
)

type RegistrationDB struct {
sync.RWMutex
registrationMap map[Registration]ProducerMap

cachedFindProducersResults *cache.Cache
}

type MetaDB struct {
Expand Down Expand Up @@ -59,7 +63,8 @@ func (p *Producer) IsTombstoned(lifetime time.Duration) bool {

func NewRegistrationDB() *RegistrationDB {
return &RegistrationDB{
registrationMap: make(map[Registration]ProducerMap),
registrationMap: make(map[Registration]ProducerMap),
cachedFindProducersResults: cache.New(1*time.Minute, 5*time.Minute),
}
}

Expand Down Expand Up @@ -181,8 +186,16 @@ func (r *RegistrationDB) FindRegistrations(category string, key string, subkey s
func (r *RegistrationDB) FindProducers(category string, key string, subkey string) Producers {
r.RLock()
defer r.RUnlock()

cachedKey := fmt.Sprintf("%s:%s:%s", category, key, subkey)

if val, found := r.cachedFindProducersResults.Get(cachedKey); found {
return val.(Producers)
}

if !r.needFilter(key, subkey) {
k := Registration{category, key, subkey}
r.cachedFindProducersResults.Set(cachedKey, ProducerMap2Slice(r.registrationMap[k]), cache.DefaultExpiration)
return ProducerMap2Slice(r.registrationMap[k])
}

Expand All @@ -200,6 +213,9 @@ func (r *RegistrationDB) FindProducers(category string, key string, subkey strin
}
}
}

r.cachedFindProducersResults.Set(cachedKey, retProducers, cache.DefaultExpiration)

return retProducers
}

Expand Down

0 comments on commit b796df8

Please sign in to comment.