Translate Chinese comments, log/error messages, validator labels and Swagger annotations to English throughout the source code, generated Swagger docs, config files and CI workflows. Make the English README primary: README.md now holds the English docs and README_EN.md holds the Chinese version, with cross-language links updated accordingly. Note: the generated docs/ swagger files were translated in place; run `go generate ./...` (swag init) to regenerate them from the now-English annotations when a Go toolchain is available. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"sync"
|
|
)
|
|
|
|
// implements a simple cache used for testing
|
|
// SimpleCache is a simple cache implementation
|
|
type SimpleCache struct {
|
|
data map[string]interface{}
|
|
mu sync.Mutex
|
|
maxBytes int64
|
|
usedBytes int64
|
|
}
|
|
|
|
func (s *SimpleCache) Get(key string, value interface{}) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
// use reflection to set the stored value into the passed-in pointer variable
|
|
val := reflect.ValueOf(value)
|
|
if val.Kind() != reflect.Ptr {
|
|
return errors.New("value must be a pointer")
|
|
}
|
|
v, ok := s.data[key]
|
|
if !ok {
|
|
// set to zero value
|
|
val.Elem().Set(reflect.Zero(val.Elem().Type()))
|
|
return nil
|
|
}
|
|
|
|
vval := reflect.ValueOf(v)
|
|
if val.Elem().Type() != vval.Type() {
|
|
// set to zero value
|
|
val.Elem().Set(reflect.Zero(val.Elem().Type()))
|
|
return nil
|
|
}
|
|
|
|
val.Elem().Set(reflect.ValueOf(v))
|
|
return nil
|
|
}
|
|
|
|
func (s *SimpleCache) Set(key string, value interface{}, exp int) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
// check whether the passed-in value is a pointer; if so, dereference it
|
|
val := reflect.ValueOf(value)
|
|
if val.Kind() == reflect.Ptr {
|
|
val = val.Elem()
|
|
}
|
|
|
|
s.data[key] = val.Interface()
|
|
return nil
|
|
}
|
|
func (s *SimpleCache) Gc() error {
|
|
return nil
|
|
}
|
|
|
|
func NewSimpleCache() *SimpleCache {
|
|
return &SimpleCache{
|
|
data: make(map[string]interface{}),
|
|
}
|
|
}
|