63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"hash/fnv"
|
|
"time"
|
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
|
)
|
|
|
|
// hashString generates a hash for a string
|
|
func hashString(s string) uint64 {
|
|
h := fnv.New64a()
|
|
h.Write([]byte(s))
|
|
return h.Sum64()
|
|
}
|
|
|
|
// uint64ToBytes converts a uint64 to bytes for cache key
|
|
func uint64ToBytes(n uint64) []byte {
|
|
b := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(b, n)
|
|
return b
|
|
}
|
|
|
|
// getCacheKey generates a cache key for a method and path
|
|
func getCacheKey(method, path string) []byte {
|
|
key := hashString(method + ":" + path)
|
|
return uint64ToBytes(key)
|
|
}
|
|
|
|
// getBytecodeKey generates a cache key for a handler path
|
|
func getBytecodeKey(handlerPath string) []byte {
|
|
key := hashString(handlerPath)
|
|
return uint64ToBytes(key)
|
|
}
|
|
|
|
// ClearCache clears all caches
|
|
func (r *LuaRouter) ClearCache() {
|
|
r.routeCache.Reset()
|
|
r.bytecodeCache.Reset()
|
|
r.middlewareCache = make(map[string][]byte)
|
|
r.sourceCache = make(map[string][]byte)
|
|
r.sourceMtimes = make(map[string]time.Time)
|
|
}
|
|
|
|
// GetCacheStats returns statistics about the cache
|
|
func (r *LuaRouter) GetCacheStats() map[string]any {
|
|
var routeStats fastcache.Stats
|
|
var bytecodeStats fastcache.Stats
|
|
|
|
r.routeCache.UpdateStats(&routeStats)
|
|
r.bytecodeCache.UpdateStats(&bytecodeStats)
|
|
|
|
return map[string]any{
|
|
"routeEntries": routeStats.EntriesCount,
|
|
"routeBytes": routeStats.BytesSize,
|
|
"routeCollisions": routeStats.Collisions,
|
|
"bytecodeEntries": bytecodeStats.EntriesCount,
|
|
"bytecodeBytes": bytecodeStats.BytesSize,
|
|
"bytecodeCollisions": bytecodeStats.Collisions,
|
|
}
|
|
}
|