kafka第三方库sarama-cluster调整为johng-cn/sarama-cluster

This commit is contained in:
john
2018-09-19 15:08:33 +08:00
parent 0669d6c4b3
commit 76e06bedcd
7 changed files with 214 additions and 48 deletions

View File

@ -69,33 +69,59 @@ func (this *StringInterfaceMap) Get(key string) interface{} {
return val
}
// 获取键值,如果键值不存在则写入默认值
func (this *StringInterfaceMap) GetWithDefault(key string, value interface{}) interface{} {
this.mu.Lock()
val, ok := this.m[key]
if !ok {
this.m[key] = value
val = value
// 设置kv缓存键值对内部会对键名的存在性使用写锁进行二次检索确认如果存在则不再写入返回键名对应的键值。
// 在高并发下有用,防止数据写入的并发逻辑错误。
func (this *StringInterfaceMap) doSetWithLockCheck(key string, value interface{}) interface{} {
this.mu.Lock()
if v, ok := this.m[key]; ok {
this.mu.Unlock()
return v
}
this.mu.Unlock()
return val
if f, ok := value.(func() interface {}); ok {
value = f()
}
this.m[key] = value
this.mu.Unlock()
return value
}
// 当键名存在时返回其键值,否则写入指定的键值
func (this *StringInterfaceMap) GetOrSet(key string, value interface{}) interface{} {
if v := this.Get(key); v == nil {
return this.doSetWithLockCheck(key, value)
} else {
return v
}
}
// 当键名存在时返回其键值,否则写入指定的键值,键值由指定的函数生成
func (this *StringInterfaceMap) GetOrSetFunc(key string, f func() interface{}) interface{} {
if v := this.Get(key); v == nil {
value := f()
this.mu.Lock()
defer this.mu.Unlock()
// 写锁二次检索确认
if v, ok := this.m[key]; !ok {
this.m[key] = value
return value
} else {
return v
}
} else {
return v
}
if v := this.Get(key); v == nil {
v = f()
this.doSetWithLockCheck(key, v)
return v
} else {
return v
}
}
// 与GetOrSetFunc不同的是f是在写锁机制内执行
func (this *StringInterfaceMap) GetOrSetFuncLock(key string, f func() interface{}) interface{} {
if v := this.Get(key); v == nil {
this.doSetWithLockCheck(key, f)
return v
} else {
return v
}
}
// 当键名不存在时写入并返回true否则返回false。
func (this *StringInterfaceMap) SetIfNotExist(key string, value interface{}) bool {
if !this.Contains(key) {
this.doSetWithLockCheck(key, value)
return true
}
return false
}
// 删除键值对