Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mtslzr/pokeapi-go
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.3.2
Choose a base ref
...
head repository: mtslzr/pokeapi-go
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.4.0
Choose a head ref
  • 7 commits
  • 6 files changed
  • 1 contributor

Commits on Apr 3, 2020

  1. Merge pull request #23 from mtslzr/release/v1.3.2

    Post-Release v1.3.2
    mtslzr authored Apr 3, 2020
    Copy the full SHA
    26b56ef View commit details

Commits on May 13, 2020

  1. Copy the full SHA
    9b83b65 View commit details
  2. Copy the full SHA
    b9f902a View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1353ccd View commit details
  4. Merge pull request #24 from mtslzr/feature/cache_settings

    Settings for Cache
    mtslzr authored May 13, 2020
    Copy the full SHA
    5c00f77 View commit details
  5. Updated CHANGELOG for v1.4.0

    mtslzr committed May 13, 2020
    Copy the full SHA
    6274273 View commit details
  6. Merge pull request #25 from mtslzr/release/v1.4.0

    Release v1.4.0
    mtslzr authored May 13, 2020
    Copy the full SHA
    ee7a30b View commit details
Showing with 116 additions and 13 deletions.
  1. +8 −1 CHANGELOG.md
  2. +3 −0 Makefile
  3. +31 −2 README.md
  4. +39 −0 cache.go
  5. +3 −10 client.go
  6. +32 −0 client_test.go
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.0] / 2020.05.12
### Added
- 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
- Current version to README.
@@ -56,7 +62,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Resource endpoint for resource lists
- Unit tests for all endpoints

[Unreleased]: https://github.com/mtslzr/pokeapi-go/compare/v1.3.2...HEAD
[Unreleased]: https://github.com/mtslzr/pokeapi-go/compare/v1.4.0...HEAD
[1.4.0]: https://github.com/mtslzr/pokeapi-go/compare/v1.3.2...v1.4.0
[1.3.2]: https://github.com/mtslzr/pokeapi-go/compare/v1.3.1...v1.3.2
[1.3.1]: https://github.com/mtslzr/pokeapi-go/compare/v1.3.0...v1.3.1
[1.3.0]: https://github.com/mtslzr/pokeapi-go/compare/v1.2.1...v1.3.0
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -12,6 +12,9 @@ deps: tidy vend
test:
${GOTEST} -v -race ${CODECOVFLAGS} ./...

test-client:
${GOTEST} -v ./.

tidy:
${GOMOD} tidy -v

33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1000,6 +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 (up to five minutes currently) 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.

Forced non-cached calls and custom cache expiration coming soon.
#### 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
```

#### 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
```
39 changes: 39 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pokeapi

import "time"

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

var defaultCacheSettings = defaultSettings{
MaxExpire: 10 * time.Minute,
MinExpire: 5 * time.Minute,
}

// defaultSettings are settings not meant to be changed.
type defaultSettings struct {
MaxExpire time.Duration
MinExpire time.Duration
}

// CacheSettings are user settings for cache expiration.
type Settings struct {
CustomExpire time.Duration
UseCache bool
}

// ClearCache clears all cached data.
func ClearCache() {
c.Flush()
}

// setCache adds new item to local cache.
func setCache(endpoint string, body []byte) {
if CacheSettings.CustomExpire != 0 {
c.Set(endpoint, body, CacheSettings.CustomExpire * time.Minute)
} else {
c.SetDefault(endpoint, body)
}
}
13 changes: 3 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
@@ -10,23 +10,16 @@ import (
)

const apiurl = "https://pokeapi.co/api/v2/"
const cachemin = 5
const cachemax = 10

var c *cache.Cache

func init() {
c = cache.New(cachemin*time.Minute, cachemax*time.Minute)
}

// ClearCache clears all cached data.
func ClearCache() {
c.Flush()
c = cache.New(defaultCacheSettings.MinExpire, defaultCacheSettings.MaxExpire)
}

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

@@ -47,6 +40,6 @@ func do(endpoint string, obj interface{}) error {
return err
}

c.Set(endpoint, body, cache.DefaultExpiration)
setCache(endpoint, body)
return json.Unmarshal(body, &obj)
}
32 changes: 32 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package pokeapi
import (
"encoding/json"
"testing"
"time"

"github.com/mtslzr/pokeapi-go/structs"
"github.com/stretchr/testify/assert"
@@ -49,3 +50,34 @@ func TestClearCache(t *testing.T) {
assert.Equal(t, nil, nocache,
"Expect no data after flushing cache.")
}

func TestCustomExpiration(t *testing.T) {
ClearCache()
defaultExpire := time.Now().Add(defaultCacheSettings.MinExpire).Minute()
_ = do(endpoint, &mockResource)
_, expires1, _ := c.GetWithExpiration(endpoint)
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()
_ = 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.UseCache = false
_ = do(endpoint, &mockResource)
_, expires2, _ := c.GetWithExpiration(endpoint)
assert.NotEqual(t, expires1, expires2,
"Expect cache expiration not to match first call.")
}