欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Golang實現(xiàn)EasyCache緩存庫實例探究

 更新時間:2024年01月24日 10:59:26   作者:紹納 nullbody筆記  
這篇文章主要為大家介紹了Golang實現(xiàn)EasyCache緩存庫實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

引言

學了不吃虧,學了不上當,進廠打釘必備基本功,看完絕對有很爽的感覺。核心代碼也就300多行,代碼雖少但是功能一點不打折

通過本項目學到什么?

  • Golang基礎語法

  • 緩存數(shù)據(jù)結構

  • 鎖的使用(并發(fā)安全 & 分片減小鎖粒度)

  • LRU(緩存淘汰算法)

  • key過期刪除策略(定時刪除)

  • 測試用例的編寫

代碼原理

New函數(shù)

負責創(chuàng)建 *EasyCache對象,對象的底層包含 conf.Shards個分片,目的在于減少鎖沖突

func New(conf Config) (*EasyCache, error) {

	if !utils.IsPowerOfTwo(conf.Shards) {
		returnnil, errors.New("shards number must be power of two")
	}

	if conf.Cap <= 0 {
		conf.Cap = defaultCap
	}
	// init cache object
	cache := &EasyCache{
		shards:    make([]*cacheShard, conf.Shards),
		conf:      conf,
		hash:      conf.Hasher,
		shardMask: uint64(conf.Shards - 1), // mask
		close:     make(chanstruct{}),
	}

	var onRemove OnRemoveCallback
	if conf.OnRemoveWithReason != nil {
		onRemove = conf.OnRemoveWithReason
	} else {
		onRemove = cache.notProvidedOnRemove
	}

	// init shard
	for i := 0; i < conf.Shards; i++ {
		cache.shards[i] = newCacheShard(conf, i, onRemove, cache.close)
	}
	return cache, nil
}

newCacheShard函數(shù)

