2021-03-01 17:41:43 +03:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2021-03-02 20:26:30 +03:00
|
|
|
package cache
|
2021-03-09 19:03:40 +03:00
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ReneKroon/ttlcache"
|
|
|
|
)
|
|
|
|
|
2021-03-09 19:03:40 +03:00
|
|
|
// Cache defines an in-memory cache that is safe to be wiped when the application is restarted
|
|
|
|
type Cache interface {
|
2021-03-11 16:30:14 +03:00
|
|
|
Store(k string, v interface{}) error
|
|
|
|
Fetch(k string) (interface{}, error)
|
2021-03-09 19:03:40 +03:00
|
|
|
}
|
2021-08-20 13:26:56 +03:00
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
c *ttlcache.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new in-memory cache.
|
|
|
|
func New() Cache {
|
|
|
|
c := ttlcache.NewCache()
|
2021-08-25 16:34:33 +03:00
|
|
|
c.SetTTL(5 * time.Minute)
|
2021-08-20 13:26:56 +03:00
|
|
|
cache := &cache{
|
|
|
|
c: c,
|
|
|
|
}
|
|
|
|
return cache
|
|
|
|
}
|