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

     1  package zcache_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/sohaha/zlsgo"
     8  	"github.com/sohaha/zlsgo/zcache"
     9  )
    10  
    11  func TestLRUCacheExpire(t *testing.T) {
    12  	tt := zlsgo.NewTest(t)
    13  	l := zcache.NewFast()
    14  
    15  	l.Set("key1", "value1")
    16  	l.Set("key3", "value3", time.Second*1)
    17  	l.Set("key5", "value5", time.Second*3)
    18  
    19  	v, ok := l.Get("key1")
    20  	_ = v
    21  	tt.EqualTrue(ok)
    22  
    23  	v, ok = l.Get("key3")
    24  	tt.EqualTrue(ok)
    25  	tt.Equal("value3", v)
    26  	v, ok = l.Get("key5")
    27  	tt.EqualTrue(ok)
    28  	tt.Equal("value5", v)
    29  
    30  	{
    31  		time.Sleep(time.Millisecond * 1500)
    32  
    33  		v, ok = l.Get("key1")
    34  		tt.EqualTrue(ok)
    35  		tt.Equal("value1", v)
    36  		v, ok = l.Get("key3")
    37  		tt.EqualTrue(!ok)
    38  		tt.Equal(nil, v)
    39  		v, ok = l.Get("key5")
    40  		tt.EqualTrue(ok)
    41  		tt.Equal("value5", v)
    42  	}
    43  
    44  	{
    45  		time.Sleep(time.Millisecond * 3500)
    46  
    47  		v, ok = l.Get("key1")
    48  		tt.Equal("value1", v)
    49  		tt.EqualTrue(ok)
    50  		v, ok = l.Get("key3")
    51  		tt.EqualTrue(!ok)
    52  		tt.Equal(nil, v)
    53  		v, ok = l.Get("key5")
    54  		tt.EqualTrue(!ok)
    55  		tt.Equal(nil, v)
    56  	}
    57  
    58  	{
    59  		l.Delete("key1")
    60  
    61  		v, ok = l.Get("key1")
    62  		tt.EqualTrue(!ok)
    63  		tt.Equal(nil, v)
    64  		v, ok = l.Get("key3")
    65  		tt.EqualTrue(!ok)
    66  		tt.Equal(nil, v)
    67  		v, ok = l.Get("key5")
    68  		tt.EqualTrue(!ok)
    69  		tt.Equal(nil, v)
    70  	}
    71  
    72  	{
    73  		l.Set("key1", "new", time.Second/2)
    74  
    75  		v, ok = l.Get("key1")
    76  		tt.EqualTrue(ok)
    77  		tt.Equal("new", v)
    78  		v, ok = l.Get("key3")
    79  		tt.EqualTrue(!ok)
    80  		tt.Equal(nil, v)
    81  		v, ok = l.Get("key5")
    82  		tt.EqualTrue(!ok)
    83  		tt.Equal(nil, v)
    84  	}
    85  
    86  	{
    87  		time.Sleep(time.Millisecond * 1500)
    88  		v, ok = l.Get("key1")
    89  		tt.EqualTrue(!ok)
    90  		tt.Equal(nil, v)
    91  	}
    92  }
    93  
    94  func TestLRUCache(t *testing.T) {
    95  	tt := zlsgo.NewTest(t)
    96  
    97  	l := zcache.NewFast(func(o *zcache.Options) {
    98  		o.Expiration = time.Second / 2
    99  		o.Bucket = 4
   100  		o.Cap = 10
   101  		o.Callback = func(kind zcache.ActionKind, key string, ptr uintptr) {
   102  			t.Log("    ", kind, key, ptr)
   103  		}
   104  	})
   105  
   106  	l.Set("key1", "value1")
   107  	l.SetBytes("key2", []byte("value2"))
   108  
   109  	v, ok := l.Get("key1")
   110  	tt.EqualTrue(ok)
   111  	tt.Equal("value1", v)
   112  	t.Log("key1", v)
   113  
   114  	v, ok = l.Get("key2")
   115  	tt.EqualTrue(ok)
   116  	tt.Equal("value2", string(v.([]byte)))
   117  	t.Log("key2", v)
   118  
   119  	vb, ok := l.GetBytes("key2")
   120  	tt.EqualTrue(ok)
   121  	tt.Equal("value2", string(vb))
   122  
   123  	v, ok = l.Get("key3")
   124  	tt.EqualTrue(!ok)
   125  	t.Log("key3", v)
   126  
   127  	{
   128  		time.Sleep(time.Second * 1)
   129  
   130  		v, ok = l.Get("key1")
   131  		tt.EqualTrue(!ok)
   132  		tt.Equal(nil, v)
   133  
   134  		v, ok = l.Get("key3")
   135  		tt.EqualTrue(!ok)
   136  		tt.Equal(nil, v)
   137  
   138  		v, ok = l.Get("key5")
   139  		tt.EqualTrue(!ok)
   140  		tt.Equal(nil, v)
   141  	}
   142  
   143  }