Add distibuted cache with Redis

This commit is contained in:
2024-06-11 11:35:50 +01:00
parent a01a4da9b5
commit 12e0294945
15 changed files with 407 additions and 89 deletions
+13 -3
View File
@@ -54,16 +54,20 @@ func checkIfUserIsBanned(c *fiber.Ctx, userID string) bool {
func apiClearCache(c *fiber.Ctx) error {
cfg.Logger.Debug("Clearing cache via API", nil)
cfg.Cache.CacheClient.ClearCache()
cacheClear()
cfg.Logger.Info("Cache cleared via API", nil)
c.Status(200).SendString("OK: cache cleared")
return nil
}
func apiCacheStats(c *fiber.Ctx) error {
stats := cfg.Cache.CacheClient.ShowStats()
stats := getCacheStats()
cfg.Logger.Debug("Getting cache stats via API", map[string]interface{}{"stats": stats})
c.JSON(stats)
err := c.JSON(stats)
if err != nil {
cfg.Logger.Error("Can't marshal cache stats", map[string]interface{}{"error": err.Error()})
return err
}
return nil
}
@@ -128,6 +132,12 @@ func loadBannedUsers() {
cfg.Logger.Error("Can't create the file", map[string]interface{}{"error": err.Error()})
return
}
// write empty json to the file
err = os.WriteFile(cfg.Api.BannedUsersFile, []byte("{}"), 0644)
if err != nil {
cfg.Logger.Error("Can't write to the file", map[string]interface{}{"error": err.Error()})
return
}
}
fileLock := flock.New(fmt.Sprintf("%s.lock", cfg.Api.BannedUsersFile))