github.com/leslie-fei/fastcache@v0.0.0-20240520092641-b7a9eb05711f/cache_test.go (about)

     1  package fastcache
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  )
     8  
     9  func TestCache(t *testing.T) {
    10  	cc, err := NewCache(128*MB, nil)
    11  	if err != nil {
    12  		panic(err)
    13  	}
    14  
    15  	var keys = make([]string, 1024)
    16  	for i := 0; i < len(keys); i++ {
    17  		keys[i] = fmt.Sprintf("key_%d", i)
    18  	}
    19  
    20  	v := []byte("v1")
    21  	n := runtime.NumCPU()
    22  	for i := 0; i < n; i++ {
    23  		go func() {
    24  			index := 0
    25  			for {
    26  				k := keys[index%len(keys)]
    27  				if err := cc.Set(k, v); err != nil {
    28  					fmt.Println("set err: ", err)
    29  					continue
    30  				}
    31  				// if not found return ErrNotFound
    32  				_, _ = cc.Get(k)
    33  				_ = cc.Del(k)
    34  
    35  				index++
    36  			}
    37  		}()
    38  	}
    39  
    40  	select {}
    41  }