diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-07 00:24:53 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-07 00:24:53 -0700 |
commit | b2e4fd9372f27d3202f07a329a5077b93a4b7390 (patch) | |
tree | 7a00e7753fdf3b4b8f8976e35fb4dd4c564c45a9 /system/db/config.go | |
parent | b5f028f0a720f1d23d2ce79d3e885fcb524bb79a (diff) |
adding cache control and etags to responses for static assets + moved handlers/helper upload func
Diffstat (limited to 'system/db/config.go')
-rw-r--r-- | system/db/config.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/system/db/config.go b/system/db/config.go index e421ff5..5bee0b3 100644 --- a/system/db/config.go +++ b/system/db/config.go @@ -2,19 +2,32 @@ package db import ( "bytes" + "encoding/base64" "encoding/json" + "fmt" "net/url" + "time" "github.com/boltdb/bolt" "github.com/gorilla/schema" "github.com/nilslice/cms/system/admin/config" ) +var configCache url.Values + +func init() { + configCache = make(url.Values) +} + // SetConfig sets key:value pairs in the db for configuration settings func SetConfig(data url.Values) error { err := store.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("_config")) + if data.Get("cache") == "invalidate" { + data.Set("etag", NewEtag()) + } + cfg := &config.Config{} dec := schema.NewDecoder() dec.SetAliasTag("json") // allows simpler struct tagging when creating a content type @@ -40,6 +53,8 @@ func SetConfig(data url.Values) error { return err } + configCache = data + return nil } @@ -52,6 +67,10 @@ func Config(key string) ([]byte, error) { return nil, err } + if len(cfg) < 1 { + return nil, nil + } + err = json.Unmarshal(cfg, &kv) if err != nil { return nil, err @@ -75,3 +94,16 @@ func ConfigAll() ([]byte, error) { return val.Bytes(), nil } + +// ConfigCache is a in-memory cache of the Configs for quicker lookups +func ConfigCache(key string) string { + return configCache.Get(key) +} + +// NewEtag generates a new Etag for response caching +func NewEtag() string { + now := fmt.Sprintf("%d", time.Now().Unix()) + etag := base64.StdEncoding.EncodeToString([]byte(now)) + + return etag +} |