mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-30 00:16:16 -06:00
use ttlcache
This commit is contained in:
parent
73fc5cf2f0
commit
cf1dcdf46a
7 changed files with 15 additions and 66 deletions
15
internal/cache/cache.go
vendored
15
internal/cache/cache.go
vendored
|
|
@ -19,8 +19,9 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ReneKroon/ttlcache"
|
||||
)
|
||||
|
||||
// Cache defines an in-memory cache that is safe to be wiped when the application is restarted
|
||||
|
|
@ -30,19 +31,15 @@ type Cache interface {
|
|||
}
|
||||
|
||||
type cache struct {
|
||||
stored *sync.Map
|
||||
c *ttlcache.Cache
|
||||
}
|
||||
|
||||
// New returns a new in-memory cache.
|
||||
func New() Cache {
|
||||
c := ttlcache.NewCache()
|
||||
c.SetTTL(30 * time.Second)
|
||||
cache := &cache{
|
||||
stored: &sync.Map{},
|
||||
c: c,
|
||||
}
|
||||
go cache.sweep()
|
||||
return cache
|
||||
}
|
||||
|
||||
type cacheEntry struct {
|
||||
updated time.Time
|
||||
value interface{}
|
||||
}
|
||||
|
|
|
|||
9
internal/cache/fetch.go
vendored
9
internal/cache/fetch.go
vendored
|
|
@ -19,15 +19,10 @@
|
|||
package cache
|
||||
|
||||
func (c *cache) Fetch(k string) (interface{}, error) {
|
||||
ceI, stored := c.stored.Load(k)
|
||||
i, stored := c.c.Get(k)
|
||||
if !stored {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
ce, ok := ceI.(*cacheEntry)
|
||||
if !ok {
|
||||
panic("cache entry was not a *cacheEntry -- this should never happen")
|
||||
}
|
||||
|
||||
return ce.value, nil
|
||||
return i, nil
|
||||
}
|
||||
|
|
|
|||
9
internal/cache/store.go
vendored
9
internal/cache/store.go
vendored
|
|
@ -18,14 +18,7 @@
|
|||
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
|
||||
func (c *cache) Store(k string, v interface{}) error {
|
||||
ce := &cacheEntry{
|
||||
updated: time.Now(),
|
||||
value: v,
|
||||
}
|
||||
|
||||
c.stored.Store(k, ce)
|
||||
c.c.Set(k, v)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
42
internal/cache/sweep.go
vendored
42
internal/cache/sweep.go
vendored
|
|
@ -1,42 +0,0 @@
|
|||
/*
|
||||
GoToSocial
|
||||
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
|
||||
// sweep removes all entries more than 5 minutes old, on a loop.
|
||||
func (c *cache) sweep() {
|
||||
t := time.NewTicker(5 * time.Minute)
|
||||
for range t.C {
|
||||
toRemove := []interface{}{}
|
||||
c.stored.Range(func(key interface{}, value interface{}) bool {
|
||||
ce, ok := value.(*cacheEntry)
|
||||
if !ok {
|
||||
panic("cache entry was not a *cacheEntry -- this should never happen")
|
||||
}
|
||||
if ce.updated.Add(5 * time.Minute).After(time.Now()) {
|
||||
toRemove = append(toRemove, key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
for _, r := range toRemove {
|
||||
c.stored.Delete(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue