forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54c5de2
commit b1b3fb2
Showing
13 changed files
with
361 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cache | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/dgraph-io/ristretto" | ||
) | ||
|
||
var ErrParsing = errors.New("parsing error") | ||
|
||
type Config struct { | ||
NumCounters int64 `yaml:"num_counters" mapstructure:"num_counters" default:"10000000"` | ||
MaxCost int64 `yaml:"max_cost" mapstructure:"max_cost" default:"1073741824"` | ||
BufferItems int64 `yaml:"buffer_items" mapstructure:"buffer_items" default:"64"` | ||
Metrics bool `yaml:"metrics" mapstructure:"metrics" default:"true"` | ||
TTLInSeconds int `yaml:"ttl_in_seconds" mapstructure:"ttl_in_seconds" default:"3600"` | ||
} | ||
|
||
type Cache struct { | ||
*ristretto.Cache | ||
config Config | ||
} | ||
|
||
func NewCache(config Config) (Cache, error) { | ||
cache, err := ristretto.NewCache(&ristretto.Config{ | ||
NumCounters: config.NumCounters, | ||
MaxCost: config.MaxCost, | ||
BufferItems: config.MaxCost, | ||
Metrics: config.Metrics, | ||
}) | ||
if err != nil { | ||
return Cache{}, err | ||
} | ||
|
||
return Cache{ | ||
Cache: cache, | ||
config: config, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/goto/shield/core/group" | ||
) | ||
|
||
var keyPrefix = "group" | ||
|
||
type GroupRepository interface { | ||
GetBySlug(ctx context.Context, slug string) (group.Group, error) | ||
} | ||
|
||
type CachedGroupRepository struct { | ||
cache Cache | ||
repository GroupRepository | ||
} | ||
|
||
func NewCachedGroupRepository(cache Cache, repository GroupRepository) *CachedGroupRepository { | ||
return &CachedGroupRepository{ | ||
cache: cache, | ||
repository: repository, | ||
} | ||
} | ||
|
||
func getKey(identifier string) string { | ||
return fmt.Sprintf("%s:%s", keyPrefix, identifier) | ||
} | ||
|
||
func (r CachedGroupRepository) GetBySlug(ctx context.Context, slug string) (group.Group, error) { | ||
key := getKey(slug) | ||
grp, found := r.cache.Get(key) | ||
if !found { | ||
grp, err := r.repository.GetBySlug(ctx, slug) | ||
if err != nil { | ||
return group.Group{}, err | ||
} | ||
|
||
r.cache.SetWithTTL(key, grp, 0, time.Duration(r.cache.config.TTLInSeconds)*time.Second) | ||
return grp, nil | ||
} | ||
|
||
grpParsed, ok := grp.(group.Group) | ||
if !ok { | ||
return group.Group{}, ErrParsing | ||
} | ||
|
||
return grpParsed, nil | ||
} |
Oops, something went wrong.