mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
字符串解析缓存
This commit is contained in:
46
compiled_buffer/compiled_buffer.go
Normal file
46
compiled_buffer/compiled_buffer.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package compiledbuffer
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type compiledbuffer[T any] struct {
|
||||||
|
buf map[string]T
|
||||||
|
compileFunc func(string) (T, error)
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCompiledBuffer[T any](fn func(string) (T, error)) *compiledbuffer[T] {
|
||||||
|
return &compiledbuffer[T]{
|
||||||
|
buf: make(map[string]T),
|
||||||
|
compileFunc: fn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *compiledbuffer[T]) Put(s string) (T, error) {
|
||||||
|
p, err := x.compileFunc(s)
|
||||||
|
if err != nil {
|
||||||
|
return p, err
|
||||||
|
}
|
||||||
|
|
||||||
|
x.mu.Lock()
|
||||||
|
x.buf[s] = p
|
||||||
|
x.mu.Unlock()
|
||||||
|
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *compiledbuffer[T]) Get(s string) (T, error) {
|
||||||
|
x.mu.RLock()
|
||||||
|
if p, ok := x.buf[s]; ok {
|
||||||
|
x.mu.RUnlock()
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
x.mu.RUnlock()
|
||||||
|
|
||||||
|
return x.Put(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *compiledbuffer[T]) Clear() {
|
||||||
|
x.mu.Lock()
|
||||||
|
x.buf = make(map[string]T)
|
||||||
|
x.mu.Unlock()
|
||||||
|
}
|
49
compiled_buffer/compiled_buffer_test.go
Normal file
49
compiled_buffer/compiled_buffer_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package compiledbuffer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var s = "^aaa^"
|
||||||
|
|
||||||
|
func TestPutGet(t *testing.T) {
|
||||||
|
b := NewCompiledBuffer(func(s string) (*regexp.Regexp, error) { return regexp.Compile(s) })
|
||||||
|
|
||||||
|
t.Log(b.Put(s))
|
||||||
|
r, _ := b.Get(s)
|
||||||
|
|
||||||
|
t.Log(r.Match([]byte("abc")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGet(b *testing.B) {
|
||||||
|
b.Run("buf", func(b *testing.B) {
|
||||||
|
buf := NewCompiledBuffer(func(s string) (*regexp.Regexp, error) { return regexp.Compile(s) })
|
||||||
|
buf.Put(s)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
buf.Get(s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("buf", func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
regexp.Compile(s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkConcurrence(b *testing.B) {
|
||||||
|
buf := NewCompiledBuffer(func(s string) (*regexp.Regexp, error) { return regexp.Compile(s) })
|
||||||
|
var i int64
|
||||||
|
|
||||||
|
b.RunParallel(func(p *testing.PB) {
|
||||||
|
gid := int(atomic.AddInt64(&i, 1) - 1)
|
||||||
|
|
||||||
|
for i := 0; p.Next(); i++ {
|
||||||
|
buf.Get(strconv.Itoa(gid))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user