optimized_cache.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package searcher
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "sort"
  8. "strconv"
  9. "time"
  10. "github.com/0xJacky/Nginx-UI/internal/nginx_log/utils"
  11. "github.com/dgraph-io/ristretto/v2"
  12. )
  13. // OptimizedSearchCache provides high-performance caching using Ristretto
  14. type OptimizedSearchCache struct {
  15. cache *ristretto.Cache[string, *SearchResult]
  16. }
  17. // NewOptimizedSearchCache creates a new optimized cache with Ristretto
  18. func NewOptimizedSearchCache(maxSize int64) *OptimizedSearchCache {
  19. cache, err := ristretto.NewCache(&ristretto.Config[string, *SearchResult]{
  20. NumCounters: maxSize * 10, // Number of keys to track frequency of (10x cache size)
  21. MaxCost: maxSize, // Maximum cost of cache (number of items)
  22. BufferItems: 64, // Number of keys per Get buffer
  23. Metrics: true, // Enable metrics collection
  24. })
  25. if err != nil {
  26. panic(fmt.Sprintf("failed to create cache: %v", err))
  27. }
  28. return &OptimizedSearchCache{
  29. cache: cache,
  30. }
  31. }
  32. // GenerateOptimizedKey generates an efficient cache key for a search request
  33. func (osc *OptimizedSearchCache) GenerateOptimizedKey(req *SearchRequest) string {
  34. // Create a unique key based on ALL relevant search parameters, including log paths and filters.
  35. // To avoid order-sensitive keys, sort all slice fields and deduplicate where appropriate.
  36. // Build sorted copies to avoid mutating the original request
  37. copyStrings := func(src []string, doSort bool, dedup bool) []string {
  38. if len(src) == 0 {
  39. return nil
  40. }
  41. res := make([]string, 0, len(src))
  42. if dedup {
  43. seen := make(map[string]struct{}, len(src))
  44. for _, s := range src {
  45. if _, ok := seen[s]; ok {
  46. continue
  47. }
  48. seen[s] = struct{}{}
  49. res = append(res, s)
  50. }
  51. } else {
  52. res = append(res, src...)
  53. }
  54. if doSort {
  55. sort.Strings(res)
  56. }
  57. return res
  58. }
  59. copyInts := func(src []int, doSort bool) []int {
  60. if len(src) == 0 {
  61. return nil
  62. }
  63. res := append([]int(nil), src...)
  64. if doSort {
  65. sort.Ints(res)
  66. }
  67. return res
  68. }
  69. keyData := struct {
  70. Query string `json:"query"`
  71. Limit int `json:"limit"`
  72. Offset int `json:"offset"`
  73. SortBy string `json:"sort_by"`
  74. SortOrder string `json:"sort_order"`
  75. StartTime *int64 `json:"start_time"`
  76. EndTime *int64 `json:"end_time"`
  77. UseMainLogPath bool `json:"use_main_log_path"`
  78. LogPaths []string `json:"log_paths"`
  79. Fields []string `json:"fields"`
  80. IPAddresses []string `json:"ip_addresses"`
  81. StatusCodes []int `json:"status_codes"`
  82. Methods []string `json:"methods"`
  83. Paths []string `json:"paths"`
  84. UserAgents []string `json:"user_agents"`
  85. Referers []string `json:"referers"`
  86. Countries []string `json:"countries"`
  87. Browsers []string `json:"browsers"`
  88. OSs []string `json:"operating_systems"`
  89. Devices []string `json:"devices"`
  90. MinBytes *int64 `json:"min_bytes"`
  91. MaxBytes *int64 `json:"max_bytes"`
  92. MinReqTime *float64 `json:"min_request_time"`
  93. MaxReqTime *float64 `json:"max_request_time"`
  94. IncludeFacets bool `json:"include_facets"`
  95. IncludeStats bool `json:"include_stats"`
  96. FacetFields []string `json:"facet_fields"`
  97. FacetSize int `json:"facet_size"`
  98. UseCache bool `json:"use_cache"`
  99. }{
  100. Query: req.Query,
  101. Limit: req.Limit,
  102. Offset: req.Offset,
  103. SortBy: req.SortBy,
  104. SortOrder: req.SortOrder,
  105. StartTime: req.StartTime,
  106. EndTime: req.EndTime,
  107. UseMainLogPath: req.UseMainLogPath,
  108. LogPaths: copyStrings(req.LogPaths, true, true),
  109. Fields: copyStrings(req.Fields, true, false),
  110. IPAddresses: copyStrings(req.IPAddresses, true, true),
  111. StatusCodes: copyInts(req.StatusCodes, true),
  112. Methods: copyStrings(req.Methods, true, true),
  113. Paths: copyStrings(req.Paths, true, true),
  114. UserAgents: copyStrings(req.UserAgents, true, true),
  115. Referers: copyStrings(req.Referers, true, true),
  116. Countries: copyStrings(req.Countries, true, true),
  117. Browsers: copyStrings(req.Browsers, true, true),
  118. OSs: copyStrings(req.OSs, true, true),
  119. Devices: copyStrings(req.Devices, true, true),
  120. MinBytes: req.MinBytes,
  121. MaxBytes: req.MaxBytes,
  122. MinReqTime: req.MinReqTime,
  123. MaxReqTime: req.MaxReqTime,
  124. IncludeFacets: req.IncludeFacets,
  125. IncludeStats: req.IncludeStats,
  126. FacetFields: copyStrings(req.FacetFields, true, true),
  127. FacetSize: req.FacetSize,
  128. UseCache: req.UseCache,
  129. }
  130. // Convert to JSON and hash for consistent key generation
  131. jsonData, err := json.Marshal(keyData)
  132. if err != nil {
  133. // Fallback to efficient string building if JSON marshal fails
  134. keyBuf := make([]byte, 0, len(req.Query)+len(req.SortBy)+len(req.SortOrder)+32)
  135. keyBuf = append(keyBuf, "q:"...)
  136. keyBuf = append(keyBuf, req.Query...)
  137. keyBuf = append(keyBuf, "|l:"...)
  138. keyBuf = utils.AppendInt(keyBuf, req.Limit)
  139. keyBuf = append(keyBuf, "|o:"...)
  140. keyBuf = utils.AppendInt(keyBuf, req.Offset)
  141. keyBuf = append(keyBuf, "|s:"...)
  142. keyBuf = append(keyBuf, req.SortBy...)
  143. keyBuf = append(keyBuf, "|so:"...)
  144. keyBuf = append(keyBuf, req.SortOrder...)
  145. return utils.BytesToStringUnsafe(keyBuf)
  146. }
  147. // Use MD5 hash for compact key representation
  148. hash := md5.Sum(jsonData)
  149. return hex.EncodeToString(hash[:])
  150. }
  151. // Get retrieves a search result from cache
  152. func (osc *OptimizedSearchCache) Get(req *SearchRequest) *SearchResult {
  153. key := osc.GenerateOptimizedKey(req)
  154. if result, found := osc.cache.Get(key); found {
  155. // Mark result as from cache
  156. cachedResult := *result // Create a copy
  157. cachedResult.FromCache = true
  158. return &cachedResult
  159. }
  160. return nil
  161. }
  162. // Put stores a search result in cache with automatic cost calculation
  163. func (osc *OptimizedSearchCache) Put(req *SearchRequest, result *SearchResult, ttl time.Duration) {
  164. key := osc.GenerateOptimizedKey(req)
  165. // Calculate cost based on result size (number of hits + base cost)
  166. cost := int64(1 + len(result.Hits)/10) // Base cost of 1 plus hits/10
  167. if cost < 1 {
  168. cost = 1
  169. }
  170. // Set with TTL
  171. osc.cache.SetWithTTL(key, result, cost, ttl)
  172. // Wait for the value to pass through buffers to ensure it's cached
  173. osc.cache.Wait()
  174. }
  175. // Clear clears all cached entries
  176. func (osc *OptimizedSearchCache) Clear() {
  177. if osc != nil && osc.cache != nil {
  178. osc.cache.Clear()
  179. }
  180. }
  181. // GetStats returns cache statistics
  182. func (osc *OptimizedSearchCache) GetStats() *CacheStats {
  183. metrics := osc.cache.Metrics
  184. return &CacheStats{
  185. Size: int(metrics.KeysAdded() - metrics.KeysEvicted()),
  186. Capacity: int(osc.cache.MaxCost()),
  187. HitCount: int64(metrics.Hits()),
  188. MissCount: int64(metrics.Misses()),
  189. HitRate: metrics.Ratio(),
  190. Evictions: int64(metrics.KeysEvicted()),
  191. Additions: int64(metrics.KeysAdded()),
  192. Updates: int64(metrics.KeysUpdated()),
  193. Cost: int64(metrics.CostAdded() - metrics.CostEvicted()),
  194. }
  195. }
  196. // CacheStats provides detailed cache statistics
  197. type CacheStats struct {
  198. Size int `json:"size"` // Current number of items
  199. Capacity int `json:"capacity"` // Maximum capacity
  200. HitCount int64 `json:"hit_count"` // Number of cache hits
  201. MissCount int64 `json:"miss_count"` // Number of cache misses
  202. HitRate float64 `json:"hit_rate"` // Cache hit rate (0.0 to 1.0)
  203. Evictions int64 `json:"evictions"` // Number of evicted items
  204. Additions int64 `json:"additions"` // Number of items added
  205. Updates int64 `json:"updates"` // Number of items updated
  206. Cost int64 `json:"cost"` // Current cost
  207. }
  208. // WarmupCache pre-loads frequently used queries into cache
  209. func (osc *OptimizedSearchCache) WarmupCache(queries []WarmupQuery) {
  210. for _, query := range queries {
  211. // Pre-generate keys to warm up the cache
  212. key := osc.GenerateOptimizedKey(query.Request)
  213. if query.Result != nil {
  214. osc.cache.Set(key, query.Result, 1) // Use cost of 1 for warmup
  215. }
  216. }
  217. // Wait for cache operations to complete
  218. osc.cache.Wait()
  219. }
  220. // WarmupQuery represents a query and result pair for cache warmup
  221. type WarmupQuery struct {
  222. Request *SearchRequest `json:"request"`
  223. Result *SearchResult `json:"result"`
  224. }
  225. // Close closes the cache and frees resources
  226. func (osc *OptimizedSearchCache) Close() {
  227. osc.cache.Close()
  228. }
  229. // FastKeyGenerator provides even faster key generation for hot paths
  230. type FastKeyGenerator struct {
  231. buffer []byte
  232. }
  233. // NewFastKeyGenerator creates a key generator with pre-allocated buffer
  234. func NewFastKeyGenerator() *FastKeyGenerator {
  235. return &FastKeyGenerator{
  236. buffer: make([]byte, 0, 256), // Pre-allocate 256 bytes
  237. }
  238. }
  239. // GenerateKey generates a key using pre-allocated buffer
  240. func (fkg *FastKeyGenerator) GenerateKey(req *SearchRequest) string {
  241. fkg.buffer = fkg.buffer[:0] // Reset buffer
  242. // Build key using buffer to avoid allocations
  243. fkg.buffer = append(fkg.buffer, "q:"...)
  244. fkg.buffer = append(fkg.buffer, req.Query...)
  245. fkg.buffer = append(fkg.buffer, "|l:"...)
  246. fkg.buffer = strconv.AppendInt(fkg.buffer, int64(req.Limit), 10)
  247. fkg.buffer = append(fkg.buffer, "|o:"...)
  248. fkg.buffer = strconv.AppendInt(fkg.buffer, int64(req.Offset), 10)
  249. fkg.buffer = append(fkg.buffer, "|s:"...)
  250. fkg.buffer = append(fkg.buffer, req.SortBy...)
  251. fkg.buffer = append(fkg.buffer, "|so:"...)
  252. fkg.buffer = append(fkg.buffer, req.SortOrder...)
  253. // Add timestamps if present
  254. if req.StartTime != nil {
  255. fkg.buffer = append(fkg.buffer, "|st:"...)
  256. fkg.buffer = strconv.AppendInt(fkg.buffer, *req.StartTime, 10)
  257. }
  258. if req.EndTime != nil {
  259. fkg.buffer = append(fkg.buffer, "|et:"...)
  260. fkg.buffer = strconv.AppendInt(fkg.buffer, *req.EndTime, 10)
  261. }
  262. // Add arrays (simplified)
  263. if len(req.StatusCodes) > 0 {
  264. fkg.buffer = append(fkg.buffer, "|sc:"...)
  265. for i, code := range req.StatusCodes {
  266. if i > 0 {
  267. fkg.buffer = append(fkg.buffer, ',')
  268. }
  269. fkg.buffer = strconv.AppendInt(fkg.buffer, int64(code), 10)
  270. }
  271. }
  272. // Convert to string (this still allocates, but fewer allocations overall)
  273. return string(fkg.buffer)
  274. }
  275. // CacheMiddleware provides middleware functionality for caching
  276. type CacheMiddleware struct {
  277. cache *OptimizedSearchCache
  278. keyGen *FastKeyGenerator
  279. enabled bool
  280. defaultTTL time.Duration
  281. }
  282. // NewCacheMiddleware creates a new cache middleware
  283. func NewCacheMiddleware(cache *OptimizedSearchCache, defaultTTL time.Duration) *CacheMiddleware {
  284. return &CacheMiddleware{
  285. cache: cache,
  286. keyGen: NewFastKeyGenerator(),
  287. enabled: true,
  288. defaultTTL: defaultTTL,
  289. }
  290. }
  291. // Enable enables caching
  292. func (cm *CacheMiddleware) Enable() {
  293. cm.enabled = true
  294. }
  295. // Disable disables caching
  296. func (cm *CacheMiddleware) Disable() {
  297. cm.enabled = false
  298. }
  299. // IsEnabled returns whether caching is enabled
  300. func (cm *CacheMiddleware) IsEnabled() bool {
  301. return cm.enabled
  302. }
  303. // GetOrSet attempts to get from cache, or executes the provided function and caches the result
  304. func (cm *CacheMiddleware) GetOrSet(req *SearchRequest, fn func() (*SearchResult, error)) (*SearchResult, error) {
  305. if !cm.enabled {
  306. return fn()
  307. }
  308. // Try cache first
  309. if cached := cm.cache.Get(req); cached != nil {
  310. return cached, nil
  311. }
  312. // Execute function
  313. result, err := fn()
  314. if err != nil {
  315. return nil, err
  316. }
  317. // Cache successful result
  318. if result != nil {
  319. cm.cache.Put(req, result, cm.defaultTTL)
  320. }
  321. return result, nil
  322. }