Skip to content

Commit

Permalink
organise cache options
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 29, 2023
1 parent 806b890 commit 40da55c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.StringVar(&Workspace, "workspace", osenv.Value("SLACK_WORKSPACE", ""), "Slack workspace to use") // TODO: load from configuration.
}
if mask&OmitUserCacheFlag == 0 {
fs.BoolVar(&SlackOptions.NoUserCache, "no-user-cache", false, "disable user cache")
fs.DurationVar(&SlackOptions.MaxUserCacheAge, "user-cache-age", slackdump.DefOptions.MaxUserCacheAge, "maximum user cache age")
fs.BoolVar(&SlackOptions.UserCache.Disabled, "no-user-cache", false, "disable user cache")
fs.DurationVar(&SlackOptions.UserCache.MaxAge, "user-cache-age", slackdump.DefOptions.UserCache.MaxAge, "maximum user cache age")
}
}
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/emoji/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func run(ctx context.Context, cmd *base.Command, args []string) error {
base.SetExitStatus(base.SAuthError)
return fmt.Errorf("auth error: %s", err)
}
cfg.SlackOptions.NoUserCache = true // don't need users for emojis
cfg.SlackOptions.UserCache.Disabled = true // don't need users for emojis
sess, err := slackdump.NewWithOptions(ctx, prov, cfg.SlackOptions)
if err != nil {
base.SetExitStatus(base.SApplicationError)
Expand Down
27 changes: 25 additions & 2 deletions cmd/slackdump/internal/list/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/internal/cache"
"github.com/rusq/slackdump/v2/types"
)

var CmdListChannels = &base.Command{
Expand Down Expand Up @@ -34,11 +36,32 @@ func listChannels(ctx context.Context, cmd *base.Command, args []string) error {
if len(args) > 0 {
filename = args[0]
}
a, err := sess.GetChannels(ctx)
return a, filename, err

cc, ok := maybeLoadChanCache(cfg.CacheDir(), sess.Info().Team)
if ok {
return cc, filename, nil
}
cc, err := sess.GetChannels(ctx)
return cc, filename, err
}); err != nil {
return err
}

return nil
}

var chanCacheOpts = slackdump.CacheOptions{
Disabled: false,
}

