Skip to content

Commit

Permalink
Merge pull request #59 from savagedev/redis-cache-expiration
Browse files Browse the repository at this point in the history
Correct cache expiration value calculation
  • Loading branch information
ezeoleaf authored Jun 14, 2022
2 parents a3b1ebc + 9679c3d commit 55d757e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
10 changes: 9 additions & 1 deletion provider/github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (p Provider) isRepoNotInCache(repoID int64) bool {

switch {
case err == redis.Nil:
err := p.CacheClient.Set(k, true, time.Duration(p.Config.Periodicity)*time.Minute)
err := p.CacheClient.Set(k, true, p.cacheExpirationMinutes())
if err != nil {
return false
}
Expand All @@ -165,6 +165,14 @@ func (p Provider) isRepoNotInCache(repoID int64) bool {
return false
}

func (p Provider) cacheExpirationMinutes() time.Duration {
expirationMinutes := p.Config.CacheSize * p.Config.Periodicity
if expirationMinutes < 0 {
expirationMinutes = 0
}
return time.Duration(expirationMinutes) * time.Minute
}

func (p Provider) getContent(repo *github.Repository) *domain.Content {
c := domain.Content{Title: repo.Name, Subtitle: repo.Description, URL: repo.HTMLURL, ExtraData: []string{}}

Expand Down
58 changes: 58 additions & 0 deletions provider/github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,61 @@ func TestGetRepo(t *testing.T) {
})
}
}

func TestCacheExpiration(t *testing.T) {
for _, tc := range []struct {
Name string
mockConfig config.Config
cacheClient mock.CacheClientMock
returnValue time.Duration
}{
{
Name: "Test success",
mockConfig: config.Config{
CacheSize: 10,
Periodicity: 2,
},
cacheClient: mock.CacheClientMock{
SetFn: func(key string, value interface{}, exp time.Duration) error {
return nil
},
},
returnValue: time.Duration(20) * time.Minute,
},
{
Name: "Test negative cache size",
mockConfig: config.Config{
CacheSize: -10,
Periodicity: 2,
},
cacheClient: mock.CacheClientMock{
SetFn: func(key string, value interface{}, exp time.Duration) error {
return nil
},
},
returnValue: time.Duration(0) * time.Minute,
},
{
Name: "Test negative periodicity",
mockConfig: config.Config{
CacheSize: 10,
Periodicity: -2,
},
cacheClient: mock.CacheClientMock{
SetFn: func(key string, value interface{}, exp time.Duration) error {
return nil
},
},
returnValue: time.Duration(0) * time.Minute,
},
} {
t.Run(tc.Name, func(t *testing.T) {
p := Provider{Config: tc.mockConfig, CacheClient: tc.cacheClient}
resp := p.cacheExpirationMinutes()

if tc.returnValue != resp {
t.Errorf("expected %v as value, got %v instead", tc.returnValue, resp)
}
})
}
}

0 comments on commit 55d757e

Please sign in to comment.