mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
822932fe15 | |||
85c5a611e1 | |||
fe5c0b54b6 | |||
54fbe8eb0a | |||
2d851d4872 | |||
a83ccf7c00 |
@ -5,7 +5,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/charlienet/go-mixed/bytesconv"
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
"github.com/charlienet/go-mixed/expr"
|
|
||||||
"github.com/charlienet/go-mixed/hash"
|
"github.com/charlienet/go-mixed/hash"
|
||||||
"github.com/charlienet/go-mixed/redis"
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
@ -58,10 +57,7 @@ func New(expectedInsertions uint, fpp float64, opts ...option) *BloomFilter {
|
|||||||
bf := &BloomFilter{
|
bf := &BloomFilter{
|
||||||
bits: bits,
|
bits: bits,
|
||||||
funcs: k,
|
funcs: k,
|
||||||
store: expr.Ternary[bitStore](
|
store: createBitStore(opt, bits),
|
||||||
opt.redisClient == nil,
|
|
||||||
newMemStore(bits),
|
|
||||||
newRedisStore(opt.redisClient, opt.redisKey, bits)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bf
|
return bf
|
||||||
@ -104,6 +100,14 @@ func (bf *BloomFilter) Clear() {
|
|||||||
bf.store.Clear()
|
bf.store.Clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createBitStore(opt *bloomOptions, bits uint) bitStore {
|
||||||
|
if opt.redisClient != nil {
|
||||||
|
return newRedisStore(opt.redisClient, opt.redisKey, bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newMemStore(bits)
|
||||||
|
}
|
||||||
|
|
||||||
// 计算优化的位图长度,
|
// 计算优化的位图长度,
|
||||||
// n 期望放置元素数量,
|
// n 期望放置元素数量,
|
||||||
// p 预期的误判概率
|
// p 预期的误判概率
|
||||||
|
@ -2,22 +2,21 @@ package bloom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/bits-and-blooms/bitset"
|
"github.com/bits-and-blooms/bitset"
|
||||||
"github.com/charlienet/go-mixed/locker"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type memStore struct {
|
type memStore struct {
|
||||||
size uint
|
size uint
|
||||||
set *bitset.BitSet // 内存位图
|
set *bitset.BitSet // 内存位图
|
||||||
lock locker.RWLocker // 同步锁
|
lock sync.RWMutex // 同步锁
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMemStore(size uint) *memStore {
|
func newMemStore(size uint) *memStore {
|
||||||
return &memStore{
|
return &memStore{
|
||||||
size: size,
|
size: size,
|
||||||
set: bitset.New(size),
|
set: bitset.New(size),
|
||||||
lock: locker.NewRWLocker(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,13 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configure interface {
|
type NotifyFunc func(Configure) error
|
||||||
GetString(string, string) string
|
|
||||||
}
|
|
||||||
|
|
||||||
type NotifyFunc func(Configure)
|
type Configure interface {
|
||||||
|
Load(dataId string, v any, onChanged ...NotifyFunc) error
|
||||||
|
GetString(string, string) string
|
||||||
|
GetInt(key string, defaultValue int) int
|
||||||
|
}
|
||||||
|
|
||||||
type conf struct {
|
type conf struct {
|
||||||
viper *viper.Viper //
|
viper *viper.Viper //
|
||||||
|
@ -3,94 +3,111 @@ package locker
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WithLocker struct {
|
var empty = &emptyLocker{}
|
||||||
once sync.Once
|
|
||||||
mu Locker
|
type Locker struct {
|
||||||
|
once sync.Once
|
||||||
|
distributedLocker DistributedLocker // 分布式锁
|
||||||
|
mu locker
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithLocker) Synchronize() {
|
func (w *Locker) WithRedis(key string, rdb redis.Client) *Locker {
|
||||||
if w.mu == nil || w.mu == EmptyLocker {
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Locker) WithDistributedLocker(d DistributedLocker) *Locker {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Locker) Synchronize() *Locker {
|
||||||
|
if w.mu == nil || w.mu == empty {
|
||||||
w.mu = NewLocker()
|
w.mu = NewLocker()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithLocker) Lock() {
|
func (w *Locker) Lock() {
|
||||||
w.ensureLocker().Lock()
|
w.ensureLocker().mu.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithLocker) Unlock() {
|
func (w *Locker) Unlock() {
|
||||||
w.ensureLocker().Unlock()
|
w.ensureLocker().mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithLocker) TryLock() bool {
|
func (w *Locker) TryLock() bool {
|
||||||
return w.ensureLocker().TryLock()
|
return w.ensureLocker().mu.TryLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithLocker) ensureLocker() Locker {
|
func (w *Locker) ensureLocker() *Locker {
|
||||||
w.once.Do(func() {
|
w.once.Do(func() {
|
||||||
if w.mu == nil {
|
if w.mu == nil {
|
||||||
w.mu = EmptyLocker
|
w.mu = empty
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return w.mu
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
type WithSpinLocker struct {
|
type SpinLocker struct {
|
||||||
WithLocker
|
Locker
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithSpinLocker) Synchronize() {
|
func (w *SpinLocker) Synchronize() {
|
||||||
if w.mu == nil || w.mu == EmptyLocker {
|
if w.mu == nil || w.mu == empty {
|
||||||
w.mu = NewSpinLocker()
|
w.mu = NewSpinLocker()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type WithRWLocker struct {
|
type RWLocker struct {
|
||||||
once sync.Once
|
once sync.Once
|
||||||
mu RWLocker
|
mu rwLocker
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) Synchronize() {
|
func (w *RWLocker) Synchronize() *RWLocker {
|
||||||
if w.mu == nil || w.mu == EmptyLocker {
|
if w.mu == nil || w.mu == empty {
|
||||||
log.Println("初始化有效锁")
|
|
||||||
w.mu = NewRWLocker()
|
w.mu = NewRWLocker()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) Lock() {
|
func (w *RWLocker) Lock() {
|
||||||
w.ensureLocker().Lock()
|
w.ensureLocker().mu.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) TryLock() bool {
|
func (w *RWLocker) TryLock() bool {
|
||||||
return w.ensureLocker().TryLock()
|
return w.ensureLocker().mu.TryLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) Unlock() {
|
func (w *RWLocker) Unlock() {
|
||||||
w.ensureLocker().Unlock()
|
w.ensureLocker().mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) RLock() {
|
func (w *RWLocker) RLock() {
|
||||||
w.ensureLocker().RLock()
|
w.ensureLocker().mu.RLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) TryRLock() bool {
|
func (w *RWLocker) TryRLock() bool {
|
||||||
return w.ensureLocker().TryRLock()
|
return w.ensureLocker().mu.TryRLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) RUnlock() {
|
func (w *RWLocker) RUnlock() {
|
||||||
w.ensureLocker().RUnlock()
|
w.ensureLocker().mu.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WithRWLocker) ensureLocker() RWLocker {
|
func (w *RWLocker) ensureLocker() *RWLocker {
|
||||||
w.once.Do(func() {
|
w.once.Do(func() {
|
||||||
if w.mu == nil {
|
if w.mu == nil {
|
||||||
log.Println("初始化一个空锁")
|
log.Println("初始化一个空锁")
|
||||||
w.mu = EmptyLocker
|
w.mu = empty
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return w.mu
|
return w
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user