func maybeLoadChanCache(cacheDir string, teamID string) (types.Channels, bool) {
m, err := cache.NewManager(cacheDir)
if err != nil {
return nil, false
}
cc, err := m.LoadChannels(teamID, chanCacheOpts.MaxAge)
if err != nil {
return nil, false
}
return cc, true
}
6 changes: 3 additions & 3 deletions cmd/slackdump/internal/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ func parseCmdLine(args []string) (params, error) {

// - cache controls
fs.StringVar(&p.appCfg.Options.CacheDir, "cache-dir", cfg.CacheDir(), "slackdump cache directory")
fs.StringVar(&p.appCfg.Options.UserCacheFilename, "user-cache-file", slackdump.DefOptions.UserCacheFilename, "user cache file`name`.")
fs.DurationVar(&p.appCfg.Options.MaxUserCacheAge, "user-cache-age", slackdump.DefOptions.MaxUserCacheAge, "user cache lifetime `duration`. Set this to 0 to disable cache.")
fs.BoolVar(&p.appCfg.Options.NoUserCache, "no-user-cache", slackdump.DefOptions.NoUserCache, "skip fetching users")
fs.StringVar(&p.appCfg.Options.UserCache.Filename, "user-cache-file", slackdump.DefOptions.UserCache.Filename, "user cache file`name`.")
fs.DurationVar(&p.appCfg.Options.UserCache.MaxAge, "user-cache-age", slackdump.DefOptions.UserCache.MaxAge, "user cache lifetime `duration`. Set this to 0 to disable cache.")
fs.BoolVar(&p.appCfg.Options.UserCache.Disabled, "no-user-cache", slackdump.DefOptions.UserCache.Disabled, "skip fetching users")

// - time frame options
fs.Var(&p.appCfg.Oldest, "dump-from", "`timestamp` of the oldest message to fetch from (i.e. 2020-12-31T23:59:59)")
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func printAll(m manager, current string, wsps []string) {
"C\tname\tfilename\tmodified\tteam\tuser\terror\n"+
"-\t-------\t------------\t-------------------\t---------\t--------\t-----")
cfg.SlackOptions.Logger = logger.Silent
cfg.SlackOptions.NoUserCache = true
cfg.SlackOptions.UserCache.Disabled = true
for _, name := range wsps {
curr := ""
if current == name {
Expand Down
36 changes: 21 additions & 15 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ const defNumWorkers = 4 // default number of file downloaders. it's here because
type Options struct {
Limits Limits

DumpFiles bool // will we save the conversation files?
UserCacheFilename string // user cache filename
MaxUserCacheAge time.Duration // how long the user cache is valid for.
NoUserCache bool // disable fetching users from the API.
CacheDir string // cache directory
Logger logger.Interface
Filesystem fsadapter.FS
DumpFiles bool // will we save the conversation files?
Filesystem fsadapter.FS

CacheDir string // cache directory
UserCache CacheOptions

Logger logger.Interface
}

// CacheOptions represents the options for the cache.
type CacheOptions struct {
Filename string
MaxAge time.Duration
Disabled bool
}

type Limits struct {
Expand Down Expand Up @@ -87,12 +94,11 @@ var DefOptions = Options{
Replies: 200, // the API-default is 1000 (see conversations.replies), but on large threads it may fail (see #54)
},
},
DumpFiles: false,
UserCacheFilename: "users.cache", // seems logical
MaxUserCacheAge: 4 * time.Hour, // quick math: that's 1/6th of a day, how's that, huh?
CacheDir: "", // default cache dir
Logger: logger.Default, // default logger is the... default logger
Filesystem: fsadapter.NewDirectory("."), // default filesystem is the current directory
DumpFiles: false,
UserCache: CacheOptions{Filename: "users.cache", MaxAge: 4 * time.Hour},
CacheDir: "", // default cache dir
Logger: logger.Default, // default logger is the... default logger
Filesystem: fsadapter.NewDirectory("."), // default filesystem is the current directory
}

var (
Expand Down Expand Up @@ -183,7 +189,7 @@ func NumWorkers(n int) Option {
func UserCacheFilename(s string) Option {
return func(options *Options) {
if s != "" {
options.UserCacheFilename = s
options.UserCache.Filename = s
}
}
}
Expand All @@ -192,7 +198,7 @@ func UserCacheFilename(s string) Option {
// will always use the API output, and never load cache.
func MaxUserCacheAge(d time.Duration) Option {
return func(options *Options) {
options.MaxUserCacheAge = d
options.UserCache.MaxAge = d
}
}

Expand Down
2 changes: 1 addition & 1 deletion slackdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewWithOptions(ctx context.Context, authProvider auth.Provider, opts Option
return nil, fmt.Errorf("failed to create the cache directory: %s", err)
}

if !sd.options.NoUserCache {
if !sd.options.UserCache.Disabled {
sd.l().Println("> checking user cache...")
users, err := sd.GetUsers(ctx)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ import (

// GetUsers retrieves all users either from cache or from the API.
func (sd *Session) GetUsers(ctx context.Context) (types.Users, error) {
// TODO: validate that the cache is from the same workspace, it can be done by team ID.
ctx, task := trace.NewTask(ctx, "GetUsers")
defer task.End()

if sd.options.NoUserCache {
if sd.options.UserCache.Disabled {
return types.Users{}, nil
}

// TODO make Manager a Session variable. Maybe?
m, err := cache.NewManager(sd.options.CacheDir, cache.WithUserBasename(sd.options.UserCacheFilename))
m, err := cache.NewManager(sd.options.CacheDir, cache.WithUserBasename(sd.options.UserCache.Filename))
if err != nil {
return nil, err
}

users, err := m.LoadUsers(sd.wspInfo.TeamID, sd.options.MaxUserCacheAge)
users, err := m.LoadUsers(sd.wspInfo.TeamID, sd.options.UserCache.MaxAge)
if err != nil {
if os.IsNotExist(err) {
sd.l().Println(" caching users for the first time")
Expand All @@ -44,8 +43,8 @@ func (sd *Session) GetUsers(ctx context.Context) (types.Users, error) {
return nil, err
}
if err := m.SaveUsers(sd.wspInfo.TeamID, users); err != nil {
trace.Logf(ctx, "error", "saving user cache to %q, error: %s", sd.options.UserCacheFilename, err)
sd.l().Printf("error saving user cache to %q: %s, but nevermind, let's continue", sd.options.UserCacheFilename, err)
trace.Logf(ctx, "error", "saving user cache to %q, error: %s", sd.options.UserCache.Filename, err)
sd.l().Printf("error saving user cache to %q: %s, but nevermind, let's continue", sd.options.UserCache.Filename, err)
}
}

Expand Down
6 changes: 2 additions & 4 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func TestSession_GetUsers(t *testing.T) {
{
"everything goes as planned",
fields{options: Options{
UserCacheFilename: gimmeTempFile(t, dir),
MaxUserCacheAge: 5 * time.Hour,
UserCache: CacheOptions{Filename: gimmeTempFile(t, dir), MaxAge: 5 * time.Hour},
Limits: Limits{
Tier2: TierLimits{Burst: 1},
Tier3: TierLimits{Burst: 1},
Expand All @@ -156,8 +155,7 @@ func TestSession_GetUsers(t *testing.T) {
{
"loaded from cache",
fields{options: Options{
UserCacheFilename: gimmeTempFileWithUsers(t, dir),
MaxUserCacheAge: 5 * time.Hour,
UserCache: CacheOptions{Filename: gimmeTempFileWithUsers(t, dir), MaxAge: 5 * time.Hour},
Limits: Limits{
Tier2: TierLimits{Burst: 1},
Tier3: TierLimits{Burst: 1},
Expand Down

0 comments on commit 40da55c

Please sign in to comment.