github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zcache/cache_test.go (about)

     1  package zcache_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/sohaha/zlsgo"
    11  	"github.com/sohaha/zlsgo/zcache"
    12  	"github.com/sohaha/zlsgo/zstring"
    13  )
    14  
    15  func TestCache(tt *testing.T) {
    16  	t := zlsgo.NewTest(tt)
    17  	// 初始一个名为 demo 的缓存对象
    18  	c := zcache.New(zstring.Rand(7))
    19  
    20  	data := 666
    21  	key := "name"
    22  
    23  	// 设置缓存key为name,值为666,过期时间为10秒
    24  	// 等同 c.SetRaw(key, Raw, 10*time.Second)
    25  	c.Set(key, data, 10)
    26  
    27  	t.EqualExit(1, c.Count())
    28  
    29  	// 或取缓存数据
    30  	name, err := c.Get(key)
    31  	if err != nil {
    32  		t.Fatal("cache name err: ", err)
    33  	}
    34  
    35  	// 判断缓存 key 是否存在
    36  	t.EqualExit(true, c.Exists(key))
    37  
    38  	// 如果缓存不存在则添加反之不生效
    39  	t.EqualExit(false, c.Add("name", 999, 5*time.Second))
    40  	t.EqualExit(data+10, name.(int)+10)
    41  	c.SetAddCallback(func(item *zcache.Item) {
    42  		t.Log("SetAddCallback", item.Data())
    43  	})
    44  	c.Add("name", 999, 5*time.Second)
    45  	// 删除缓存
    46  	_, _ = c.Delete(key)
    47  	t.EqualExit(false, c.Exists(key))
    48  
    49  	_, err = c.Delete(key)
    50  	t.Equal(zcache.ErrKeyNotFound, err)
    51  
    52  	t.EqualExit(true, c.Add("name", 999, 5*time.Second))
    53  
    54  	c.SetLoadNotCallback(func(key string, args ...interface{}) *zcache.Item {
    55  		return c.Set(key, "88", 10)
    56  	})
    57  
    58  	hoho, _ := c.Get("key2")
    59  	t.EqualExit("88", hoho.(string))
    60  	tt.Log(c.MostAccessed(1))
    61  }
    62  
    63  func TestDefCache(tt *testing.T) {
    64  	t := zlsgo.NewTest(tt)
    65  
    66  	key := "test_cache_def_key"
    67  	key2 := "test_cache_def_key_2"
    68  	key3 := "test_cache_def_key_3"
    69  
    70  	tt.Log("TestDefCache")
    71  	zcache.SetDeleteCallback(func(key string) bool {
    72  		fmt.Println("删除", key)
    73  		return true
    74  	})
    75  
    76  	data := "cache_def_data"
    77  	tt.Log(data)
    78  	zcache.Set(key, data, 1)
    79  	zcache.Set(key2, data, 1)
    80  
    81  	a, e := zcache.Get(key)
    82  	tt.Log(a, e)
    83  
    84  	ar, err := zcache.GetT(key)
    85  	t.EqualExit(nil, err)
    86  	tt.Log(ar.AccessCount())
    87  	tt.Log(ar.RemainingLife())
    88  	ar.AccessedTime()
    89  	ar.LifeSpanUint()
    90  	ar.LifeSpan()
    91  	ar.RemainingLife()
    92  	ar.Data()
    93  	ar.Key()
    94  	ar.CreatedTime()
    95  
    96  	ar.SetDeleteCallback(func(key string) bool {
    97  		tt.Log("拦截不删除", key)
    98  		return false
    99  	})
   100  
   101  	a, e = zcache.Get(key)
   102  	tt.Log(key, a, e)
   103  
   104  	time.Sleep(time.Millisecond * 1100)
   105  
   106  	a, e = zcache.Get(key)
   107  	tt.Log(key, a, e)
   108  
   109  	a, e = zcache.Get(key2)
   110  	tt.Log(key2, a, e)
   111  
   112  	a, e = zcache.Get(key3)
   113  	tt.Log(key3, a, e)
   114  
   115  	time.Sleep(time.Millisecond * 1100)
   116  
   117  	a, e = zcache.Get(key)
   118  	tt.Log(key, a, e)
   119  
   120  	zcache.Set(key2, data, 1)
   121  	a, e = zcache.Get(key2)
   122  	tt.Log(key2, a, e)
   123  
   124  	zcache.Set(key3, data, 1, true)
   125  	a, e = zcache.Get(key3)
   126  	tt.Log(key3, a, e)
   127  
   128  	time.Sleep(time.Millisecond * 900)
   129  	a, e = zcache.Get(key)
   130  	tt.Log(key, a, e)
   131  
   132  	a, e = zcache.Get(key2)
   133  	tt.Log(key2, a, e)
   134  
   135  	a, e = zcache.Get(key3)
   136  	tt.Log(key3, a, e)
   137  
   138  	time.Sleep(time.Millisecond * 900)
   139  	a, e = zcache.Get(key)
   140  	tt.Log(key, a, e)
   141  
   142  	_, e = zcache.Get(key2)
   143  	t.EqualExit(true, e != nil)
   144  
   145  	a, e = zcache.Get(key3)
   146  	t.EqualExit(data, a)
   147  	t.EqualExit(nil, e)
   148  
   149  	time.Sleep(time.Millisecond * 900)
   150  
   151  	a, e = zcache.Get(key)
   152  	tt.Log(key, a, e)
   153  
   154  	_, e = zcache.Get(key2)
   155  	t.EqualExit(true, e != nil)
   156  
   157  	_, e = zcache.Get(key3)
   158  	t.EqualExit(nil, e)
   159  	_, _ = zcache.Delete(key3)
   160  	a, e = zcache.Get(key3)
   161  	tt.Log(key3, a, e)
   162  	t.EqualExit(nil, a)
   163  	t.EqualExit(true, e != nil)
   164  
   165  	a, e = zcache.Get(key)
   166  	tt.Log(key, a, e)
   167  	t.EqualExit(data, a)
   168  	t.EqualExit(nil, e)
   169  
   170  	zcache.Clear()
   171  
   172  	a, e = zcache.Get(key)
   173  	tt.Log(key, a, e)
   174  	t.EqualExit(nil, a)
   175  	t.EqualExit(true, e != nil)
   176  }
   177  
   178  func TestCacheForEach(tt *testing.T) {
   179  	t := zlsgo.NewTest(tt)
   180  	c := zcache.New("CacheForEach")
   181  
   182  	data := 666
   183  
   184  	c.Set("name1", data, 1)
   185  	c.Set("name2", "name--2", 1)
   186  
   187  	tt.Log("ForEach:")
   188  	c.ForEach(func(key string, value interface{}) bool {
   189  		data, _ := c.GetT(key)
   190  		tt.Log("ForEach", key)
   191  		tt.Log(data.Key(), data.Data(), data.LifeSpan())
   192  		return true
   193  	})
   194  
   195  	i := 0
   196  	c.ForEach(func(key string, value interface{}) bool {
   197  		i++
   198  		return false
   199  	})
   200  
   201  	t.Equal(1, i)
   202  	time.Sleep(time.Millisecond * 1100)
   203  	t.EqualExit(0, c.Count())
   204  }
   205  
   206  func TestOther(t *testing.T) {
   207  	tt := zlsgo.NewTest(t)
   208  
   209  	zcache.Set("TestOther", "123", 1)
   210  	s, err := zcache.GetString("TestOther")
   211  	tt.EqualNil(err)
   212  	tt.Equal("123", s)
   213  
   214  	zcache.Set("TestOther", 123, 1)
   215  	i, err := zcache.GetInt("TestOther")
   216  	tt.EqualNil(err)
   217  	tt.Equal(123, i)
   218  }
   219  
   220  func TestAccessCount(t *testing.T) {
   221  	tt := zlsgo.NewTest(t)
   222  	cache := zcache.New("AccessCount", true)
   223  
   224  	cache.SetRaw("TestOther", 123, 100*time.Millisecond, true)
   225  	i, err := cache.GetInt("TestOther")
   226  	tt.EqualNil(err)
   227  	tt.Equal(123, i)
   228  	time.Sleep(90 * time.Millisecond)
   229  	i, err = cache.GetInt("TestOther")
   230  	tt.EqualNil(err)
   231  	tt.Equal(123, i)
   232  	time.Sleep(90 * time.Millisecond)
   233  	i, err = cache.GetInt("TestOther")
   234  	tt.EqualNil(err)
   235  	tt.Equal(123, i)
   236  	time.Sleep(time.Second * 1)
   237  	i, err = cache.GetInt("TestOther")
   238  	t.Log(i, err)
   239  }
   240  
   241  // func TestExportJSON(t *testing.T) {
   242  // 	cache := zcache.New("ExportJSON")
   243  // 	cache.Set("tmp1", &testSt{Name: "isName", Key: 100}, 1, true)
   244  // 	cache.Set("tmp2", 666, 2)
   245  // 	cache.Set("tmp3", "is string", 2)
   246  // 	jsonData := cache.ExportJSON()
   247  // 	t.Log(jsonData)
   248  // }
   249  
   250  func TestDo(t *testing.T) {
   251  	var g sync.WaitGroup
   252  	for i := 1; i <= 10; i++ {
   253  		g.Add(1)
   254  		go func(ii int) {
   255  			if ii > 8 {
   256  				time.Sleep(time.Duration(210*(ii-8)) * time.Millisecond)
   257  			}
   258  			v, o := zcache.MustGet("do", func(set func(data interface{},
   259  				lifeSpan time.Duration, interval ...bool)) (err error) {
   260  				if ii < 9 {
   261  					set(ii, 200*time.Millisecond)
   262  					return nil
   263  				} else if ii == 9 {
   264  					set("ok", 200*time.Millisecond)
   265  					return nil
   266  				}
   267  				return errors.New("不设置")
   268  			})
   269  			t.Log(ii, o, v)
   270  			g.Done()
   271  		}(i)
   272  	}
   273  	g.Wait()
   274  }