Skip to content

Commit

Permalink
Merge pull request #70 from CodFrm/main
Browse files Browse the repository at this point in the history
refactor cache
  • Loading branch information
Mzack9999 authored Feb 7, 2023
2 parents 5c19d18 + 44a7b56 commit 3a79675
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
48 changes: 25 additions & 23 deletions store/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ type Cache interface {
}

type CacheMemory struct {
*cacheMemory
}

type cacheMemory struct {
DefaultExpiration time.Duration
Items map[string]Item
mu sync.RWMutex
onEvicted func(string, interface{})
janitor *janitor
}

func (c *CacheMemory) SetWithExpiration(k string, x interface{}, d time.Duration) {
func (c *cacheMemory) SetWithExpiration(k string, x interface{}, d time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.set(k, x, d)
}

func (c *CacheMemory) set(k string, x interface{}, d time.Duration) {
func (c *cacheMemory) set(k string, x interface{}, d time.Duration) {
var e int64
if d == DefaultExpiration {
d = c.DefaultExpiration
Expand All @@ -51,11 +55,11 @@ func (c *CacheMemory) set(k string, x interface{}, d time.Duration) {
}
}

func (c *CacheMemory) Set(k string, x interface{}) {
func (c *cacheMemory) Set(k string, x interface{}) {
c.SetWithExpiration(k, x, c.DefaultExpiration)
}

func (c *CacheMemory) Get(k string) (interface{}, bool) {
func (c *cacheMemory) Get(k string) (interface{}, bool) {
c.mu.RLock()
item, found := c.Items[k]
if !found {
Expand All @@ -73,7 +77,7 @@ func (c *CacheMemory) Get(k string) (interface{}, bool) {
return item.Object, true
}

func (c *CacheMemory) refresh(k string) bool {
func (c *cacheMemory) refresh(k string) bool {
c.mu.Lock()
defer c.mu.Unlock()

Expand All @@ -85,7 +89,7 @@ func (c *CacheMemory) refresh(k string) bool {
return true
}

func (c *CacheMemory) Delete(k string) {
func (c *cacheMemory) Delete(k string) {
c.mu.Lock()
v, evicted := c.delete(k)
c.mu.Unlock()
Expand All @@ -94,7 +98,7 @@ func (c *CacheMemory) Delete(k string) {
}
}

func (c *CacheMemory) delete(k string) (interface{}, bool) {
func (c *cacheMemory) delete(k string) (interface{}, bool) {
if c.onEvicted != nil {
if v, found := c.Items[k]; found {
delete(c.Items, k)
Expand All @@ -106,7 +110,7 @@ func (c *CacheMemory) delete(k string) (interface{}, bool) {
}

// Delete all expired items from the cache.
func (c *CacheMemory) DeleteExpired() {
func (c *cacheMemory) DeleteExpired() {
var evictedItems []keyAndValue
now := time.Now().UnixNano()
c.mu.Lock()
Expand All @@ -125,13 +129,13 @@ func (c *CacheMemory) DeleteExpired() {
}
}

func (c *CacheMemory) OnEvicted(f func(string, interface{})) {
func (c *cacheMemory) OnEvicted(f func(string, interface{})) {
c.mu.Lock()
defer c.mu.Unlock()
c.onEvicted = f
}

func (c *CacheMemory) Scan(f func([]byte, []byte) error) {
func (c *cacheMemory) Scan(f func([]byte, []byte) error) {
c.mu.Lock()
defer c.mu.Unlock()

Expand All @@ -142,7 +146,7 @@ func (c *CacheMemory) Scan(f func([]byte, []byte) error) {
}
}

func (c *CacheMemory) CloneItems() map[string]Item {
func (c *cacheMemory) CloneItems() map[string]Item {
c.mu.RLock()
defer c.mu.RUnlock()
m := make(map[string]Item, len(c.Items))
Expand All @@ -159,46 +163,44 @@ func (c *CacheMemory) CloneItems() map[string]Item {
return m
}

func (c *CacheMemory) ItemCount() int {
func (c *cacheMemory) ItemCount() int {
c.mu.RLock()
defer c.mu.RUnlock()
n := len(c.Items)

return n
}

func (c *CacheMemory) Empty() {
func (c *cacheMemory) Empty() {
c.mu.Lock()
defer c.mu.Unlock()

c.Items = map[string]Item{}
}

func newCache(de time.Duration, m map[string]Item) *CacheMemory {
func newCache(de time.Duration, m map[string]Item) *cacheMemory {
if de == 0 {
de = -1
}
c := &CacheMemory{
c := &cacheMemory{
DefaultExpiration: de,
Items: m,
}
return c
}

type finalizerWrapper struct {
*CacheMemory
}

func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *CacheMemory {
c := newCache(de, m)
w := &CacheMemory{
cacheMemory: c,
}
if ci > 0 {
runJanitor(c, ci)
wrapper := &finalizerWrapper{CacheMemory: c}
runtime.SetFinalizer(wrapper, func(w *finalizerWrapper) {
stopJanitor(w.CacheMemory)
runtime.SetFinalizer(w, func(c *CacheMemory) {
stopJanitor(c.cacheMemory)
})
}
return c
return w
}

func New(defaultExpiration, cleanupInterval time.Duration) *CacheMemory {
Expand Down
6 changes: 3 additions & 3 deletions store/cache/janitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type janitor struct {
stop chan struct{}
}

func (j *janitor) Run(c *CacheMemory) {
func (j *janitor) Run(c *cacheMemory) {
ticker := time.NewTicker(j.Interval)
for {
select {
Expand All @@ -22,11 +22,11 @@ func (j *janitor) Run(c *CacheMemory) {
}
}

func stopJanitor(c *CacheMemory) {
func stopJanitor(c *cacheMemory) {
c.janitor.stop <- struct{}{}
}

func runJanitor(c *CacheMemory, ci time.Duration) {
func runJanitor(c *cacheMemory, ci time.Duration) {
j := &janitor{
Interval: ci,
stop: make(chan struct{}),
Expand Down

0 comments on commit 3a79675

Please sign in to comment.