日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

concurrent map使用

發布時間:2024/4/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 concurrent map使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

concurrent map使用


目錄

  • 概述
  • 例子

  • 1. 概述

  • Go語言原生的map類型并不支持并發讀寫。concurrent-map提供了一種高性能的解決方案:通過對內部map進行分片,降低鎖粒度,從而達到最少的鎖等待時間(鎖沖突)
  • 在Go 1.9之前,go語言標準庫中并沒有實現并發map。在Go 1.9中,引入了sync.Map。新的sync.Map與此concurrent-map有幾個關鍵區別。
  • 標準庫中的sync.Map是專為append-only場景設計的。因此,如果想將Map用于一個類似內存數據庫,那么使用concurrent-map可能會更受益。
  • sync.Map在讀多寫少性能比較好,否則并發性能很差

  • 2. 例子

  • 引用:
  • go test "github.com/orcaman/concurrent-map"
  • 例子使用了部分方法,具體查看:concurrent_map_test.go
  • package mainimport ("encoding/json""fmt"cmap "github.com/orcaman/concurrent-map""github.com/prometheus/common/log""strconv" )type Animal struct {name string }func main() {m := cmap.New() // cmap.ConcurrentMapelephant := Animal{"elephant"}monkey := Animal{"monkey"}m.Set("elephant", elephant) // Set:添加元素m.Set("monkey", monkey)tmp, ok := m.Get("elephant") // Get:獲取元素if ok == false {log.Error("ok should be true for item stored within the map.")}elephant, ok = tmp.(Animal) // 類型斷言,轉成指定類型,key為指針時需要加*if !ok {log.Error("expecting an element, not null.")}if elephant.name != "elephant" {log.Error("item was modified.")}if m.Has("elephant") == false { // Has:是否有這個元素log.Error("element exists, expecting Has to return True.")}m.Remove("monkey") // Remove:去除元素if m.Count() != 0 {log.Error("Expecting count to be zero once item was removed.")}monkey = Animal{"monkey"}m.Set("monkey", monkey)v, exists := m.Pop("monkey") // Pop:從map中獲取這個元素并刪除if !exists {log.Error("Pop didn't find a monkey.")}m1, ok := v.(Animal)if !ok || m1 != monkey {log.Error("Pop found something else, but monkey.")}if m.Count() != 100 { // Count: 計算map中元素個數log.Error("Expecting 100 element within map.")}if m.IsEmpty() == false { // IsEmpty:判斷map是否為nillog.Error("new map should be empty")}// 插入100個元素for i := 0; i < 100; i++ {m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})}counter := 0// Iterate over elements.for item := range m.IterBuffered() { // IterBuffered:獲取緩沖迭代器,可用于for循環println(item.Val)val := item.Valname := item.Val.(Animal).namefmt.Println(name)if val == nil {log.Error("Expecting an object.")}counter++}m.Clear() // Clear:清空map// Insert 100 elements.for i := 0; i < 100; i++ {m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})}// Iterate over elements.m.IterCb(func(key string, v interface{}) { // IterCb:遍歷map,獲取key,valuenum, ok := v.(Animal)fmt.Println(key,"---",num.name)if !ok {log.Error("Expecting an animal object")}counter++})items := m.Items() // Items:轉換成map[string]interface{}if len(items) != 100 {log.Error("We should have counted 100 elements.")}m.Clear()m.Set("a", 1)m.Set("b", 2)j, err := json.Marshal(m) // 轉換成json格式if err != nil {log.Error(err)}fmt.Println(j)keys := m.Keys() // Keys:獲取map的key列表println(keys)animals := map[string]interface{}{"elephant": Animal{"elephant"},"monkey": Animal{"monkey"},}m.MSet(animals) // MSet:同時添加多個元素println(m.Count())dolphin := Animal{"dolphin"}whale := Animal{"whale"}tiger := Animal{"tiger"}lion := Animal{"lion"}cb := func(exists bool, valueInMap interface{}, newValue interface{}) interface{} {nv := newValue.(Animal)if !exists {return []Animal{nv}}res := valueInMap.([]Animal)return append(res, nv)}m.Set("marine", []Animal{dolphin})m.Upsert("marine", whale, cb) // Upsert:插入或更新元素m.Upsert("predator", tiger, cb)m.Upsert("predator", lion, cb) }

    總結

    以上是生活随笔為你收集整理的concurrent map使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。