3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
67 lines
1.0 KiB
Go
67 lines
1.0 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type memCache struct {
|
|
opts Options
|
|
|
|
items map[string]Item
|
|
sync.RWMutex
|
|
}
|
|
|
|
func (c *memCache) Get(ctx context.Context, key string) (interface{}, time.Time, error) {
|
|
c.RLock()
|
|
defer c.RUnlock()
|
|
|
|
item, found := c.items[key]
|
|
if !found {
|
|
return nil, time.Time{}, ErrKeyNotFound
|
|
}
|
|
if item.Expired() {
|
|
return nil, time.Time{}, ErrItemExpired
|
|
}
|
|
|
|
return item.Value, time.Unix(0, item.Expiration), nil
|
|
}
|
|
|
|
func (c *memCache) Put(ctx context.Context, key string, val interface{}, d time.Duration) error {
|
|
var e int64
|
|
if d == DefaultExpiration {
|
|
d = c.opts.Expiration
|
|
}
|
|
if d > 0 {
|
|
e = time.Now().Add(d).UnixNano()
|
|
}
|
|
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
|
|
c.items[key] = Item{
|
|
Value: val,
|
|
Expiration: e,
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *memCache) Delete(ctx context.Context, key string) error {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
|
|
_, found := c.items[key]
|
|
if !found {
|
|
return ErrKeyNotFound
|
|
}
|
|
|
|
delete(c.items, key)
|
|
return nil
|
|
}
|
|
|
|
func (c *memCache) String() string {
|
|
return "memory"
|
|
}
|