Skip to content

Commit

Permalink
feat: add servicekey to cache (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors authored Jun 15, 2020
1 parent 088e4c2 commit cc08b15
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions rediscache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ type Options struct {
LocalCacheTimeout time.Duration
CacheTimeout time.Duration
CacheVersion int
ServiceKey string
}

var DefaultOptions = &Options{
CacheVersion: 1,
CacheTimeout: 12 * time.Hour,
LocalCacheTimeout: 1 * time.Hour,
ServiceKey: "cache",
}

// Init sets up a cache.
Expand Down
4 changes: 2 additions & 2 deletions rediscache/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

func (c *Cache) createFuncCacheKey(funcKey, versionKey, lookup string) string {
return fmt.Sprintf("0:cache:f:%d:%s:%s:%x", c.opts.CacheVersion, funcKey, versionKey, util.Hash(lookup))
return fmt.Sprintf("0:%s:f:%d:%s:%s:%x", c.opts.ServiceKey, c.opts.CacheVersion, funcKey, versionKey, util.Hash(lookup))
}

func (c *Cache) createFuncVersionCacheKey(funcKey, versionKey string) string {
return fmt.Sprintf("0:cache:f:%d:%s:%s:version", c.opts.CacheVersion, funcKey, versionKey)
return fmt.Sprintf("0:%s:f:%d:%s:%s:version", c.opts.ServiceKey, c.opts.CacheVersion, funcKey, versionKey)
}

func (c *Cache) FuncCacheInvalidate(funcKey, versionKey string) error {
Expand Down
4 changes: 2 additions & 2 deletions rediscache/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

func (c *Cache) createModelCacheKey(schema, model, lookup string) string {
return fmt.Sprintf("%s:cache:m:%d:%s:%x", schema, c.opts.CacheVersion, model, util.Hash(lookup))
return fmt.Sprintf("%s:%s:m:%d:%s:%x", schema, c.opts.ServiceKey, c.opts.CacheVersion, model, util.Hash(lookup))
}

func (c *Cache) createModelVersionCacheKey(schema, model string, pk interface{}) string {
return fmt.Sprintf("%s:cache:m:%d:%s:%v:version", schema, c.opts.CacheVersion, model, pk)
return fmt.Sprintf("%s:%s:m:%d:%s:%v:version", schema, c.opts.ServiceKey, c.opts.CacheVersion, model, pk)
}

func getSchemaKey(db orm.DB) string {
Expand Down

0 comments on commit cc08b15

Please sign in to comment.