feat: improve perf by caching proxy and layers locally
This commit is contained in:
34
internal/cache/memory/cache.go
vendored
Normal file
34
internal/cache/memory/cache.go
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
cache "forge.cadoles.com/cadoles/bouncer/internal/cache"
|
||||
)
|
||||
|
||||
type Cache[K comparable, V any] struct {
|
||||
store *sync.Map
|
||||
}
|
||||
|
||||
// Get implements cache.Cache.
|
||||
func (c *Cache[K, V]) Get(key K) (V, bool) {
|
||||
raw, exists := c.store.Load(key)
|
||||
if !exists {
|
||||
return *new(V), false
|
||||
}
|
||||
|
||||
return raw.(V), exists
|
||||
}
|
||||
|
||||
// Set implements cache.Cache.
|
||||
func (c *Cache[K, V]) Set(key K, value V) {
|
||||
c.store.Store(key, value)
|
||||
}
|
||||
|
||||
func NewCache[K comparable, V any]() *Cache[K, V] {
|
||||
return &Cache[K, V]{
|
||||
store: new(sync.Map),
|
||||
}
|
||||
}
|
||||
|
||||
var _ cache.Cache[string, bool] = &Cache[string, bool]{}
|
Reference in New Issue
Block a user