@@ -17,6 +17,7 @@ import (
17
17
18
18
dotenv "github.com/joho/godotenv"
19
19
dat "github.com/nerdynz/dat"
20
+ "github.com/nerdynz/dat/kvs"
20
21
runner "github.com/nerdynz/dat/sqlx-runner"
21
22
redis "gopkg.in/redis.v5"
22
23
)
@@ -32,8 +33,8 @@ type Datastore struct {
32
33
// New - returns a new datastore which contains redis, database, view globals and settings.
33
34
func New () * Datastore {
34
35
store := Simple ()
35
- store .DB = getDBConnection (store .Settings )
36
36
store .Cache = getCacheConnection (store .Settings )
37
+ store .DB = getDBConnection (store .Settings )
37
38
return store
38
39
}
39
40
@@ -99,11 +100,22 @@ func getDBConnection(settings *Settings) *runner.DB {
99
100
err = db .Ping ()
100
101
if err != nil {
101
102
log .Error (err )
103
+ panic (err )
102
104
}
103
105
log .Info ("database running" )
104
106
// ensures the database can be pinged with an exponential backoff (15 min)
105
107
runner .MustPing (db )
106
108
109
+ if settings .CacheNamespace != "" {
110
+ store , err := kvs .NewRedisStore (settings .CacheNamespace , ":6379" , "" )
111
+ if err != nil {
112
+ log .Error (err )
113
+ panic (err )
114
+ }
115
+ log .Info ("USING CACHE" , settings .CacheNamespace )
116
+ runner .SetCache (store )
117
+ }
118
+
107
119
// set to reasonable values for production
108
120
db .SetMaxIdleConns (4 )
109
121
db .SetMaxOpenConns (16 )
@@ -186,6 +198,7 @@ type Settings struct {
186
198
EmailFromName string
187
199
EmailFromEmail string
188
200
IsSiteBound bool
201
+ CacheNamespace string
189
202
}
190
203
191
204
func loadSettings () * Settings {
@@ -205,6 +218,7 @@ func loadSettings() *Settings {
205
218
s .DSN = os .Getenv ("DATABASE_URL" )
206
219
s .Sitename = os .Getenv ("SITE_NAME" )
207
220
s .EncKey = os .Getenv ("SECURITY_ENCRYPTION_KEY" )
221
+ s .CacheNamespace = os .Getenv ("CACHE_NAMESPACE" )
208
222
209
223
s .EmailFromName = os .Getenv ("EMAIL_FROM_NAME" )
210
224
if s .EmailFromName == "" {
0 commit comments