Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 64bcf90

Browse files
authored
feat(cache): add inmemory cache (#10)
* add lru * add LRU * remove lfu * re-order the cached mechanism * add cache control header * refactor cache packeg * add implemetantaion inmem cache
1 parent eee9c78 commit 64bcf90

10 files changed

Lines changed: 238 additions & 76 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
.vscode

cache/cache.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cache
2+
3+
import (
4+
"errors"
5+
"time"
6+
)
7+
8+
var (
9+
// ErrInvalidCachedResponse will throw if the cached response is invalid
10+
ErrInvalidCachedResponse = errors.New("Cached Response is Invalid")
11+
// ErrFailedToSaveToCache will throw if the item can't be saved to cache
12+
ErrFailedToSaveToCache = errors.New("Failed to save item")
13+
// ErrCacheMissed will throw if an item can't be retrieved (due to invalid, or missing)
14+
ErrCacheMissed = errors.New("Cache is missing")
15+
)
16+
17+
// Interactor ...
18+
type Interactor interface {
19+
Set(key string, value CachedResponse) error
20+
Get(key string) (CachedResponse, error)
21+
Delete(key string) error
22+
}
23+
24+
// CachedResponse represent the cacher struct item
25+
type CachedResponse struct {
26+
StatusCode int `json:"statusCode"`
27+
DumpedResponse []byte `json:"response"`
28+
DumpedBody []byte `json:"body"`
29+
RequestURI string `json:"requestUri"`
30+
RequestMethod string `json:"requestMethod"`
31+
CachedTime time.Time `json:"cachedTime"`
32+
}
33+
34+
// Validate will validate the cached response
35+
func (c *CachedResponse) Validate() (err error) {
36+
if c.StatusCode == 0 {
37+
return ErrInvalidCachedResponse
38+
}
39+
40+
if c.RequestMethod == "" {
41+
return ErrInvalidCachedResponse
42+
}
43+
44+
if c.RequestURI == "" {
45+
return ErrInvalidCachedResponse
46+
}
47+
48+
if len(c.DumpedResponse) == 0 {
49+
return ErrInvalidCachedResponse
50+
}
51+
52+
if c.CachedTime.IsZero() {
53+
return ErrInvalidCachedResponse
54+
}
55+
56+
return
57+
}

cache/inmem/inmem.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package inmem
2+
3+
import (
4+
memcache "github.com/bxcodec/gotcha/cache"
5+
"github.com/bxcodec/hache/cache"
6+
)
7+
8+
type inmemCache struct {
9+
cache memcache.Cache
10+
}
11+
12+
// NewCache ...
13+
func NewCache(c memcache.Cache) cache.Interactor {
14+
return &inmemCache{
15+
cache: c,
16+
}
17+
}
18+
19+
func (i *inmemCache) Set(key string, value cache.CachedResponse) (err error) {
20+
return i.cache.Set(key, value)
21+
}
22+
23+
func (i *inmemCache) Get(key string) (res cache.CachedResponse, err error) {
24+
item, err := i.cache.Get(key)
25+
if err != nil {
26+
return
27+
}
28+
res = item.(cache.CachedResponse)
29+
return
30+
}
31+
32+
func (i *inmemCache) Delete(key string) (err error) {
33+
return i.cache.Delete(key)
34+
}

cacher.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
module github.com/bxcodec/hache
2+
3+
require (
4+
github.com/bxcodec/gotcha v1.0.0-beta.2
5+
github.com/hashicorp/golang-lru v0.5.1
6+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/bxcodec/gotcha v1.0.0-beta.2 h1:0jY/Mx6O5jzM2fkcz84zzyy67hLu/bKGJEFTtcRmw5I=
2+
github.com/bxcodec/gotcha v1.0.0-beta.2/go.mod h1:MEL9PRYL9Squu1zxreMIzJU6xtMouPmQybWEtXrL1nk=
3+
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
4+
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

hache.go

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package hache
22

33
import (
4-
"errors"
54
"net/http"
65
"time"
7-
)
86

9-
var (
10-
// ErrInvalidCachedResponse will throw if the cached response is invalid
11-
ErrInvalidCachedResponse = errors.New("Cached Response is Invalid")
7+
"github.com/bxcodec/gotcha"
8+
inmemcache "github.com/bxcodec/gotcha/cache"
9+
"github.com/bxcodec/hache/cache"
10+
"github.com/bxcodec/hache/cache/inmem"
1211
)
1312

1413
// New ...
15-
func New(client *http.Client, cacheInteractor CacheInteractor) (err error) {
14+
func New(client *http.Client, cacheInteractor cache.Interactor) (err error) {
15+
newClient(client, cacheInteractor)
16+
return
17+
}
18+
19+
func newClient(client *http.Client, cacheInteractor cache.Interactor) (err error) {
1620
roundtrip := &RoundTrip{
1721
DefaultRoundTripper: client.Transport,
1822
CacheInteractor: cacheInteractor,
@@ -24,40 +28,15 @@ func New(client *http.Client, cacheInteractor CacheInteractor) (err error) {
2428
// NewWithInmemoryCache will create a complete cache-support of HTTP client with using inmemory cache.
2529
// If the duration not set, the cache will use LFU algorithm
2630
func NewWithInmemoryCache(client *http.Client, duration ...time.Duration) (err error) {
27-
panic("TODO: (bxcodec)")
28-
return
29-
}
30-
31-
// CachedResponse represent the cacher struct item
32-
type CachedResponse struct {
33-
StatusCode int `json:"statusCode"`
34-
DumpedResponse []byte `json:"body"`
35-
RequestURI string `json:"requestUri"`
36-
RequestMethod string `json:"requestMethod"`
37-
CachedTime time.Time `json:"cachedTime"`
38-
}
39-
40-
// Validate will validate the cached response
41-
func (c *CachedResponse) Validate() (err error) {
42-
if c.StatusCode == 0 {
43-
return ErrInvalidCachedResponse
44-
}
45-
46-
if c.RequestMethod == "" {
47-
return ErrInvalidCachedResponse
48-
}
49-
50-
if c.RequestURI == "" {
51-
return ErrInvalidCachedResponse
52-
}
53-
54-
if len(c.DumpedResponse) == 0 {
55-
return ErrInvalidCachedResponse
56-
}
57-
58-
if c.CachedTime.IsZero() {
59-
return ErrInvalidCachedResponse
31+
var expiryTime time.Duration
32+
if len(duration) > 0 {
33+
expiryTime = duration[0]
6034
}
35+
c := gotcha.New(
36+
gotcha.NewOption().SetAlgorithm(inmemcache.LRUAlgorithm).
37+
SetExpiryTime(expiryTime).SetMaxSizeItem(100),
38+
)
6139

40+
newClient(client, inmem.NewCache(c))
6241
return
6342
}

inmem/lfu/.gitkeep

Whitespace-only changes.

inmem/lru/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)