Skip to content

Commit

Permalink
feat(cache): add cache stats
Browse files Browse the repository at this point in the history
  • Loading branch information
id committed May 22, 2020
1 parent 0785244 commit 11d4487
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
go.sum
16 changes: 15 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,24 @@ func NewSyncCacheClient(opts SyncCacheOpts) *SyncCacheClient {

func (c *SyncCacheClient) AddCacheGroup(cacheGroupName string, getterFunc GetterFunc) {
c.cacheGroupManager.Lock()
c.cacheGroupManager.cacheGroups[cacheGroupName] = NewCacheGroup(getterFunc)
c.cacheGroupManager.cacheGroups[cacheGroupName] = NewCacheGroup(cacheGroupName, getterFunc)
c.cacheGroupManager.Unlock()
}

func (c *SyncCacheClient) GetCacheGroup(cacheGroupName string) *CacheGroup {
c.cacheGroupManager.RUnlock()
g := c.cacheGroupManager.cacheGroups[cacheGroupName]
c.cacheGroupManager.RUnlock()
return g
}

func (c *SyncCacheClient) GetCacheGroups() map[string]*CacheGroup {
c.cacheGroupManager.RUnlock()
g := c.cacheGroupManager.cacheGroups
c.cacheGroupManager.RUnlock()
return g
}

func (c *SyncCacheClient) RemoveCacheGroup(cacheGroupName string) {
c.cacheGroupManager.Lock()
delete(c.cacheGroupManager.cacheGroups, cacheGroupName)
Expand Down
18 changes: 17 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type GetterFunc func(key string, setCacheFunc SetCacheFunc) error
type SetCacheFunc func(i interface{})

type CacheGroup struct {
name string
sync.RWMutex
cache map[string]CacheEntity
getterFunc GetterFunc
Expand All @@ -18,13 +19,28 @@ type CacheEntity struct {
object interface{}
}

func NewCacheGroup(getterFunc GetterFunc) *CacheGroup {
type CacheStats struct {
Items int
}

func NewCacheGroup(name string, getterFunc GetterFunc) *CacheGroup {
return &CacheGroup{
name: name,
cache: make(map[string]CacheEntity, GroupCacheCapacity),
getterFunc: getterFunc,
}
}

func (g *CacheGroup) Name() string {
return g.name
}

func (g *CacheGroup) CacheStats() *CacheStats {
return &CacheStats{
Items: len(g.cache),
}
}

func (g *CacheGroup) set(key string, i interface{}, uuid string) {
g.Lock()
g.cache[key] = CacheEntity{
Expand Down

0 comments on commit 11d4487

Please sign in to comment.