用來初始化實際存放 k/v的數(shù)據(jù)結構*cacheShard(也就是單個分片)。分片底層的存儲采用兩個map和一個list:

  • items負責保存所有的k/v(過期or不過期都有存)

  • expireItems負責保存有過期時間的k/v,目的在于減少掃描key`的數(shù)據(jù)量

  • list用作LRU記錄最近最少使用key的順序。LRU代碼實現(xiàn)看這篇文章 Leetcode LRU題解,有助于理解本項目中的LRU的細節(jié)。

func newCacheShard(conf Config, id int, onRemove OnRemoveCallback, close chan struct{}) *cacheShard {
	shard := &cacheShard{
		items:           make(map[string]*list.Element),
		expireItems:     make(map[string]*list.Element),
		cap:             conf.Cap,
		list:            list.New(),
		logger:          newLogger(conf.Logger),
		cleanupInterval: defaultInternal,
		cleanupTicker:   time.NewTicker(defaultInternal),
		addChan:         make(chanstring),
		isVerbose:       conf.Verbose,
		id:              id,
		onRemove:        onRemove,
		close:           close,
	}
	// goroutine clean expired key
	go shard.expireCleanup()
	return shard
}

expireCleanup

負責對本分片中過期的key進行定期刪除:代碼理解的關鍵在于不同的key會有不同的過期時間,例如key=a 過期時間3s,key=b 過期時間5s。

  • 定時器定時執(zhí)行間隔不能太長,例如10s,a/b都已經(jīng)過期了還不清理,太不及時

  • 定時器定時執(zhí)行間隔不能太短,例如1s,執(zhí)行頻率又太高了,a/b都未過期,空轉

  • 過期間隔肯定是動態(tài)變化的,一開始為3s間隔,執(zhí)行后清理掉a,此時b還剩(5-3)=2s的存活時間,所以間隔再設定為2s。再執(zhí)行完以后,沒有數(shù)據(jù)了,那間隔就在設定一個大值smallestInternal = defaultInternal處于休眠狀態(tài)

這里再思考一種情況,按照上述解釋一開始間隔設定3s,等到過期了就可以將a清理掉。那如果用戶這時又設定了key=c 過期時間1s,那如果定時器按照3s執(zhí)行又變成了間隔太長了。所以我們需要發(fā)送信號cs.addChan:,重新設定過期間隔

/*
1.當定時器到期,執(zhí)行過期清理
2.當新增的key有過期時間,通過addChan觸發(fā)執(zhí)行
*/
func (cs *cacheShard) expireCleanup() {
	for {
		select {
		case <-cs.cleanupTicker.C:
		case <-cs.addChan: // 立即觸發(fā)
		case <-cs.close: // stop goroutine
			if cs.isVerbose {
				cs.logger.Printf("[shard %d] flush..", cs.id)
			}
			cs.flush() // free
			return
		}
		cs.cleanupTicker.Stop()
		// 記錄下一次定時器的最小間隔(目的:key過期了,盡快刪除)
		smallestInternal := 0 * time.Second
		now := time.Now()
		cs.lock.Lock()
		for key, ele := range cs.expireItems { // 遍歷過期key
			item := ele.Value.(*cacheItem)
			if item.LifeSpan() == 0 { // 沒有過期時間
				cs.logger.Printf("warning wrong data\n")
				continue
			}
			if now.Sub(item.CreatedOn()) >= item.LifeSpan() { // 過期
				// del
				delete(cs.items, key)
				delete(cs.expireItems, key)
				cs.list.Remove(ele)
				cs.onRemove(key, item.Value(), Expired)
				if cs.isVerbose {
					cs.logger.Printf("[shard %d]: expire del key <%s>  createdOn:%v,  lifeSpan:%d ms \n", cs.id, key, item.CreatedOn(), item.LifeSpan().Milliseconds())
				}
			} else {
				d := item.LifeSpan() - now.Sub(item.CreatedOn())
				if smallestInternal == 0 || d < smallestInternal {
					smallestInternal = d
				}
			}
		}
		if smallestInternal == 0 {
			smallestInternal = defaultInternal
		}
		cs.cleanupInterval = smallestInternal
		cs.cleanupTicker.Reset(cs.cleanupInterval)
		cs.lock.Unlock()
	}
}

set 函數(shù)理解

關鍵在于,用戶可以對同一個key重復設定:

cache.Set(key, 0, 5*time.Second) // expire 5s
cache.Set(key, 0, 0*time.Second) // expire 0s

第一次設定為5s過期,立刻又修改為0s不過期,所以在代碼中需要判斷key是否之前已經(jīng)存在,

  • 如果存在重復&有過期時間,需要從過期expireItems中剔除

  • 如果不存在直接新增即可(前提:容量還有剩余)

LRU的基本規(guī)則

  • 最新數(shù)據(jù)放到list的Front

  • 如果超過最大容量,從list的Back刪除元素

func (cs *cacheShard) set(key string, value interface{}, lifeSpan time.Duration) error {

	cs.lock.Lock()
	defer cs.lock.Unlock()

	oldEle, ok := cs.items[key]
	if ok { // old item
		oldItem := oldEle.Value.(*cacheItem)
		oldLifeSpan := oldItem.LifeSpan()

		// modify
		oldEle.Value = newCacheItem(key, value, lifeSpan)
		cs.list.MoveToFront(oldEle)

		if oldLifeSpan &gt; 0 &amp;&amp; lifeSpan == 0 { // 原來的有過期時間,新的沒有過期時間
			delete(cs.expireItems, key)
		}

		if oldLifeSpan == 0 &amp;&amp; lifeSpan &gt; 0 { // 原有的無過期時間,當前有過期時間
			cs.expireItems[key] = oldEle
			if lifeSpan &lt; cs.cleanupInterval {
				gofunc() {
					cs.addChan &lt;- key
				}()
			}
		}

	} else { // new item

		iflen(cs.items) &gt;= int(cs.cap) { // lru: No space
			delVal := cs.list.Remove(cs.list.Back())
			item := delVal.(*cacheItem)
			delete(cs.items, item.Key())
			if item.LifeSpan() &gt; 0 {
				delete(cs.expireItems, item.Key())
			}
			cs.onRemove(key, item.Value(), NoSpace)

			if cs.isVerbose {
				cs.logger.Printf("[shard %d] no space del key &lt;%s&gt;\n", cs.id, item.Key())
			}
		}
		// add
		ele := cs.list.PushFront(newCacheItem(key, value, lifeSpan))
		cs.items[key] = ele
		if lifeSpan &gt; 0 {
			cs.expireItems[key] = ele
			if lifeSpan &lt; cs.cleanupInterval {
				gofunc() {
					cs.addChan &lt;- key
				}()
			}
		}
	}

	if cs.isVerbose {
		if lifeSpan == 0 {
			cs.logger.Printf("[shard %d]: set persist key &lt;%s&gt;\n", cs.id, key)
		} else {
			cs.logger.Printf("[shard %d]: set expired key &lt;%s&gt;", cs.id, key)
		}
	}
	returnnil
}

以上就是Golang實現(xiàn)EasyCache緩存庫實例探究的詳細內(nèi)容,更多關于Golang EasyCache緩存庫的資料請關注腳本之家其它相關文章!

相關文章

  • 初探GO中unsafe包的使用

    初探GO中unsafe包的使用

    unsafe是Go語言標準庫中的一個包,提供了一些不安全的編程操作,本文將深入探討Go語言中的unsafe包,介紹它的使用方法和注意事項,感興趣的可以了解下
    2023-08-08
  • goland設置顏色和字體的操作

    goland設置顏色和字體的操作

    這篇文章主要介紹了goland設置顏色和字體的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • Golang報“import cycle not allowed”錯誤的2種解決方法

    Golang報“import cycle not allowed”錯誤的2種解決方法

    這篇文章主要給大家介紹了關于Golang報"import cycle not allowed"錯誤的2種解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以們下面隨著小編來一起看看吧
    2018-08-08
  • GoFrame基于性能測試得知grpool使用場景

    GoFrame基于性能測試得知grpool使用場景

    這篇文章主要為大家介紹了GoFrame基于性能測試得知grpool使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • go-zero源碼閱讀之布隆過濾器實現(xiàn)代碼

    go-zero源碼閱讀之布隆過濾器實現(xiàn)代碼

    布隆過濾器可以用于檢索一個元素是否在一個集合中。它的優(yōu)點是空間效率和查詢時間都比一般的算法要好的多,缺點是有一定的誤識別率和刪除困難,這篇文章主要介紹了go-zero源碼閱讀-布隆過濾器,需要的朋友可以參考下
    2023-02-02
  • go語言中json數(shù)據(jù)的讀取和寫出操作

    go語言中json數(shù)據(jù)的讀取和寫出操作

    這篇文章主要介紹了go語言中json數(shù)據(jù)的讀取和寫出操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Golang中實現(xiàn)數(shù)據(jù)脫敏處理的go-mask包分享

    Golang中實現(xiàn)數(shù)據(jù)脫敏處理的go-mask包分享

    這篇文章主要是來和大家分享一個在輸出中對敏感數(shù)據(jù)進行脫敏的工作包:go-mask,可以將敏感信息輸出的時候替換成星號或其他字符,感興趣的小編可以跟隨小編一起了解下
    2023-05-05
  • Go類型安全的HTTP請求示例詳解

    Go類型安全的HTTP請求示例詳解

    這篇文章主要為大家介紹了Go類型安全的HTTP請求示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • golang調(diào)用shell命令(實時輸出,終止)

    golang調(diào)用shell命令(實時輸出,終止)

    本文主要介紹了golang調(diào)用shell命令(實時輸出,終止),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Golang異常控制處理程序錯誤流程

    Golang異常控制處理程序錯誤流程

    這篇文章主要介紹了Golang異??刂铺幚沓绦蝈e誤流程,Golang異常處理機制包括錯誤處理、panic和defer,可控制程序錯誤流程,保證程序穩(wěn)定性和安全性,是Golang編程的關鍵方式
    2023-04-04

最新評論