<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 并發非阻塞緩存 通過函數傳入 key 獲取去獲取信息,如果存在多個key,則只會進行執行一次函數, ``` type result struct { value interface{} err error } type entry struct { res result ready chan struct{} // closed when res is ready } type Mem struct { mu sync.Mutex cache map[string]*entry f Func } type Func func(key string) (interface{}, error) func NewMem(f Func) *Mem { return &Mem{cache: map[string]*entry{}, f: f} } func (m *Mem) Get(key string) (interface{}, error) { m.mu.Lock() e := m.cache[key] if e == nil { e = &entry{ready: make(chan struct{})} m.cache[key] = e m.mu.Unlock() e.res.value, e.res.err = m.f(key) close(e.ready) } else { m.mu.Unlock() <-e.ready } return e.res.value, e.res.err } ``` goroutine必須等待值ready之后才能讀到條目的結果。這個讀取操作在channel關閉之前一直是阻塞 測試 ```var url = []string{ "http://www.baidu.com", "http://www.bilibili.com", "http://www.baidu.com", "http://www.baidu.com", "http://www.baidu.com", } httpGetBody := func(url string) (interface{}, error) { resp, err := http.Get(url) if err != nil { return nil, err } bytes, err := ioutil.ReadAll(resp.Body) return bytes, err } mem := NewMem(httpGetBody) for _, v := range url { go func(v string) { fmt.Printf("%+v\n", v) b, e := mem.Get(v) if e != nil { log.Printf("%v", e) } fmt.Printf("%+v\n", len(b.([]byte))) }(v) } s := make(chan os.Signal) signal.Notify(s, os.Kill, os.Interrupt) <-s //output //http://www.baidu.com //http://www.baidu.com //http://www.baidu.com //http://www.bilibili.com //http://www.baidu.com //3530 //294264 //294264 //294264 //294264 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看