codex optz.

This commit is contained in:
2026-06-20 23:29:58 +08:00
parent 40afc6c0d5
commit ebed85772f
11 changed files with 282 additions and 422 deletions

62
cache/mapsync/map.go vendored Normal file
View File

@@ -0,0 +1,62 @@
package mapsync
import "sync"
type syncMap[T any] struct {
sync.RWMutex
Data map[string]T
}
func newSyncMap[T any]() *syncMap[T] {
return &syncMap[T]{
Data: make(map[string]T),
}
}
func (c *syncMap[T]) Set(key string, val T) {
c.Lock()
defer c.Unlock()
if c.Data == nil {
c.Data = make(map[string]T)
}
c.Data[key] = val
}
func (c *syncMap[T]) Get(key string) T {
c.RLock()
defer c.RUnlock()
return c.Data[key]
}
func (c *syncMap[T]) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *syncMap[T]) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
keys = make([]string, 0, len(c.Data))
for k := range c.Data {
keys = append(keys, k)
}
return keys
}
func (c *syncMap[T]) All() map[string]T {
c.RLock()
defer c.RUnlock()
out := make(map[string]T, len(c.Data))
for k, v := range c.Data {
out[k] = v
}
return out
}

View File

@@ -1,64 +1,12 @@
package mapsync
import (
"sync"
)
var (
// sync map
MapFloat *mapFloat
)
// lock
type mapFloat struct {
sync.RWMutex
Data map[string]float64
}
type mapFloat = syncMap[float64]
func NewMapFloat() *mapFloat {
return &mapFloat{
Data: make(map[string]float64),
}
}
func (c *mapFloat) Set(key string, val float64) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *mapFloat) Get(key string) float64 {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return 0
}
return vals
}
func (c *mapFloat) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *mapFloat) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
}
func (c *mapFloat) All() map[string]float64 {
c.RLock()
defer c.RUnlock()
return c.Data
return newSyncMap[float64]()
}

View File

@@ -1,64 +1,12 @@
package mapsync
import (
"sync"
)
var (
// sync map
MapInt *mapInt
)
// lock
type mapInt struct {
sync.RWMutex
Data map[string]int
}
type mapInt = syncMap[int]
func NewMapInt() *mapInt {
return &mapInt{
Data: make(map[string]int),
}
}
func (c *mapInt) Set(key string, val int) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *mapInt) Get(key string) int {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return 0
}
return vals
}
func (c *mapInt) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *mapInt) All() map[string]int {
c.RLock()
defer c.RUnlock()
return c.Data
}
func (c *mapInt) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
return newSyncMap[int]()
}

View File

@@ -1,64 +1,12 @@
package mapsync
import (
"sync"
)
var (
// sync map
MapString *mapString
)
// lock
type mapString struct {
sync.RWMutex
Data map[string]string
}
type mapString = syncMap[string]
func NewMapString() *mapString {
return &mapString{
Data: make(map[string]string),
}
}
func (c *mapString) Set(key, val string) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *mapString) Get(key string) string {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return ""
}
return vals
}
func (c *mapString) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *mapString) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
}
func (c *mapString) All() map[string]string {
c.RLock()
defer c.RUnlock()
return c.Data
return newSyncMap[string]()
}

35
cache/redis/redis.go vendored
View File

@@ -2,6 +2,7 @@ package redis
import (
"context"
"fmt"
"hash/fnv"
"net/url"
"strconv"
@@ -24,10 +25,22 @@ type RedisClient struct {
}
func New(dsn string, hashRadix string) *RedisClient {
arg, err := url.Parse(dsn)
client, err := NewWithContext(context.Background(), dsn, hashRadix)
if err != nil {
panic(err)
}
return client
}
func NewWithContext(ctx context.Context, dsn string, hashRadix string) (*RedisClient, error) {
if ctx == nil {
ctx = context.Background()
}
arg, err := url.Parse(dsn)
if err != nil {
return nil, fmt.Errorf("parse redis dsn: %w", err)
}
pwd, _ := arg.User.Password()
//get db number,default:0
@@ -36,7 +49,10 @@ func New(dsn string, hashRadix string) *RedisClient {
if arg.Path == "" {
db = Hash(hashRadix)
} else {
db, _ = strconv.Atoi(arg.Path)
db, err = strconv.Atoi(arg.Path)
if err != nil {
return nil, fmt.Errorf("parse redis db index: %w", err)
}
}
//connect redis server
@@ -46,21 +62,26 @@ func New(dsn string, hashRadix string) *RedisClient {
DB: db, // use default DB
Protocol: 3,
})
_, err = client.Ping(context.Background()).Result()
_, err = client.Ping(ctx).Result()
if err != nil {
panic(err)
_ = client.Close()
return nil, fmt.Errorf("ping redis: %w", err)
}
return &RedisClient{
DB: db,
Client: client,
Ctx: context.Background(),
Ctx: ctx,
memory: make(map[string]any),
}
}, nil
}
func Hash(s string) int {
if vars.RedisShardings <= 0 {
return 0
}
h := fnv.New32a()
h.Write([]byte(strings.ToLower(s)))
_, _ = h.Write([]byte(strings.ToLower(s)))
return int(h.Sum32()) % vars.RedisShardings
}