Skip to content

Commit 83b8917

Browse files
authored
Merge pull request #33 from ipinfo/uman/vsn-cache-key
Add versioned cache key.
2 parents be16a8e + 2b4d2a2 commit 83b8917

File tree

9 files changed

+27
-8
lines changed

9 files changed

+27
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.5.0
2+
3+
- Added versioned cache keys.
4+
This allows more reliable changes to cached data in the future without
5+
causing confusing incompatibilities. This should be transparent to the user.
6+
17
# 2.4.0
28

39
- Added support for IP Map API.

example/cache/in-memory/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"net"
7+
"os"
78
"time"
89

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

2123
for i := 0; i < 2; i++ {

example/cache/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"net"
7+
"os"
78

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

4041
func main() {
4142
ipinfo.SetCache(dummyCache)
43+
ipinfo.SetToken(os.Getenv("TOKEN"))
4244
ip := net.ParseIP("8.8.8.8")
4345

4446
for i := 0; i < 2; i++ {

ipinfo/asn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) {
6161

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

8484
// cache req result
8585
if c.Cache != nil {
86-
if err := c.Cache.Set(asn, v); err != nil {
86+
if err := c.Cache.Set(cacheKey(asn), v); err != nil {
8787
return v, err
8888
}
8989
}

ipinfo/batch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (c *Client) GetBatch(
9696
if c.Cache != nil {
9797
lookupUrls = make([]string, 0, len(urls)/2)
9898
for _, url := range urls {
99-
if res, err := c.Cache.Get(url); err == nil {
99+
if res, err := c.Cache.Get(cacheKey(url)); err == nil {
100100
result[url] = res
101101
} else {
102102
lookupUrls = append(lookupUrls, url)
@@ -226,7 +226,7 @@ func (c *Client) GetBatch(
226226
if c.Cache != nil {
227227
for _, url := range lookupUrls {
228228
if v, exists := result[url]; exists {
229-
if err := c.Cache.Set(url, v); err != nil {
229+
if err := c.Cache.Set(cacheKey(url), v); err != nil {
230230
// NOTE: still return the result even if the cache fails.
231231
return result, err
232232
}

ipinfo/cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package ipinfo
22

33
import (
4+
"fmt"
5+
46
"github.com/ipinfo/go/v2/ipinfo/cache"
57
)
68

9+
const cacheKeyVsn = "1"
10+
711
// Cache represents the internal cache used by the IPinfo client.
812
type Cache struct {
913
cache.Interface
@@ -13,3 +17,8 @@ type Cache struct {
1317
func NewCache(engine cache.Interface) *Cache {
1418
return &Cache{Interface: engine}
1519
}
20+
21+
// return a versioned cache key.
22+
func cacheKey(k string) string {
23+
return fmt.Sprintf("%s:%s", k, cacheKeyVsn)
24+
}

ipinfo/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
const (
1515
defaultBaseURL = "https://ipinfo.io/"
16-
defaultUserAgent = "IPinfoClient/Go/2.4.0"
16+
defaultUserAgent = "IPinfoClient/Go/2.5.0"
1717
)
1818

1919
// A Client is the main handler to communicate with the IPinfo API.

ipinfo/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (c *Client) GetIPInfo(ip net.IP) (*Core, error) {
100100

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

123123
// cache req result
124124
if c.Cache != nil {
125-
if err := c.Cache.Set(relURL, v); err != nil {
125+
if err := c.Cache.Set(cacheKey(relURL), v); err != nil {
126126
// NOTE: still return the value even if the cache fails.
127127
return v, err
128128
}

ipinfo/ipinfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ package ipinfo
44
var DefaultClient *Client
55

66
func init() {
7-
/* Create a global, default client. */
7+
// create a global, default client.
88
DefaultClient = NewClient(nil, nil, "")
99
}

0 commit comments

Comments
 (0)