log_cache.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package nginx_log
  2. import (
  3. "sync"
  4. )
  5. // NginxLogCache represents a cached log entry from nginx configuration
  6. type NginxLogCache struct {
  7. Path string `json:"path"` // Path to the log file
  8. Type string `json:"type"` // Type of log: "access" or "error"
  9. Name string `json:"name"` // Name of the log file
  10. }
  11. var (
  12. // logCache is the map to store all found log files
  13. logCache = make(map[string]*NginxLogCache)
  14. cacheMutex sync.RWMutex
  15. )
  16. // AddLogPath adds a log path to the log cache
  17. func AddLogPath(path, logType, name string) {
  18. cacheMutex.Lock()
  19. defer cacheMutex.Unlock()
  20. logCache[path] = &NginxLogCache{
  21. Path: path,
  22. Type: logType,
  23. Name: name,
  24. }
  25. }
  26. // GetAllLogPaths returns all cached log paths
  27. func GetAllLogPaths(filters ...func(*NginxLogCache) bool) []*NginxLogCache {
  28. cacheMutex.RLock()
  29. defer cacheMutex.RUnlock()
  30. result := make([]*NginxLogCache, 0, len(logCache))
  31. for _, cache := range logCache {
  32. flag := true
  33. if len(filters) > 0 {
  34. for _, filter := range filters {
  35. if !filter(cache) {
  36. flag = false
  37. break
  38. }
  39. }
  40. }
  41. if flag {
  42. result = append(result, cache)
  43. }
  44. }
  45. return result
  46. }
  47. // ClearLogCache clears all entries in the log cache
  48. func ClearLogCache() {
  49. cacheMutex.Lock()
  50. defer cacheMutex.Unlock()
  51. // Clear the cache
  52. logCache = make(map[string]*NginxLogCache)
  53. }