github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/cache/operation_test.go (about) 1 package cache 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 "time" 8 ) 9 10 type Value struct { 11 Name string 12 Age int 13 } 14 15 func Test_cache_Get(t *testing.T) { 16 c := New() 17 v := Value{"库陈胜", 2022} 18 _ = c.Set("test", v) 19 _ = c.Set("test1", "啊哈") 20 get, err := c.Get("test") 21 t.Logf("result %v,%v", get, err) 22 23 c.Remove("test") 24 get, err = c.Get("test") 25 t.Logf("result %v,%v", get, err) 26 } 27 28 func Test_cache_Get1(t *testing.T) { 29 c := NewWithExpiration(1 * time.Second) 30 _ = c.Set("testV", "库陈胜") 31 time.Sleep(1 * time.Second) 32 get, b := c.Get("testV") 33 if b { 34 t.Error("expire fail get is ", get) 35 } 36 _ = c.SetHash("Key", "subKey", "库陈胜") 37 _ = c.SetHash("Key", "6不liuKey@", "贼溜") 38 39 if v1, b := c.Get("Key"); !b { 40 t.Error("未获取到hash值") 41 } else { 42 t.Logf("获取到hash值 %v", v1) 43 } 44 time.Sleep(1 * time.Second) 45 46 if v2, b := c.GetHash("Key", "subKey"); !b { 47 t.Error("未获取到hash值 v2") 48 } else { 49 t.Logf("获取到hash 及子值 %v", v2) 50 } 51 time.Sleep(2 * time.Second) 52 53 if _, b = c.Get("Key"); b { 54 t.Error("Key 未过期") 55 } 56 57 if _, b = c.GetHash("Key", "subKey"); b { 58 t.Error("Key - subKey 未过期") 59 } 60 } 61 62 //性能测试 63 //fixme 并发问题有待处理 64 func Test_cache_Get2(t *testing.T) { 65 c := NewWithExpiration(5 * time.Second) 66 length := 300000 67 ch := make(chan int8, length) 68 start := time.Now() 69 println("开始执行", start.UnixNano()) 70 for i := 0; i < length/3; i++ { 71 key := fmt.Sprintf("%s%d", "Key", i) 72 go func(ii int, k string) { 73 _ = c.Set(k, "库陈胜"+k) 74 ch <- int8(1) 75 _ = c.SetHash(k+"hash", strconv.Itoa(ii), "性能测试"+k) 76 ch <- int8(1) 77 _ = c.AddItem(k, ii) 78 ch <- int8(1) 79 }(i, key) 80 } 81 82 for i := 0; i < length; i++ { 83 <-ch 84 } 85 close(ch) 86 87 //for c.Cap() != 20000 { 88 // time.Sleep(100 * time.Millisecond) 89 // t.Logf("CAP %d", c.Cap()) 90 //} 91 92 t.Logf("PUT结束执行,耗时 %d ms, key总数: %d", time.Now().UnixMilli()-start.UnixMilli(), c.Cap()) 93 94 ch1 := make(chan int8, length) 95 for i := 0; i < length/3; i++ { 96 key := fmt.Sprintf("%s%d", "Key", i) 97 subKey := key + "hash" 98 go func(k, s string) { 99 _, _ = c.Get(k) 100 ch1 <- int8(1) 101 _, _ = c.GetHash(key+"hash", s) 102 ch1 <- int8(1) 103 ret := c.GetItem(k) 104 fmt.Println("k=", k, "value=", ret) 105 }(key, subKey) 106 } 107 for i := 0; i < length; i++ { 108 <-ch1 109 } 110 close(ch1) 111 112 t.Logf("当前key的数量 = %d", c.Cap()) 113 times := 1 114 for c.Cap() > 0 { 115 time.Sleep(time.Second) 116 t.Log("沉睡", times, "秒后,剩余多少Key?", c.Cap()) 117 times++ 118 } 119 } 120 121 func TestCache_AddItem(t *testing.T) { 122 c := NewWithExpiration(5 * time.Second) 123 start := time.Now().UnixMilli() 124 ch := make(chan int8, 100) 125 for i := 0; i < 100; i++ { 126 go func(ii int) { 127 if err := c.AddItem("test", ii, 666); err != nil { 128 t.Errorf("%v", err) 129 } 130 ch <- int8(1) 131 }(i) 132 } 133 for i := 0; i < 100; i++ { 134 <-ch 135 } 136 t.Log("执行结束,耗时", time.Now().UnixMilli()-start, "ms", "缓存大小", c.Cap()) 137 ret := c.GetItem("test") 138 t.Logf("size = %d,%v", len(ret), ret) 139 140 for i := 0; i < 100; i++ { 141 go func(ii int) { 142 if err := c.SetItem("test", ii, 777); err != nil { 143 t.Errorf("%v", err) 144 } 145 ch <- int8(1) 146 }(i) 147 } 148 for i := 0; i < 100; i++ { 149 <-ch 150 } 151 152 t.Log("执行结束,耗时", time.Now().UnixMilli()-start, "ms", "缓存大小", c.Cap()) 153 ret1 := c.GetItem("test") 154 t.Logf("size = %d,%v", len(ret1), ret1) 155 156 for i := 0; i < 100; i++ { 157 go func(ii int) { 158 if err := c.RemoveItem("test", ii); err != nil { 159 t.Errorf("%v", err) 160 } 161 ch <- int8(1) 162 }(i) 163 } 164 for i := 0; i < 100; i++ { 165 <-ch 166 } 167 t.Log("执行结束,耗时", time.Now().UnixMilli()-start, "ms", "缓存大小", c.Cap()) 168 ret2 := c.GetItem("test") 169 t.Logf("size = %d,%v", len(ret2), ret2) 170 171 }