Skip to content

Commit 833e8be

Browse files
committed
Caching for DAT
1 parent 549f4ce commit 833e8be

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

datastore.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
dotenv "github.com/joho/godotenv"
1919
dat "github.com/nerdynz/dat"
20+
"github.com/nerdynz/dat/kvs"
2021
runner "github.com/nerdynz/dat/sqlx-runner"
2122
redis "gopkg.in/redis.v5"
2223
)
@@ -32,8 +33,8 @@ type Datastore struct {
3233
// New - returns a new datastore which contains redis, database, view globals and settings.
3334
func New() *Datastore {
3435
store := Simple()
35-
store.DB = getDBConnection(store.Settings)
3636
store.Cache = getCacheConnection(store.Settings)
37+
store.DB = getDBConnection(store.Settings)
3738
return store
3839
}
3940

@@ -99,11 +100,22 @@ func getDBConnection(settings *Settings) *runner.DB {
99100
err = db.Ping()
100101
if err != nil {
101102
log.Error(err)
103+
panic(err)
102104
}
103105
log.Info("database running")
104106
// ensures the database can be pinged with an exponential backoff (15 min)
105107
runner.MustPing(db)
106108

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+
107119
// set to reasonable values for production
108120
db.SetMaxIdleConns(4)
109121
db.SetMaxOpenConns(16)
@@ -186,6 +198,7 @@ type Settings struct {
186198
EmailFromName string
187199
EmailFromEmail string
188200
IsSiteBound bool
201+
CacheNamespace string
189202
}
190203

191204
func loadSettings() *Settings {
@@ -205,6 +218,7 @@ func loadSettings() *Settings {
205218
s.DSN = os.Getenv("DATABASE_URL")
206219
s.Sitename = os.Getenv("SITE_NAME")
207220
s.EncKey = os.Getenv("SECURITY_ENCRYPTION_KEY")
221+
s.CacheNamespace = os.Getenv("CACHE_NAMESPACE")
208222

209223
s.EmailFromName = os.Getenv("EMAIL_FROM_NAME")
210224
if s.EmailFromName == "" {

0 commit comments

Comments
 (0)