github.com/charlienet/go-mixed@v0.3.7/cache/cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"sync/atomic"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/charlienet/go-mixed/bytesconv"
    11  	"github.com/charlienet/go-mixed/logx"
    12  )
    13  
    14  var (
    15  	defaultKey = "u-000"
    16  )
    17  
    18  func TestNewCache(t *testing.T) {
    19  	c, err := NewCacheBuilder().
    20  		WithRedis(RedisConfig{
    21  			Addrs:    []string{"192.168.2.222:6379"},
    22  			Password: "123456",
    23  		}).
    24  		WithPrefix("cache_test").
    25  		WithLogger(logx.NewLogrus()).
    26  		Build()
    27  
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	c.Set("abc", "value", time.Minute*10)
    33  
    34  	var s string
    35  	c.Get("abc", &s)
    36  
    37  	t.Log(s)
    38  }
    39  
    40  type SimpleUser struct {
    41  	FirstName string
    42  	LastName  string
    43  }
    44  
    45  func TestMemCache(t *testing.T) {
    46  	b, _ := NewBigCache(BigCacheConfig{})
    47  	var mems = []MemCache{
    48  		NewFreeCache(10 * 1024 * 1024),
    49  		b,
    50  	}
    51  
    52  	u := SimpleUser{FirstName: "Radomir", LastName: "Sohlich"}
    53  	encoded, _ := bytesconv.Encode(u)
    54  	for _, m := range mems {
    55  		m.Set(defaultKey, encoded, time.Second)
    56  		ret, err := m.Get(defaultKey)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  
    61  		var u2 SimpleUser
    62  		bytesconv.Decode(ret, &u2)
    63  		t.Log(u2)
    64  	}
    65  }
    66  
    67  func TestDistributedCache(t *testing.T) {
    68  	c := NewRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456", Prefix: "abcdef"})
    69  
    70  	if err := c.Ping(); err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	t.Log(c.Exist(defaultKey))
    75  
    76  	u := SimpleUser{FirstName: "redis client"}
    77  
    78  	var u2 SimpleUser
    79  	c.Get(defaultKey, &u2)
    80  
    81  	c.Set(defaultKey, u, time.Minute*10)
    82  	t.Log(c.Exist(defaultKey))
    83  
    84  	if err := c.Get(defaultKey, &u2); err != nil {
    85  		t.Fatal("err:", err)
    86  	}
    87  	t.Logf("%+v", u2)
    88  
    89  	// c.Delete(defaultKey)
    90  }
    91  
    92  func TestGetFn(t *testing.T) {
    93  	c := buildCache()
    94  	var u2 SimpleUser
    95  
    96  	c.GetFn(context.Background(), defaultKey, &u2, func(ctx context.Context) (out any, err error) {
    97  		v := &u2
    98  		v.FirstName = "abc"
    99  		v.LastName = "aaaa"
   100  
   101  		return nil, nil
   102  	}, time.Minute*1)
   103  
   104  	t.Logf("%+v", u2)
   105  }
   106  
   107  func TestGetFromSource(t *testing.T) {
   108  	var count int32
   109  
   110  	n := 10
   111  	c := &Cache{}
   112  	wg := &sync.WaitGroup{}
   113  	wg.Add(n)
   114  	for i := 0; i < n; i++ {
   115  		go func() {
   116  			c.getFromSource(context.Background(), defaultKey, func(ctx context.Context) (any, error) {
   117  				atomic.AddInt32(&count, 1)
   118  				time.Sleep(time.Second)
   119  
   120  				return "abc", nil
   121  			})
   122  
   123  			wg.Done()
   124  		}()
   125  	}
   126  
   127  	wg.Wait()
   128  	t.Log("count:", count)
   129  }
   130  
   131  func BenchmarkMemCache(b *testing.B) {
   132  }
   133  
   134  func load() (any, error) {
   135  	return nil, nil
   136  }
   137  
   138  func buildCache() *Cache {
   139  	c, err := NewCacheBuilder().
   140  		WithFreeCache(10 * 1024 * 1024).
   141  		WithRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456"}).
   142  		Build()
   143  
   144  	if err != nil {
   145  		panic(err)
   146  	}
   147  
   148  	return c
   149  }