initial commit
This commit is contained in:
5
inmemorycache/go.mod
Normal file
5
inmemorycache/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module git.derfenix.pro/fenix/commander/inmemorycache
|
||||
|
||||
go 1.22
|
||||
|
||||
require github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
2
inmemorycache/go.sum
Normal file
2
inmemorycache/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
30
inmemorycache/inmemorycache.go
Normal file
30
inmemorycache/inmemorycache.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package inmemorycache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
type InMemoryCache struct {
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewInMemoryCache() *InMemoryCache {
|
||||
return &InMemoryCache{cache: cache.New(time.Minute, 5*time.Minute)}
|
||||
}
|
||||
|
||||
func (i *InMemoryCache) Set(key string, value []byte, ttl time.Duration) error {
|
||||
i.cache.Set(key, value, ttl)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *InMemoryCache) Get(key string) ([]byte, error) {
|
||||
res, ok := i.cache.Get(key)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return res.([]byte), nil
|
||||
}
|
||||
Reference in New Issue
Block a user