cache.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. // Cache provides high-performance caching using Ristretto
  14. type Cache struct {
  15. cache *ristretto.Cache[string, *SearchResult]
  16. }
  17. // NewCache creates a new cache with Ristretto
  18. func NewCache(maxSize int64) *Cache {
  19. cache, err := ristretto.NewCache(&ristretto.Config[string, *SearchResult]{
  20. NumCounters: maxSize * 10,
  21. MaxCost: maxSize,
  22. BufferItems: 64,
  23. Metrics: true,
  24. })
  25. if err != nil {
  26. panic(fmt.Sprintf("failed to create cache: %v", err))
  27. }
  28. return &Cache{cache: cache}
  29. }
  30. // CacheKeyData represents the normalized data used for cache key generation
  31. type CacheKeyData struct {
  32. Query string `json:"query"`
  33. Limit int `json:"limit"`
  34. Offset int `json:"offset"`
  35. SortBy string `json:"sort_by"`
  36. SortOrder string `json:"sort_order"`
  37. StartTime *int64 `json:"start_time"`
  38. EndTime *int64 `json:"end_time"`
  39. UseMainLogPath bool `json:"use_main_log_path"`
  40. LogPaths []string `json:"log_paths"`
  41. Fields []string `json:"fields"`
  42. IPAddresses []string `json:"ip_addresses"`
  43. StatusCodes []int `json:"status_codes"`
  44. Methods []string `json:"methods"`
  45. Paths []string `json:"paths"`
  46. UserAgents []string `json:"user_agents"`
  47. Referers []string `json:"referers"`
  48. Countries []string `json:"countries"`
  49. Browsers []string `json:"browsers"`
  50. OSs []string `json:"operating_systems"`
  51. Devices []string `json:"devices"`
  52. MinBytes *int64 `json:"min_bytes"`
  53. MaxBytes *int64 `json:"max_bytes"`
  54. MinReqTime *float64 `json:"min_request_time"`
  55. MaxReqTime *float64 `json:"max_request_time"`
  56. IncludeFacets bool `json:"include_facets"`
  57. IncludeStats bool `json:"include_stats"`
  58. FacetFields []string `json:"facet_fields"`
  59. FacetSize int `json:"facet_size"`
  60. UseCache bool `json:"use_cache"`
  61. }
  62. // sortedUniqueStrings returns a sorted, deduplicated copy of a string slice
  63. func sortedUniqueStrings(src []string) []string {
  64. if len(src) == 0 {
  65. return nil
  66. }
  67. seen := make(map[string]struct{}, len(src))
  68. res := make([]string, 0, len(src))
  69. for _, s := range src {
  70. if _, exists := seen[s]; !exists {
  71. seen[s] = struct{}{}
  72. res = append(res, s)
  73. }
  74. }
  75. sort.Strings(res)
  76. return res
  77. }
  78. // sortedStrings returns a sorted copy of a string slice
  79. func sortedStrings(src []string) []string {
  80. if len(src) == 0 {
  81. return nil
  82. }
  83. res := make([]string, len(src))
  84. copy(res, src)
  85. sort.Strings(res)
  86. return res
  87. }
  88. // sortedInts returns a sorted copy of an int slice
  89. func sortedInts(src []int) []int {
  90. if len(src) == 0 {
  91. return nil
  92. }
  93. res := make([]int, len(src))
  94. copy(res, src)
  95. sort.Ints(res)
  96. return res
  97. }
  98. // GenerateKey generates an efficient cache key for a search request
  99. func (c *Cache) GenerateKey(req *SearchRequest) string {
  100. keyData := CacheKeyData{
  101. Query: req.Query,
  102. Limit: req.Limit,
  103. Offset: req.Offset,
  104. SortBy: req.SortBy,
  105. SortOrder: req.SortOrder,
  106. StartTime: req.StartTime,
  107. EndTime: req.EndTime,
  108. UseMainLogPath: req.UseMainLogPath,
  109. LogPaths: sortedUniqueStrings(req.LogPaths),
  110. Fields: sortedStrings(req.Fields),
  111. IPAddresses: sortedUniqueStrings(req.IPAddresses),
  112. StatusCodes: sortedInts(req.StatusCodes),
  113. Methods: sortedUniqueStrings(req.Methods),
  114. Paths: sortedUniqueStrings(req.Paths),
  115. UserAgents: sortedUniqueStrings(req.UserAgents),
  116. Referers: sortedUniqueStrings(req.Referers),
  117. Countries: sortedUniqueStrings(req.Countries),
  118. Browsers: sortedUniqueStrings(req.Browsers),
  119. OSs: sortedUniqueStrings(req.OSs),
  120. Devices: sortedUniqueStrings(req.Devices),
  121. MinBytes: req.MinBytes,
  122. MaxBytes: req.MaxBytes,
  123. MinReqTime: req.MinReqTime,
  124. MaxReqTime: req.MaxReqTime,
  125. IncludeFacets: req.IncludeFacets,
  126. IncludeStats: req.IncludeStats,
  127. FacetFields: sortedUniqueStrings(req.FacetFields),
  128. FacetSize: req.FacetSize,
  129. UseCache: req.UseCache,
  130. }
  131. jsonData, err := json.Marshal(keyData)
  132. if err != nil {
  133. return c.generateFallbackKey(req)
  134. }
  135. hash := md5.Sum(jsonData)
  136. return hex.EncodeToString(hash[:])
  137. }
  138. // generateFallbackKey creates a basic cache key when JSON marshaling fails
  139. func (c *Cache) generateFallbackKey(req *SearchRequest) string {
  140. keyBuf := make([]byte, 0, len(req.Query)+len(req.SortBy)+len(req.SortOrder)+32)
  141. keyBuf = append(keyBuf, "q:"...)
  142. keyBuf = append(keyBuf, req.Query...)
  143. keyBuf = append(keyBuf, "|l:"...)
  144. keyBuf = utils.AppendInt(keyBuf, req.Limit)
  145. keyBuf = append(keyBuf, "|o:"...)
  146. keyBuf = utils.AppendInt(keyBuf, req.Offset)
  147. keyBuf = append(keyBuf, "|s:"...)
  148. keyBuf = append(keyBuf, req.SortBy...)
  149. keyBuf = append(keyBuf, "|so:"...)
  150. keyBuf = append(keyBuf, req.SortOrder...)
  151. return utils.BytesToStringUnsafe(keyBuf)
  152. }
  153. // Get retrieves a search result from cache
  154. func (c *Cache) Get(req *SearchRequest) *SearchResult {
  155. key := c.GenerateKey(req)
  156. result, found := c.cache.Get(key)
  157. if !found {
  158. return nil
  159. }
  160. cachedResult := *result
  161. cachedResult.FromCache = true
  162. return &cachedResult
  163. }
  164. // Put stores a search result in cache with automatic cost calculation
  165. func (c *Cache) Put(req *SearchRequest, result *SearchResult, ttl time.Duration) {
  166. key := c.GenerateKey(req)
  167. cost := int64(1 + len(result.Hits)/10)
  168. if cost < 1 {
  169. cost = 1
  170. }
  171. c.cache.SetWithTTL(key, result, cost, ttl)
  172. c.cache.Wait()
  173. }
  174. // Clear clears all cached entries
  175. func (c *Cache) Clear() {
  176. if c != nil && c.cache != nil {
  177. c.cache.Clear()
  178. }
  179. }
  180. // GetStats returns cache statistics
  181. func (c *Cache) GetStats() *CacheStats {
  182. metrics := c.cache.Metrics
  183. return &CacheStats{
  184. Size: int(metrics.KeysAdded() - metrics.KeysEvicted()),
  185. Capacity: int(c.cache.MaxCost()),
  186. HitCount: int64(metrics.Hits()),
  187. MissCount: int64(metrics.Misses()),
  188. HitRate: metrics.Ratio(),
  189. Evictions: int64(metrics.KeysEvicted()),
  190. Additions: int64(metrics.KeysAdded()),
  191. Updates: int64(metrics.KeysUpdated()),
  192. Cost: int64(metrics.CostAdded() - metrics.CostEvicted()),
  193. }
  194. }
  195. // CacheStats provides detailed cache statistics
  196. type CacheStats struct {
  197. Size int `json:"size"`
  198. Capacity int `json:"capacity"`
  199. HitCount int64 `json:"hit_count"`
  200. MissCount int64 `json:"miss_count"`
  201. HitRate float64 `json:"hit_rate"`
  202. Evictions int64 `json:"evictions"`
  203. Additions int64 `json:"additions"`
  204. Updates int64 `json:"updates"`
  205. Cost int64 `json:"cost"`
  206. }
  207. // Warmup pre-loads frequently used queries into cache
  208. func (c *Cache) Warmup(queries []WarmupQuery) {
  209. for _, query := range queries {
  210. if query.Result != nil {
  211. key := c.GenerateKey(query.Request)
  212. c.cache.Set(key, query.Result, 1)
  213. }
  214. }
  215. c.cache.Wait()
  216. }
  217. // WarmupQuery represents a query and result pair for cache warmup
  218. type WarmupQuery struct {
  219. Request *SearchRequest `json:"request"`
  220. Result *SearchResult `json:"result"`
  221. }
  222. // Close closes the cache and frees resources
  223. func (c *Cache) Close() {
  224. c.cache.Close()
  225. }
  226. // KeyGen provides even faster key generation for hot paths
  227. type KeyGen struct {
  228. buffer []byte
  229. }
  230. // NewKeyGen creates a key generator with pre-allocated buffer
  231. func NewKeyGen() *KeyGen {
  232. return &KeyGen{
  233. buffer: make([]byte, 0, 256),
  234. }
  235. }
  236. // GenerateKey generates a key using pre-allocated buffer
  237. func (kg *KeyGen) GenerateKey(req *SearchRequest) string {
  238. kg.buffer = kg.buffer[:0]
  239. kg.buffer = append(kg.buffer, "q:"...)
  240. kg.buffer = append(kg.buffer, req.Query...)
  241. kg.buffer = append(kg.buffer, "|l:"...)
  242. kg.buffer = strconv.AppendInt(kg.buffer, int64(req.Limit), 10)
  243. kg.buffer = append(kg.buffer, "|o:"...)
  244. kg.buffer = strconv.AppendInt(kg.buffer, int64(req.Offset), 10)
  245. kg.buffer = append(kg.buffer, "|s:"...)
  246. kg.buffer = append(kg.buffer, req.SortBy...)
  247. kg.buffer = append(kg.buffer, "|so:"...)
  248. kg.buffer = append(kg.buffer, req.SortOrder...)
  249. if req.StartTime != nil {
  250. kg.buffer = append(kg.buffer, "|st:"...)
  251. kg.buffer = strconv.AppendInt(kg.buffer, *req.StartTime, 10)
  252. }
  253. if req.EndTime != nil {
  254. kg.buffer = append(kg.buffer, "|et:"...)
  255. kg.buffer = strconv.AppendInt(kg.buffer, *req.EndTime, 10)
  256. }
  257. if len(req.StatusCodes) > 0 {
  258. kg.buffer = append(kg.buffer, "|sc:"...)
  259. for i, code := range req.StatusCodes {
  260. if i > 0 {
  261. kg.buffer = append(kg.buffer, ',')
  262. }
  263. kg.buffer = strconv.AppendInt(kg.buffer, int64(code), 10)
  264. }
  265. }
  266. return string(kg.buffer)
  267. }
  268. // Middleware provides middleware functionality for caching
  269. type Middleware struct {
  270. cache *Cache
  271. keyGen *KeyGen
  272. enabled bool
  273. defaultTTL time.Duration
  274. }
  275. // NewMiddleware creates a new cache middleware
  276. func NewMiddleware(cache *Cache, defaultTTL time.Duration) *Middleware {
  277. return &Middleware{
  278. cache: cache,
  279. keyGen: NewKeyGen(),
  280. enabled: true,
  281. defaultTTL: defaultTTL,
  282. }
  283. }
  284. // Enable enables caching
  285. func (m *Middleware) Enable() {
  286. m.enabled = true
  287. }
  288. // Disable disables caching
  289. func (m *Middleware) Disable() {
  290. m.enabled = false
  291. }
  292. // IsEnabled returns whether caching is enabled
  293. func (m *Middleware) IsEnabled() bool {
  294. return m.enabled
  295. }
  296. // GetOrSet attempts to get from cache, or executes the provided function and caches the result
  297. func (m *Middleware) GetOrSet(req *SearchRequest, fn func() (*SearchResult, error)) (*SearchResult, error) {
  298. if !m.enabled {
  299. return fn()
  300. }
  301. if cached := m.cache.Get(req); cached != nil {
  302. return cached, nil
  303. }
  304. result, err := fn()
  305. if err != nil {
  306. return nil, err
  307. }
  308. if result != nil {
  309. m.cache.Put(req, result, m.defaultTTL)
  310. }
  311. return result, nil
  312. }