mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
// EphemeralCache is a cache that can be used to store data in a request level context
|
|
// This is useful for caching data that is expensive to calculate and is likely to be
|
|
// used multiple times in a request.
|
|
type EphemeralCache struct {
|
|
data map[any]map[any]any
|
|
lock sync.RWMutex
|
|
created time.Time
|
|
checkLifeTime time.Duration
|
|
}
|
|
|
|
var timeNow = time.Now
|
|
|
|
func NewEphemeralCache(checkLifeTime ...time.Duration) *EphemeralCache {
|
|
return &EphemeralCache{
|
|
data: make(map[any]map[any]any),
|
|
created: timeNow(),
|
|
checkLifeTime: util.OptionalArg(checkLifeTime, 0),
|
|
}
|
|
}
|
|
|
|
func (cc *EphemeralCache) checkExceededLifeTime(tp, key any) bool {
|
|
if cc.checkLifeTime > 0 && timeNow().Sub(cc.created) > cc.checkLifeTime {
|
|
log.Warn("EphemeralCache is expired, is highly likely to be abused for long-life tasks: %v, %v", tp, key)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (cc *EphemeralCache) Get(tp, key any) (any, bool) {
|
|
if cc.checkExceededLifeTime(tp, key) {
|
|
return nil, false
|
|
}
|
|
cc.lock.RLock()
|
|
defer cc.lock.RUnlock()
|
|
ret, ok := cc.data[tp][key]
|
|
return ret, ok
|
|
}
|
|
|
|
func (cc *EphemeralCache) Put(tp, key, value any) {
|
|
if cc.checkExceededLifeTime(tp, key) {
|
|
return
|
|
}
|
|
|
|
cc.lock.Lock()
|
|
defer cc.lock.Unlock()
|
|
|
|
d := cc.data[tp]
|
|
if d == nil {
|
|
d = make(map[any]any)
|
|
cc.data[tp] = d
|
|
}
|
|
d[key] = value
|
|
}
|
|
|
|
func (cc *EphemeralCache) Delete(tp, key any) {
|
|
if cc.checkExceededLifeTime(tp, key) {
|
|
return
|
|
}
|
|
|
|
cc.lock.Lock()
|
|
defer cc.lock.Unlock()
|
|
delete(cc.data[tp], key)
|
|
}
|
|
|
|
func GetWithEphemeralCache[T, K any](ctx context.Context, c *EphemeralCache, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error) {
|
|
v, has := c.Get(groupKey, targetKey)
|
|
if vv, ok := v.(T); has && ok {
|
|
return vv, nil
|
|
}
|
|
t, err := f(ctx, targetKey)
|
|
if err != nil {
|
|
return t, err
|
|
}
|
|
c.Put(groupKey, targetKey, t)
|
|
return t, nil
|
|
}
|