Last active
December 13, 2015 23:39
-
-
Save pandemicsyn/4993299 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io" | |
| "net/http" | |
| "os" | |
| "unsafe" | |
| "code.google.com/p/vitess/go/cache" | |
| ) | |
| type result struct { | |
| id int | |
| bytes int64 | |
| info string | |
| } | |
| // The presence of the cache value is all we need, so keep this super simple. | |
| type CacheValue struct { | |
| target string | |
| active bool | |
| } | |
| // Calculate the size (in bytes) of our struct. | |
| const cache_value_size = uint64(unsafe.Sizeof(CacheValue{})) | |
| // Determines the max cache size, in bytes. | |
| const cache_size_limit = cache_value_size * 1000 | |
| // Satisfies the Value interface. | |
| func (self *CacheValue) Size() int { | |
| return int(cache_value_size) | |
| } | |
| func geturl(num int, url string, c chan *result) { | |
| var oops = "wtf" | |
| response, err := http.Get(url) | |
| if err != nil { | |
| c <- &result{num, 0, err.Error()} | |
| return | |
| } | |
| defer response.Body.Close() | |
| file, err := os.Create(fmt.Sprintf("%d.html", num)) | |
| if err != nil { | |
| c <- &result{num, 0, oops} | |
| return | |
| } | |
| defer file.Close() | |
| bytes, err := io.Copy(file, response.Body) | |
| if err != nil { | |
| c <- &result{num, bytes, oops} | |
| return | |
| } | |
| c <- &result{num, bytes, response.Status} | |
| } | |
| func main() { | |
| c := make(chan *result, 100) | |
| cache := cache.NewLRUCache(cache_size_limit) | |
| var cache_item = &CacheValue{found: "test"} | |
| var cache_key = "wtf" | |
| cache.Set(cache_key, cache_item) | |
| cache_item2, cache_hit := cache.Get(cache_key) | |
| if cache_hit { | |
| fmt.Println(cache_item2) | |
| } | |
| req := []string{ | |
| "http://www.yahoo.com/", | |
| "http://arandf120121fadfad.pl/", | |
| "http://www.google.com/", | |
| "http://127.0.0.1/", | |
| "http://127.0.0.1/404", | |
| } | |
| for i := range req { | |
| go geturl(i, req[i], c) | |
| } | |
| for i := range req { | |
| res := <-c | |
| fmt.Printf("[%d] %s (%d bytes): %s\n", i, req[res.id], res.bytes, res.info) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment