@@ -33,34 +33,53 @@ import (
33
33
" github.com/arturmon/multi-tier-caching/storage"
34
34
)
35
35
36
- func main () {
37
36
37
+ func main () {
38
+ ctx , cancel := context.WithCancel (context.Background ())
39
+ defer cancel ()
38
40
cfg := config.LoadConfig ()
39
41
42
+ var debugCache = false
43
+
40
44
logger.InitLogger (cfg.LogLevel )
41
45
logger.Info (" Launching the cache system" , " memoryCacheSize" , cfg.MemoryCacheSize )
42
46
43
- dbStorage , err := storage.NewDatabaseStorage (cfg.DatabaseDSN )
47
+ memoryStorage , err := storage.NewRistrettoCache (ctx, int64 (cfg.MemoryCacheSize ))
48
+ if err != nil {
49
+ logger.Error (" Failed to create Memory stoage: %v " , err)
50
+ }
51
+
52
+ dbStorage , err := storage.NewDatabaseStorage (cfg.DatabaseDSN , debugCache)
44
53
if err != nil {
45
54
logger.Error (" Failed to connect to the database" , " error" , err)
46
55
return
47
56
}
48
57
defer dbStorage.Close ()
49
58
50
- redisStorage , err := storage.NewRedisStorage (cfg.RedisAddr , cfg.RedisPassword )
59
+ redisStorage , err := storage.NewRedisStorage (cfg.RedisAddr , cfg.RedisPassword , 1 )
51
60
if err != nil {
52
61
logger.Error (" Failed to connect to Redis" , " error" , err)
53
62
return
54
63
}
55
64
56
- cache := multi_tier_caching.NewMultiTierCache (
57
- []multi_tier_caching.CacheLayer {
58
- multi_tier_caching.NewMemoryCache (),
59
- multi_tier_caching.NewRedisCache (redisStorage),
65
+ logger.Info (" Launching the cache system" , " memoryCacheSize" , cfg.MemoryCacheSize )
66
+ logger.Info (" Launching the cache system" , " databaseDSN" , cfg.DatabaseDSN )
67
+ logger.Info (" Launching the cache system" , " redisAddr" , cfg.RedisAddr )
68
+
69
+ cacheConfig := multi_tier_caching.MultiTierCacheConfig {
70
+ Layers: []multi_tier_caching.LayerInfo {
71
+ multi_tier_caching.NewLayerInfo (multi_tier_caching.NewMemoryCache (memoryStorage)),
72
+ multi_tier_caching.NewLayerInfo (multi_tier_caching.NewRedisCache (redisStorage)),
60
73
},
61
- multi_tier_caching.NewDatabaseCache (dbStorage),
62
- )
74
+ DB: multi_tier_caching.NewDatabaseCache (dbStorage),
75
+ Thresholds: []int {10 , 5 },
76
+ BloomSize: 100000 ,
77
+ BloomHashes: 5 ,
78
+ Debug: debugCache,
79
+ }
63
80
81
+ cache := multi_tier_caching.NewMultiTierCache (ctx, cacheConfig)
82
+
64
83
err = cache.Set (context.Background (), " key1" , " value1" )
65
84
if err != nil {
66
85
return
@@ -90,11 +109,57 @@ fmt.Println("Cached Value:", value)
90
109
```
91
110
92
111
## Features
93
- - ** Multi-tier caching** : Uses memory, Redis, and database storage layers.
94
- - ** Bloom filter** : Reduces unnecessary database queries.
95
- - ** Write-behind queue** : Efficient database updates.
96
- - ** TTL Management** : Supports automatic expiration of cached items.
97
- - ** Cache Analytics** : Provides cache hit/miss statistics.
112
+
113
+ - ** Multi-tier caching architecture** :
114
+ - Configurable cache layers (e.g., hot, warm, cold) for optimized data access.
115
+ - Prioritizes checks from the hottest layer (in-memory) to colder layers (e.g., disk, remote).
116
+
117
+ - ** Bloom filter optimization** :
118
+ - Reduces unnecessary database queries by probabilistically checking key existence.
119
+ - Dynamically resizes based on request frequency, miss rate, and load factors.
120
+
121
+ - ** Adaptive TTL management** :
122
+ - Adjusts time-to-live (TTL) dynamically using key request frequency.
123
+ - Longer TTL for high-frequency keys to minimize cache churn.
124
+
125
+ - ** Intelligent data migration** :
126
+ - Automatically promotes/demotes keys between layers using frequency thresholds.
127
+ - Processes migrations asynchronously with adjustable intervals via background workers.
128
+
129
+ - ** Metrics and analytics** :
130
+ - Tracks cache hits, misses, migration times, and key frequency.
131
+ - Exposes Prometheus metrics (` cacheHits ` , ` migrationTime ` , ` keyFrequency ` ).
132
+
133
+ - ** Write-behind queue** :
134
+ - Batches and asynchronously persists updates to reduce database latency.
135
+ - Adaptive processing intervals based on queue load.
136
+
137
+ - ** Self-optimizing components** :
138
+ - ** Bloom filter auto-scaling** : Dynamically adjusts size and hash functions.
139
+ - ** TTL auto-tuning** : Balances cache efficiency and storage costs.
140
+
141
+ - ** Health monitoring** :
142
+ - Health checks for all cache layers and the underlying database.
143
+ - Graceful shutdown with resource cleanup.
144
+
145
+ - ** Debugging and observability** :
146
+ - Detailed logs for migrations, TTL adjustments, and cache operations.
147
+ - ` GetDebugInfo() ` method for real-time Bloom filter stats (false positive rate, load factor).
148
+
149
+ - ** Configurable policies** :
150
+ - Customizable frequency thresholds for layer transitions.
151
+ - Adjustable Bloom filter parameters (initial size, hash functions).
152
+
153
+ - ** Database integration** :
154
+ - Fallback to database on cache misses, with automatic cache refresh for fetched keys.
155
+
156
+ - ** Concurrency and scalability** :
157
+ - Thread-safe operations using mutexes and channels.
158
+ - Parallel migration workers for high-throughput scenarios.
159
+
160
+ - ** Prometheus integration** :
161
+ - Built-in support for Prometheus metrics (histograms, counters, gauges).
162
+
98
163
99
164
## Contributing
100
165
Pull requests are welcome. Please open an issue first to discuss any major changes.
0 commit comments