Skip to content

Commit

Permalink
Added option for disabling cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mtslzr committed May 13, 2020
1 parent 9b83b65 commit b9f902a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Settings for cache (pokeapi.CacheSettings)
- Additional testing and documentation for cache settings
- User settings for caching (pokeapi.CacheSettings)
- CustomExpire: set a custom cache expiration (in minutes)
- UseCache: turn caching on/off

## [1.3.2] / 2020.04.03
### Added
Expand Down
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,12 +1000,35 @@ As an alternative to `pokeapi.Resource()`, you can use Search to filter resource

### Caching

Calls are automatically cached to cut down on API traffic to PokeAPI, with subsequent calls returning local data. You can clear *all* existing cache data with `pokeapi.ClearCache()`.
Calls are automatically cached to cut down on API traffic to PokeAPI, with subsequent calls returning local data.

By default, cache lasts five minutes. You can adjust this by setting `pokeapi.CacheSettings.CustomExpire` to another number, in minutes. For example, to use a twenty-minute cache expiration:
#### Clearing Cache

```go
// Clear all existing cache entries.
pokeapi.ClearCache()
```

#### Custom Expiration

Custom cache expiration remains for all calls until changed or unset.

```go
// Set cache expiration to twenty minutes.
pokeapi.CacheSettings.CustomExpire = 20
// Turn custom expiration back off.
pokeapi.CacheSettings.CustomExpire = 0
```

Forced non-cached calls coming soon.
#### Disable Cache

_Please be considerate of PokeAPI and be sure to always operate within this requested limits._

As with custom expiration, this setting remains for all calls until changed or unset.

```go
// Disable checking for cached data
pokeapi.CacheSettings.UseCache = false
// Re-enable checking for cached data
pokeapi.CacheSettings.UseCache = true
```
5 changes: 3 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package pokeapi
import "time"

var CacheSettings = Settings{
NoCache: false,
CustomExpire: 0,
UseCache: true,
}

var defaultCacheSettings = defaultSettings{
Expand All @@ -20,7 +21,7 @@ type defaultSettings struct {
// CacheSettings are user settings for cache expiration.
type Settings struct {
CustomExpire time.Duration
NoCache bool
UseCache bool
}

// ClearCache clears all cached data.
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {

func do(endpoint string, obj interface{}) error {
cached, found := c.Get(endpoint)
if found {
if found && CacheSettings.UseCache {
return json.Unmarshal(cached.([]byte), &obj)
}

Expand Down
16 changes: 15 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,25 @@ func TestCustomExpiration(t *testing.T) {
assert.Equal(t, defaultExpire, expires1.Minute(),
"Expect expiration time to match default setting.")

ClearCache()
CacheSettings.CustomExpire = 20
customExpire := time.Now().Add(CacheSettings.CustomExpire * time.Minute).Minute()
ClearCache()
_ = do(endpoint, &mockResource)
_, expires2, _ := c.GetWithExpiration(endpoint)
assert.Equal(t, customExpire, expires2.Minute(),
"Expect expiration time to match custom setting.")
}

func TestNoCache(t *testing.T) {
ClearCache()
_ = do(endpoint, &mockResource)
_, expires1, found1 := c.GetWithExpiration(endpoint)
assert.Equal(t, true, found1,
"Expect to have cached data after first call.")

CacheSettings.NoCache = true
_ = do(endpoint, &mockResource)
_, expires2, _ := c.GetWithExpiration(endpoint)
assert.NotEqual(t, expires1, expires2,
"Expect cache expiration not to match first call.")
}

0 comments on commit b9f902a

Please sign in to comment.