分享
  1. 首页
  2. 文章

防止缓存击穿工具 singlefight 解读

Hello · · 1179 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

缓存击穿

缓存击穿是指当缓存中的一个key过期时,对此数据的大量并发请求在缓存未重新生成时,会请求到数据库上。导致数据库的崩溃。

解决方案

大量并发请求的都是一个数据,只需要一个请求到达数据库然后设置缓存即可。所以需要控制其他请求等缓存生成之后再继续进行。

singleflight

singleflight是groupCache中的一个工具。它的作用是:

Package singleflight provides a duplicate function call suppression mechanism.

翻译过来意思是:包singleflight提供了一个重复的函数调用抑制机制。
即singleflight可以让多次相同的函数调用实际上只执行一次,把第一次的执行结果直接赋值给其他调用。

代码

singleflight代码一共只有一百多行

// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package singleflight provides a duplicate function call suppression
// mechanism.
package singleflight // import "golang.org/x/sync/singleflight"
import "sync"
// call is an in-flight or completed singleflight.Do call
type call struct {
 wg sync.WaitGroup
 // These fields are written once before the WaitGroup is done
 // and are only read after the WaitGroup is done.
 val interface{}
 err error
 // forgotten indicates whether Forget was called with this call's key
 // while the call was still in flight.
 forgotten bool
 // These fields are read and written with the singleflight
 // mutex held before the WaitGroup is done, and are read but
 // not written after the WaitGroup is done.
 dups int
 chans []chan<- Result
}
// Group represents a class of work and forms a namespace in
// which units of work can be executed with duplicate suppression.
type Group struct {
 mu sync.Mutex // protects m
 m map[string]*call // lazily initialized
}
// Result holds the results of Do, so they can be passed
// on a channel.
type Result struct {
 Val interface{}
 Err error
 Shared bool
}
// Do executes and returns the results of the given function, making
// sure that only one execution is in-flight for a given key at a
// time. If a duplicate comes in, the duplicate caller waits for the
// original to complete and receives the same results.
// The return value shared indicates whether v was given to multiple callers.
func (g *Group) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) {
 g.mu.Lock()
 if g.m == nil {
 g.m = make(map[string]*call)
 }
 if c, ok := g.m[key]; ok {
 c.dups++
 g.mu.Unlock()
 c.wg.Wait()
 return c.val, c.err, true
 }
 c := new(call)
 c.wg.Add(1)
 g.m[key] = c
 g.mu.Unlock()
 g.doCall(c, key, fn)
 return c.val, c.err, c.dups > 0
}
// DoChan is like Do but returns a channel that will receive the
// results when they are ready.
func (g *Group) DoChan(key string, fn func() (interface{}, error)) <-chan Result {
 ch := make(chan Result, 1)
 g.mu.Lock()
 if g.m == nil {
 g.m = make(map[string]*call)
 }
 if c, ok := g.m[key]; ok {
 c.dups++
 c.chans = append(c.chans, ch)
 g.mu.Unlock()
 return ch
 }
 c := &call{chans: []chan<- Result{ch}}
 c.wg.Add(1)
 g.m[key] = c
 g.mu.Unlock()
 go g.doCall(c, key, fn)
 return ch
}
// doCall handles the single call for a key.
func (g *Group) doCall(c *call, key string, fn func() (interface{}, error)) {
 c.val, c.err = fn()
 c.wg.Done()
 g.mu.Lock()
 if !c.forgotten {
 delete(g.m, key)
 }
 for _, ch := range c.chans {
 ch <- Result{c.val, c.err, c.dups > 0}
 }
 g.mu.Unlock()
}
// Forget tells the singleflight to forget about a key. Future calls
// to Do for this key will call the function rather than waiting for
// an earlier call to complete.
func (g *Group) Forget(key string) {
 g.mu.Lock()
 if c, ok := g.m[key]; ok {
 c.forgotten = true
 }
 delete(g.m, key)
 g.mu.Unlock()
}

思路就是每次使用singleflight进行函数调用,就会查看Group里有没有已注册的相同调用。如果有则等待调用完成,并返回其结果。如果没有,则注册一个,并且执行函数。执行完成后,将注册的调用删掉。


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:Hello

查看原文:防止缓存击穿工具 singlefight 解读

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
1179 次点击
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