Skip to content

Commit

Permalink
Merge pull request #132 from icey-yu/add-cache
Browse files Browse the repository at this point in the history
feat: cache
  • Loading branch information
icey-yu authored Sep 5, 2024
2 parents f4b3898 + 113d0d4 commit 6026390
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions db/cacheutil/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cacheutil

import "sync"

// Cache is a Generic sync.Map structure.
type Cache[K comparable, V any] struct {
m sync.Map
}

func NewCache[K comparable, V any]() *Cache[K, V] {
return &Cache[K, V]{}
}

// Load returns the value stored in the map for a key, or nil if no value is present.
func (c *Cache[K, V]) Load(key K) (value V, ok bool) {
rawValue, ok := c.m.Load(key)
if !ok {
return
}
return rawValue.(V), ok
}

// Store sets the value for a key.
func (c *Cache[K, V]) Store(key K, value V) {
c.m.Store(key, value)
}

// StoreAll sets all value by f's key.
func (c *Cache[K, V]) StoreAll(f func(value V) K, values []V) {
for _, v := range values {
c.m.Store(f(v), v)
}
}

// LoadOrStore returns the existing value for the key if present.
func (c *Cache[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
rawValue, loaded := c.m.LoadOrStore(key, value)
return rawValue.(V), loaded
}

// Delete deletes the value for a key.
func (c *Cache[K, V]) Delete(key K) {
c.m.Delete(key)
}

// DeleteAll deletes all values.
func (c *Cache[K, V]) DeleteAll() {
c.m.Range(func(key, value interface{}) bool {
c.m.Delete(key)
return true
})
}

// RangeAll returns all values in the map.
func (c *Cache[K, V]) RangeAll() (values []V) {
c.m.Range(func(rawKey, rawValue interface{}) bool {
values = append(values, rawValue.(V))
return true
})
return values
}

// RangeCon returns values in the map that satisfy condition f.
func (c *Cache[K, V]) RangeCon(f func(key K, value V) bool) (values []V) {
c.m.Range(func(rawKey, rawValue interface{}) bool {
if f(rawKey.(K), rawValue.(V)) {
values = append(values, rawValue.(V))
}
return true
})
return values
}

0 comments on commit 6026390

Please sign in to comment.