From 416745ddabf49e0849ef3f27748e97bdd20e04c6 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 13 Sep 2018 19:53:03 +0200 Subject: [PATCH] added options to configure cache --- Featurecreep.md | 2 +- Gopkg.lock | 20 ++++++++++++++++++++ config.yml.sample | 14 +++++++++++++- docs/config.md | 12 ++++++++++++ models/config.go | 6 ++++++ models/models.go | 20 ++++++++++++++++++-- 6 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Featurecreep.md b/Featurecreep.md index aa52818f..c4c394d9 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -206,7 +206,7 @@ Teams sind global, d.h. Ein Team kann mehrere Namespaces verwalten. ### Short Term -* [ ] Cacher konfigurierbar +* [x] Cacher konfigurierbar * [ ] Validation der ankommenden structs, am besten mit https://github.com/go-validator/validator * [ ] Wenn die ID bei irgendeiner GetByID... Methode < 1 ist soll ein error not exist geworfen werden diff --git a/Gopkg.lock b/Gopkg.lock index 11692cbc..4e2c43dd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -24,6 +24,17 @@ revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9" version = "v1.4.7" +[[projects]] + digest = "1:0594af97b2f4cec6554086eeace6597e20a4b69466eb4ada25adf9f4300dddd2" + name = "github.com/garyburd/redigo" + packages = [ + "internal", + "redis", + ] + pruneopts = "UT" + revision = "a69d19351219b6dd56f274f96d85a7014a2ec34e" + version = "v1.6.0" + [[projects]] digest = "1:d2b67246c3fa959edfa1be84b407cf0a0a4cabbace0843814b1729b5af808d9e" name = "github.com/go-sql-driver/mysql" @@ -53,6 +64,14 @@ pruneopts = "UT" revision = "29d4a0330a00b9be468b70e3fb0f74109348c358" +[[projects]] + branch = "master" + digest = "1:d4a957ebe4ccebc299c1f6b6a78a0713af379721980cf14990b8ab8fa93cd92d" + name = "github.com/go-xorm/xorm-redis-cache" + packages = ["."] + pruneopts = "UT" + revision = "859b313566b2ef090319245fd4fe7692f25dbd79" + [[projects]] digest = "1:c0d19ab64b32ce9fe5cf4ddceba78d5bc9807f0016db6b1183599da3dcc24d10" name = "github.com/hashicorp/hcl" @@ -282,6 +301,7 @@ "github.com/go-sql-driver/mysql", "github.com/go-xorm/core", "github.com/go-xorm/xorm", + "github.com/go-xorm/xorm-redis-cache", "github.com/imdario/mergo", "github.com/labstack/echo", "github.com/labstack/echo/middleware", diff --git a/config.yml.sample b/config.yml.sample index ce6c1e9d..0f93e621 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -20,4 +20,16 @@ database: # When using sqlite, this is the path where to store the data Path: "./vikunja.db" # Whether to show mysql queries or not. Useful for debugging. - showqueries: "false" \ No newline at end of file + showqueries: "false" + +cache: + # If cache is enabled or not + enabled: false + # Cache type. Possible values are memory or redis + type: memory + # When using memory this defines the maximum size an element can take + maxelementsize: 1000 + # When using redis, this is the host of the redis server including its port. + redishost: 'localhost:6379' + # When using redis, this is the password used to authenicate against the redis server + redispassword: '' diff --git a/docs/config.md b/docs/config.md index 046878a5..8767bd8d 100644 --- a/docs/config.md +++ b/docs/config.md @@ -45,4 +45,16 @@ database: Path: "./vikunja.db" # Whether to show mysql queries or not. Useful for debugging. showqueries: "false" + +cache: + # If cache is enabled or not + enabled: false + # Cache type. Possible values are memory or redis + type: memory + # When using memory this defines the maximum size an element can take + maxelementsize: 1000 + # When using redis, this is the host of the redis server including its port. + redishost: 'localhost:6379' + # When using redis, this is the password used to authenicate against the redis server + redispassword: '' ``` diff --git a/models/config.go b/models/config.go index a6b0ba11..05df346a 100644 --- a/models/config.go +++ b/models/config.go @@ -27,6 +27,12 @@ func InitConfig() (err error) { viper.SetDefault("database.database", "vikunja") viper.SetDefault("database.path", "./vikunja.db") viper.SetDefault("database.showqueries", false) + // Cacher + viper.SetDefault("cache.enabled", false) + viper.SetDefault("cache.type", "memory") + viper.SetDefault("cache.maxelementsize", 1000) + viper.SetDefault("cache.redishost", "localhost:6379") + viper.SetDefault("cache.redispassword", "") // Init checking for environment variables viper.SetEnvPrefix("vikunja") diff --git a/models/models.go b/models/models.go index 1ae238ad..f0497995 100644 --- a/models/models.go +++ b/models/models.go @@ -6,7 +6,10 @@ import ( "github.com/go-xorm/core" "github.com/go-xorm/xorm" _ "github.com/mattn/go-sqlite3" // Because. + xrc "github.com/go-xorm/xorm-redis-cache" + "github.com/spf13/viper" + "encoding/gob" ) var ( @@ -58,8 +61,21 @@ func SetEngine() (err error) { } // Cache - //cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) - //x.SetDefaultCacher(cacher) + if viper.GetBool("cache.enabled") { + switch viper.GetString("cache.type") { + case "memory": + cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), viper.GetInt("cache.maxelementsize")) + x.SetDefaultCacher(cacher) + break + case "redis": + cacher := xrc.NewRedisCacher(viper.GetString("cache.redishost"), viper.GetString("cache.redispassword"), xrc.DEFAULT_EXPIRATION, x.Logger()) + x.SetDefaultCacher(cacher) + gob.Register(tables) + break + default: + fmt.Println("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.") + } + } x.SetMapper(core.GonicMapper{})