mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
optimize ttl cache.
This commit is contained in:
parent
8748b9b30d
commit
297b902100
1
go.sum
1
go.sum
@ -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.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-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.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/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.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
32
utils/ttl.go
32
utils/ttl.go
@ -8,16 +8,15 @@ import (
|
|||||||
// https://github.com/Konstantin8105/SimpleTTL
|
// https://github.com/Konstantin8105/SimpleTTL
|
||||||
// entry - typical element of cache
|
// entry - typical element of cache
|
||||||
type entry struct {
|
type entry struct {
|
||||||
|
expiry time.Time
|
||||||
value interface{}
|
value interface{}
|
||||||
expiry *time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache - simple implementation of cache
|
// Cache - simple implementation of cache
|
||||||
// More information: https://en.wikipedia.org/wiki/Time_to_live
|
// More information: https://en.wikipedia.org/wiki/Time_to_live
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
timeTTL time.Duration
|
lock sync.RWMutex
|
||||||
cache map[string]*entry
|
cache map[string]*entry
|
||||||
lock *sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCache - initialization of new cache.
|
// NewCache - initialization of new cache.
|
||||||
@ -27,13 +26,9 @@ func NewCache(interval time.Duration) *Cache {
|
|||||||
if interval < time.Second {
|
if interval < time.Second {
|
||||||
interval = time.Second
|
interval = time.Second
|
||||||
}
|
}
|
||||||
cache := &Cache{
|
cache := &Cache{cache: make(map[string]*entry)}
|
||||||
timeTTL: interval,
|
|
||||||
cache: make(map[string]*entry),
|
|
||||||
lock: &sync.RWMutex{},
|
|
||||||
}
|
|
||||||
go func() {
|
go func() {
|
||||||
ticker := time.NewTicker(cache.timeTTL)
|
ticker := time.NewTicker(interval)
|
||||||
for {
|
for {
|
||||||
// wait of ticker
|
// wait of ticker
|
||||||
now := <-ticker.C
|
now := <-ticker.C
|
||||||
@ -41,7 +36,7 @@ func NewCache(interval time.Duration) *Cache {
|
|||||||
// remove entry outside TTL
|
// remove entry outside TTL
|
||||||
cache.lock.Lock()
|
cache.lock.Lock()
|
||||||
for id, entry := range cache.cache {
|
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)
|
delete(cache.cache, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,7 +61,7 @@ func (cache *Cache) Get(key string) (interface{}, bool) {
|
|||||||
|
|
||||||
e, ok := cache.cache[key]
|
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 e.value, true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
@ -76,11 +71,8 @@ func (cache *Cache) GetAndUpdate(key string, ttl time.Duration) (interface{}, bo
|
|||||||
cache.lock.RLock()
|
cache.lock.RLock()
|
||||||
defer cache.lock.RUnlock()
|
defer cache.lock.RUnlock()
|
||||||
|
|
||||||
e, ok := cache.cache[key]
|
if e, ok := cache.cache[key]; ok {
|
||||||
|
e.expiry = time.Now().Add(ttl)
|
||||||
if ok && e.expiry != nil {
|
|
||||||
expiry := time.Now().Add(ttl)
|
|
||||||
e.expiry = &expiry
|
|
||||||
return e.value, true
|
return e.value, true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
@ -91,20 +83,18 @@ func (cache *Cache) Add(key string, value interface{}, ttl time.Duration) {
|
|||||||
cache.lock.Lock()
|
cache.lock.Lock()
|
||||||
defer cache.lock.Unlock()
|
defer cache.lock.Unlock()
|
||||||
|
|
||||||
expiry := time.Now().Add(ttl)
|
|
||||||
|
|
||||||
cache.cache[key] = &entry{
|
cache.cache[key] = &entry{
|
||||||
value: value,
|
value: value,
|
||||||
expiry: &expiry,
|
expiry: time.Now().Add(ttl),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetKeys - return all keys of cache map
|
// GetKeys - return all keys of cache map
|
||||||
func (cache *Cache) GetKeys() []interface{} {
|
func (cache *Cache) GetKeys() []string {
|
||||||
cache.lock.RLock()
|
cache.lock.RLock()
|
||||||
defer cache.lock.RUnlock()
|
defer cache.lock.RUnlock()
|
||||||
|
|
||||||
keys := make([]interface{}, len(cache.cache))
|
keys := make([]string, len(cache.cache))
|
||||||
var i int
|
var i int
|
||||||
for k := range cache.cache {
|
for k := range cache.cache {
|
||||||
keys[i] = k
|
keys[i] = k
|
||||||
|
Loading…
x
Reference in New Issue
Block a user