Skip to content

Commit

Permalink
Merge pull request #33 from ipinfo/uman/vsn-cache-key
Browse files Browse the repository at this point in the history
Add versioned cache key.
  • Loading branch information
UmanShahzad authored Apr 22, 2021
2 parents be16a8e + 2b4d2a2 commit 83b8917
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.5.0

- Added versioned cache keys.
This allows more reliable changes to cached data in the future without
causing confusing incompatibilities. This should be transparent to the user.

# 2.4.0

- Added support for IP Map API.
Expand Down
2 changes: 2 additions & 0 deletions example/cache/in-memory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net"
"os"
"time"

"github.com/ipinfo/go/v2/ipinfo"
Expand All @@ -16,6 +17,7 @@ func main() {
cache.NewInMemory().WithExpiration(5 * time.Minute),
),
)
ipinfo.SetToken(os.Getenv("TOKEN"))
ip := net.ParseIP("8.8.8.8")

for i := 0; i < 2; i++ {
Expand Down
2 changes: 2 additions & 0 deletions example/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net"
"os"

"github.com/ipinfo/go/v2/ipinfo"
"github.com/ipinfo/go/v2/ipinfo/cache"
Expand Down Expand Up @@ -39,6 +40,7 @@ var dummyCache = ipinfo.NewCache(newDummyCacheEngine())

func main() {
ipinfo.SetCache(dummyCache)
ipinfo.SetToken(os.Getenv("TOKEN"))
ip := net.ParseIP("8.8.8.8")

for i := 0; i < 2; i++ {
Expand Down
4 changes: 2 additions & 2 deletions ipinfo/asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) {

// perform cache lookup.
if c.Cache != nil {
if res, err := c.Cache.Get(asn); err == nil {
if res, err := c.Cache.Get(cacheKey(asn)); err == nil {
return res.(*ASNDetails), nil
}
}
Expand All @@ -83,7 +83,7 @@ func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) {

// cache req result
if c.Cache != nil {
if err := c.Cache.Set(asn, v); err != nil {
if err := c.Cache.Set(cacheKey(asn), v); err != nil {
return v, err
}
}
Expand Down
4 changes: 2 additions & 2 deletions ipinfo/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *Client) GetBatch(
if c.Cache != nil {
lookupUrls = make([]string, 0, len(urls)/2)
for _, url := range urls {
if res, err := c.Cache.Get(url); err == nil {
if res, err := c.Cache.Get(cacheKey(url)); err == nil {
result[url] = res
} else {
lookupUrls = append(lookupUrls, url)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (c *Client) GetBatch(
if c.Cache != nil {
for _, url := range lookupUrls {
if v, exists := result[url]; exists {
if err := c.Cache.Set(url, v); err != nil {
if err := c.Cache.Set(cacheKey(url), v); err != nil {
// NOTE: still return the result even if the cache fails.
return result, err
}
Expand Down
9 changes: 9 additions & 0 deletions ipinfo/cache.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package ipinfo

import (
"fmt"

"github.com/ipinfo/go/v2/ipinfo/cache"
)

const cacheKeyVsn = "1"

// Cache represents the internal cache used by the IPinfo client.
type Cache struct {
cache.Interface
Expand All @@ -13,3 +17,8 @@ type Cache struct {
func NewCache(engine cache.Interface) *Cache {
return &Cache{Interface: engine}
}

// return a versioned cache key.
func cacheKey(k string) string {
return fmt.Sprintf("%s:%s", k, cacheKeyVsn)
}
2 changes: 1 addition & 1 deletion ipinfo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
defaultBaseURL = "https://ipinfo.io/"
defaultUserAgent = "IPinfoClient/Go/2.4.0"
defaultUserAgent = "IPinfoClient/Go/2.5.0"
)

// A Client is the main handler to communicate with the IPinfo API.
Expand Down
4 changes: 2 additions & 2 deletions ipinfo/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Client) GetIPInfo(ip net.IP) (*Core, error) {

// perform cache lookup.
if c.Cache != nil {
if res, err := c.Cache.Get(relURL); err == nil {
if res, err := c.Cache.Get(cacheKey(relURL)); err == nil {
return res.(*Core), nil
}
}
Expand All @@ -122,7 +122,7 @@ func (c *Client) GetIPInfo(ip net.IP) (*Core, error) {

// cache req result
if c.Cache != nil {
if err := c.Cache.Set(relURL, v); err != nil {
if err := c.Cache.Set(cacheKey(relURL), v); err != nil {
// NOTE: still return the value even if the cache fails.
return v, err
}
Expand Down
2 changes: 1 addition & 1 deletion ipinfo/ipinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package ipinfo
var DefaultClient *Client

func init() {
/* Create a global, default client. */
// create a global, default client.
DefaultClient = NewClient(nil, nil, "")
}

0 comments on commit 83b8917

Please sign in to comment.