-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching_fetcher.go
57 lines (48 loc) · 1.21 KB
/
caching_fetcher.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
package main
import (
"fmt"
"sync"
)
type result struct {
fakeResult
}
//NewCachingFetcher returns a caching fetcher based on the specified fetcher
func NewCachingFetcher(fetcher Fetcher) Fetcher {
return &CachingFetcher{
fetcher: fetcher,
mutex: &sync.Mutex{},
visitedUrls: make(map[string]*result),
}
}
//CachingFetcher caches data fetched by a fetcher
type CachingFetcher struct {
fetcher Fetcher
mutex *sync.Mutex
visitedUrls map[string]*result
}
//Fetch caches successfully fetched data from the given url
func (cf *CachingFetcher) Fetch(url string) (body string, urls []string, err error) {
if visitAsync {
trace("waiting for lock")
cf.mutex.Lock()
trace("lock acquired")
defer cf.mutex.Unlock()
}
if r, ok := cf.visitedUrls[url]; ok {
debugf("cache hit: %v", url)
return r.body, r.urls, nil
}
body, urls, err = cf.fetcher.Fetch(url)
if err == nil {
debugf("caching data for %v", url)
cf.visitedUrls[url] = &result{
fakeResult{body,
urls},
}
}
return
}
//GoString returns a representation of CachingFetcher
func (cf *CachingFetcher) GoString() string {
return fmt.Sprintf("CachingFetcher{visitedUrls: %v, fetcher: %#v}", cf.visitedUrls, cf.fetcher)
}