server update
This commit is contained in:
parent
1ff168a931
commit
ee8ffc2abd
12 changed files with 214 additions and 1996 deletions
server/pkg/maxcache
|
@ -24,20 +24,22 @@ type Cache[K comparable, V any] struct {
|
|||
callGroup singleflight.Group[K, V]
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Get(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, error) {
|
||||
type DoFunc[V any] func() (V, error)
|
||||
|
||||
func (c *Cache[K, V]) Get(ctx context.Context, key K, fn DoFunc[V]) (V, error) {
|
||||
return c.get(ctx, key, false, fn)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) GetFresh(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, error) {
|
||||
func (c *Cache[K, V]) GetFresh(ctx context.Context, key K, fn DoFunc[V]) (V, error) {
|
||||
return c.get(ctx, key, true, fn)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Set(ctx context.Context, key K, fn singleflight.DoFunc[V]) (V, bool, error) {
|
||||
func (c *Cache[K, V]) Set(ctx context.Context, key K, fn DoFunc[V]) (V, bool, error) {
|
||||
v, err, shared := c.callGroup.Do(key, c.set(key, fn))
|
||||
return v, shared, err
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn singleflight.DoFunc[V]) (V, error) {
|
||||
func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn DoFunc[V]) (V, error) {
|
||||
c.mu.RLock()
|
||||
val, ok := c.values.Get(key)
|
||||
c.mu.RUnlock()
|
||||
|
@ -61,8 +63,8 @@ func (c *Cache[K, V]) get(ctx context.Context, key K, freshOnly bool, fn singlef
|
|||
return v, err
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) set(key K, fn singleflight.DoFunc[V]) singleflight.DoFunc[V] {
|
||||
return singleflight.DoFunc[V](func() (V, error) {
|
||||
func (c *Cache[K, V]) set(key K, fn DoFunc[V]) DoFunc[V] {
|
||||
return DoFunc[V](func() (V, error) {
|
||||
val, err := fn()
|
||||
if err != nil {
|
||||
return val, err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue