-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package common | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type CacheMap[T any] struct { | ||
value map[string]T | ||
err map[string]error | ||
lastUse map[string]time.Time | ||
mu sync.Mutex | ||
null T | ||
} | ||
|
||
func NewCache[T any]() CacheMap[T] { | ||
return CacheMap[T]{ | ||
value: map[string]T{}, | ||
err: map[string]error{}, | ||
lastUse: map[string]time.Time{}, | ||
} | ||
} | ||
|
||
// get returns a value or an error if it exists | ||
// | ||
// if the object key does not exist, it will return both a nil/zero value (of the relevant type) and nil error | ||
func (cache *CacheMap[T]) Get(key string) (T, error) { | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
|
||
if err, ok := cache.err[key]; ok { | ||
cache.lastUse[key] = time.Now() | ||
return cache.null, err | ||
}else if val, ok := cache.value[key]; ok { | ||
cache.lastUse[key] = time.Now() | ||
return val, nil | ||
} | ||
|
||
return cache.null, nil | ||
} | ||
|
||
// set sets or adds a new key with either a value, or an error | ||
func (cache *CacheMap[T]) Set(key string, value T, err error) { | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
|
||
if err != nil { | ||
cache.err[key] = err | ||
delete(cache.value, key) | ||
cache.lastUse[key] = time.Now() | ||
}else{ | ||
cache.value[key] = value | ||
delete(cache.err, key) | ||
cache.lastUse[key] = time.Now() | ||
} | ||
} | ||
|
||
// delOld removes old cache items | ||
func (cache *CacheMap[T]) DelOld(cacheTime time.Duration){ | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
|
||
if cacheTime == 0 { | ||
for key := range cache.lastUse { | ||
delete(cache.value, key) | ||
delete(cache.err, key) | ||
delete(cache.lastUse, key) | ||
} | ||
return | ||
} | ||
|
||
now := time.Now().UnixNano() | ||
|
||
for key, lastUse := range cache.lastUse { | ||
if now - lastUse.UnixNano() > int64(cacheTime) { | ||
delete(cache.value, key) | ||
delete(cache.err, key) | ||
delete(cache.lastUse, key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,2 @@ | ||
github.com/AspieSoft/go-syncterval v1.0.5 h1:fzSNZofSX/7iBkLrWizMzGLGcBl3d+76cHyr8M9tjGg= | ||
github.com/AspieSoft/go-syncterval v1.0.5/go.mod h1:r+mTZPOWvfS0Y5YAxKINGNt8eX76i2Lib0VeqAw3SW4= | ||
github.com/AspieSoft/go-ttlcache v1.2.2 h1:U6MMYY5PKANsB4/vhTiQsQ2U/y1P1yqMCxLw+qFNmCo= | ||
github.com/AspieSoft/go-ttlcache v1.2.2/go.mod h1:czwXaDat1SKWGJDXkoMZWZFip97MXxuU0KzqJ9zVLDo= | ||
github.com/GRbit/go-pcre v1.0.1 h1:8F7Wj1rxIq8ejKSXVVW2wE+4I4VnZbuOemrMk8kn3hc= | ||
github.com/GRbit/go-pcre v1.0.1/go.mod h1:0g7qVGbMpd2Odevd92x1RpaLpR3c3F/Gv2HEnI7CwEA= | ||
github.com/alphadose/haxmap v1.3.0 h1:C/2LboOnPCZP27GmmSXOcwx360st0P8N0fTJ3voefKc= | ||
github.com/alphadose/haxmap v1.3.0/go.mod h1:rjHw1IAqbxm0S3U5tD16GoKsiAd8FWx5BJ2IYqXwgmM= | ||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0= | ||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y= | ||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= | ||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters