1
0

cache.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package cache
  2. import (
  3. "context"
  4. "time"
  5. "github.com/dgraph-io/ristretto/v2"
  6. "github.com/uozi-tech/cosy/logger"
  7. )
  8. // Global cache instance
  9. var cache *ristretto.Cache[string, any]
  10. // InitInMemoryCache initializes just the in-memory cache system (Ristretto).
  11. // This is suitable for unit tests that don't require search functionality.
  12. func InitInMemoryCache() {
  13. if cache != nil {
  14. cache.Close()
  15. }
  16. var err error
  17. cache, err = ristretto.NewCache(&ristretto.Config[string, any]{
  18. NumCounters: 1e7, // Track frequency of 10M keys
  19. MaxCost: 1 << 30, // Maximum cache size: 1GB
  20. BufferItems: 64, // Keys per Get buffer
  21. })
  22. if err != nil {
  23. logger.Fatal("Failed to initialize in-memory cache:", err)
  24. }
  25. logger.Info("In-memory cache initialized successfully")
  26. }
  27. // Init initializes the full cache system including search indexing and config scanning.
  28. // This should be used by the main application and integration tests.
  29. func Init(ctx context.Context) {
  30. // Force release any existing file system resources before initialization
  31. logger.Info("Initializing full cache system - ensuring clean state...")
  32. ForceReleaseResources()
  33. // Initialize the main in-memory cache
  34. InitInMemoryCache()
  35. // Initialize search index
  36. if err := InitSearchIndex(ctx); err != nil {
  37. logger.Error("Failed to initialize search index:", err)
  38. }
  39. // Initialize config file scanner
  40. logger.Info("Starting config scanner initialization...")
  41. InitScanner(ctx)
  42. logger.Info("Cache system initialization completed")
  43. go func() {
  44. <-ctx.Done()
  45. Shutdown()
  46. }()
  47. }
  48. // Set stores a value in cache with TTL
  49. func Set(key string, value interface{}, ttl time.Duration) {
  50. if cache == nil {
  51. logger.Warn("Cache not initialized, skipping Set operation.")
  52. return
  53. }
  54. cache.SetWithTTL(key, value, 0, ttl)
  55. cache.Wait()
  56. }
  57. // Get retrieves a value from cache
  58. func Get(key string) (interface{}, bool) {
  59. if cache == nil {
  60. logger.Warn("Cache not initialized, returning not found.")
  61. return nil, false
  62. }
  63. return cache.Get(key)
  64. }
  65. // Del removes a value from cache
  66. func Del(key string) {
  67. if cache == nil {
  68. logger.Warn("Cache not initialized, skipping Del operation.")
  69. return
  70. }
  71. cache.Del(key)
  72. }
  73. // Shutdown gracefully shuts down the cache system and releases all resources
  74. func Shutdown() {
  75. logger.Info("Shutting down cache system...")
  76. // Force release all file system resources
  77. ForceReleaseResources()
  78. // Close main cache
  79. if cache != nil {
  80. cache.Close()
  81. cache = nil
  82. logger.Info("Main cache closed")
  83. }
  84. logger.Info("Cache system shutdown completed")
  85. }