diff --git a/bytePool/byte_pool.go b/bytePool/byte_pool.go new file mode 100644 index 0000000..44954d6 --- /dev/null +++ b/bytePool/byte_pool.go @@ -0,0 +1,36 @@ +package bytecache + +type BytePool struct { + c chan []byte + w int + wcap int +} + +func NewBytePool(poolSize, size, cap int) *BytePool { + return &BytePool{ + c: make(chan []byte, poolSize), + w: size, + wcap: cap, + } +} + +func (bp *BytePool) Get() (b []byte) { + select { + case b = <-bp.c: + default: + if bp.wcap > 0 { + b = make([]byte, bp.w, bp.wcap) + } else { + b = make([]byte, bp.w) + } + } + + return +} + +func (bp *BytePool) Put(b []byte) { + select { + case bp.c <- b: + default: + } +} diff --git a/bytePool/byte_pool_test.go b/bytePool/byte_pool_test.go new file mode 100644 index 0000000..85fe757 --- /dev/null +++ b/bytePool/byte_pool_test.go @@ -0,0 +1,11 @@ +package bytecache + +import "testing" + +func TestByteCache(t *testing.T) { + bp := NewBytePool(512, 1024, 1024) + buffer := bp.Get() + defer bp.Put(buffer) + + t.Log(len(buffer)) +}