added options to configure cache

This commit is contained in:
kolaente 2018-09-13 19:53:03 +02:00
parent 23dc9f777e
commit 416745ddab
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 70 additions and 4 deletions

View file

@ -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

20
Gopkg.lock generated
View file

@ -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",

View file

@ -21,3 +21,15 @@ 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: ''

View file

@ -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: ''
```

View file

@ -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")

View file

@ -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{})