11package hache
22
33import (
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
2630func 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}
0 commit comments