From b333d47a7a2e727dee4fcffcffabce5eaeb953df Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Sun, 25 Apr 2021 14:54:58 -0600
Subject: [PATCH] Deprecate in-memory cache, preferring redis

---
 CHANGELOG.md                   |  9 +++++++++
 common/config/conf_main.go     |  5 +++++
 common/config/models_domain.go | 10 ----------
 common/config/models_main.go   | 10 ++++++++++
 internal_cache/instance.go     | 16 +++++++++++++++-
 5 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3e379dd1..fc53c1b5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 
 ## [Unreleased]
 
+### Deprecation notices
+
+In a future version (likely the next), the in-memory cache support will be removed. Instead, please use the Redis
+caching that is now supported properly by this release, or disable caching if not applicable for your deployment.
+
+### Changed
+
+* Support the Redis config at the root level of the config, promoting it to a proper feature.
+
 ## [1.2.7] - April 19th, 2021
 
 ### Security advisories
diff --git a/common/config/conf_main.go b/common/config/conf_main.go
index 3d255c56..e061590f 100644
--- a/common/config/conf_main.go
+++ b/common/config/conf_main.go
@@ -15,6 +15,7 @@ type MainRepoConfig struct {
 	Federation        FederationConfig      `yaml:"federation"`
 	Plugins           []PluginConfig        `yaml:"plugins,flow"`
 	Sentry            SentryConfig          `yaml:"sentry"`
+	Redis             RedisConfig           `yaml:"redis"`
 }
 
 func NewDefaultMainConfig() MainRepoConfig {
@@ -136,5 +137,9 @@ func NewDefaultMainConfig() MainRepoConfig {
 			Environment: "",
 			Debug:       false,
 		},
+		Redis: RedisConfig{
+			Enabled: false,
+			Shards:  []RedisShardConfig{},
+		},
 	}
 }
diff --git a/common/config/models_domain.go b/common/config/models_domain.go
index fbc49152..14ef0f30 100644
--- a/common/config/models_domain.go
+++ b/common/config/models_domain.go
@@ -111,16 +111,6 @@ type IPFSDaemonConfig struct {
 	RepoPath string `yaml:"repoPath"`
 }
 
-type RedisConfig struct {
-	Enabled bool               `yaml:"enabled"`
-	Shards  []RedisShardConfig `yaml:"shards,flow"`
-}
-
-type RedisShardConfig struct {
-	Name    string `yaml:"name"`
-	Address string `yaml:"addr"`
-}
-
 type AccessTokenConfig struct {
 	MaxCacheTimeSeconds int                `yaml:"maxCacheTimeSeconds"`
 	UseAppservices      bool               `yaml:"useLocalAppserviceConfig"`
diff --git a/common/config/models_main.go b/common/config/models_main.go
index a331754b..02bc8ab7 100644
--- a/common/config/models_main.go
+++ b/common/config/models_main.go
@@ -88,3 +88,13 @@ type SentryConfig struct {
 	Environment string `yaml:"environment"`
 	Debug       bool   `yaml:"debug"`
 }
+
+type RedisConfig struct {
+	Enabled bool               `yaml:"enabled"`
+	Shards  []RedisShardConfig `yaml:"shards,flow"`
+}
+
+type RedisShardConfig struct {
+	Name    string `yaml:"name"`
+	Address string `yaml:"addr"`
+}
\ No newline at end of file
diff --git a/internal_cache/instance.go b/internal_cache/instance.go
index dabf916f..22bcebb6 100644
--- a/internal_cache/instance.go
+++ b/internal_cache/instance.go
@@ -1,6 +1,7 @@
 package internal_cache
 
 import (
+	"github.com/getsentry/sentry-go"
 	"sync"
 
 	"github.com/sirupsen/logrus"
@@ -16,14 +17,27 @@ func Get() ContentCache {
 	}
 
 	lock.Do(func() {
-		if config.Get().Features.Redis.Enabled {
+		if config.Get().Redis.Enabled {
 			logrus.Info("Setting up Redis cache")
+			instance = NewRedisCache()
+		} else if config.Get().Features.Redis.Enabled {
+			logrus.Info("Setting up Redis cache")
+
+			warnMsg := "Your configuration uses a legacy approach for enabling Redis support. Please move this to the root config or visit #media-repo:t2bot.io for assistance."
+			logrus.Warn(warnMsg)
+			sentry.CaptureMessage(warnMsg)
+
 			instance = NewRedisCache()
 		} else if !config.Get().Downloads.Cache.Enabled {
 			logrus.Warn("Cache is disabled - setting up a dummy instance")
 			instance = NewNoopCache()
 		} else {
 			logrus.Info("Setting up in-memory cache")
+
+			warnMsg := "The built-in cache mechanism is being removed in a future version. Please set up Redis as a cache mechanism."
+			logrus.Warn(warnMsg)
+			sentry.CaptureMessage(warnMsg)
+
 			instance = NewMemoryCache()
 		}
 	})
-- 
GitLab