This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
example_storages_test.go
106 lines (98 loc) · 2.57 KB
/
example_storages_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package httpcache_test
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/bxcodec/httpcache"
"github.com/bxcodec/httpcache/cache/redis"
)
func Example_inMemoryStorageDefault() {
client := &http.Client{}
handler, err := httpcache.NewWithInmemoryCache(client, true, time.Second*15)
if err != nil {
log.Fatal(err)
}
processCachedRequest(client, handler)
// Example Output:
/*
2020/06/21 13:14:51 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 940086 micro-second
Status Code 200
Sequence >>> 0
2020/06/21 13:14:53 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 73679 micro-second
Status Code 200
Sequence >>> 1
Response time: 126 micro-second
Status Code 200
Sequence >>> 2
Response time: 96 micro-second
Status Code 200
Sequence >>> 3
Response time: 102 micro-second
Status Code 200
Sequence >>> 4
Response time: 94 micro-second
Status Code 200
Sequence >>> 5
*/
}
func Example_redisStorage() {
client := &http.Client{}
handler, err := httpcache.NewWithRedisCache(client, true, &redis.CacheOptions{
Addr: "localhost:6379",
}, time.Second*15)
if err != nil {
log.Fatal(err)
}
processCachedRequest(client, handler)
// Example Output:
/*
2020/06/21 13:14:51 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 940086 micro-second
Status Code 200
Sequence >>> 0
2020/06/21 13:14:53 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 73679 micro-second
Status Code 200
Sequence >>> 1
Response time: 126 micro-second
Status Code 200
Sequence >>> 2
Response time: 96 micro-second
Status Code 200
Sequence >>> 3
Response time: 102 micro-second
Status Code 200
Sequence >>> 4
Response time: 94 micro-second
Status Code 200
Sequence >>> 5
*/
}
func processCachedRequest(client *http.Client, handler *httpcache.CacheHandler) {
for i := 0; i < 100; i++ {
startTime := time.Now()
req, err := http.NewRequestWithContext(context.TODO(), "GET", "https://imantumorang.com", http.NoBody)
if err != nil {
log.Fatal((err))
}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response time: %v micro-second\n", time.Since(startTime).Microseconds())
fmt.Println("Status Code", res.StatusCode)
time.Sleep(time.Second * 1)
fmt.Println("Sequence >>> ", i)
if i%5 == 0 {
err := handler.CacheInteractor.Flush()
if err != nil {
log.Fatal(err)
}
}
res.Body.Close()
}
}