1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

optimize ttl cache.

This commit is contained in:
wdvxdr 2021-07-11 21:50:14 +08:00
parent 8748b9b30d
commit 297b902100
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6
2 changed files with 12 additions and 23 deletions

1
go.sum
View File

@ -16,7 +16,6 @@ github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:x
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=

View File

@ -8,16 +8,15 @@ import (
// https://github.com/Konstantin8105/SimpleTTL
// entry - typical element of cache
type entry struct {
expiry time.Time
value interface{}
expiry *time.Time
}
// Cache - simple implementation of cache
// More information: https://en.wikipedia.org/wiki/Time_to_live
type Cache struct {
timeTTL time.Duration
lock sync.RWMutex
cache map[string]*entry
lock *sync.RWMutex
}
// NewCache - initialization of new cache.
@ -27,13 +26,9 @@ func NewCache(interval time.Duration) *Cache {
if interval < time.Second {
interval = time.Second
}
cache := &Cache{
timeTTL: interval,
cache: make(map[string]*entry),
lock: &sync.RWMutex{},
}
cache := &Cache{cache: make(map[string]*entry)}
go func() {
ticker := time.NewTicker(cache.timeTTL)
ticker := time.NewTicker(interval)
for {
// wait of ticker
now := <-ticker.C
@ -41,7 +36,7 @@ func NewCache(interval time.Duration) *Cache {
// remove entry outside TTL
cache.lock.Lock()
for id, entry := range cache.cache {
if entry.expiry != nil && entry.expiry.Before(now) {
if entry == nil || entry.expiry.Before(now) {
delete(cache.cache, id)
}
}
@ -66,7 +61,7 @@ func (cache *Cache) Get(key string) (interface{}, bool) {
e, ok := cache.cache[key]
if ok && e.expiry != nil && e.expiry.After(time.Now()) {
if ok && e.expiry.After(time.Now()) {
return e.value, true
}
return nil, false
@ -76,11 +71,8 @@ func (cache *Cache) GetAndUpdate(key string, ttl time.Duration) (interface{}, bo
cache.lock.RLock()
defer cache.lock.RUnlock()
e, ok := cache.cache[key]
if ok && e.expiry != nil {
expiry := time.Now().Add(ttl)
e.expiry = &expiry
if e, ok := cache.cache[key]; ok {
e.expiry = time.Now().Add(ttl)
return e.value, true
}
return nil, false
@ -91,20 +83,18 @@ func (cache *Cache) Add(key string, value interface{}, ttl time.Duration) {
cache.lock.Lock()
defer cache.lock.Unlock()
expiry := time.Now().Add(ttl)
cache.cache[key] = &entry{
value: value,
expiry: &expiry,
expiry: time.Now().Add(ttl),
}
}
// GetKeys - return all keys of cache map
func (cache *Cache) GetKeys() []interface{} {
func (cache *Cache) GetKeys() []string {
cache.lock.RLock()
defer cache.lock.RUnlock()
keys := make([]interface{}, len(cache.cache))
keys := make([]string, len(cache.cache))
var i int
for k := range cache.cache {
keys[i] = k