github.com/gogf/gf@v1.16.9/os/gcache/gcache_z_unit_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gcache_test 10 11 import ( 12 "context" 13 "github.com/gogf/gf/util/guid" 14 "math" 15 "testing" 16 "time" 17 18 "github.com/gogf/gf/container/gset" 19 "github.com/gogf/gf/frame/g" 20 "github.com/gogf/gf/os/gcache" 21 "github.com/gogf/gf/os/grpool" 22 "github.com/gogf/gf/test/gtest" 23 ) 24 25 func TestCache_GCache_Set(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 gcache.Set(1, 11, 0) 28 defer gcache.Removes(g.Slice{1, 2, 3}) 29 v, _ := gcache.Get(1) 30 t.Assert(v, 11) 31 b, _ := gcache.Contains(1) 32 t.Assert(b, true) 33 }) 34 } 35 36 func TestCache_Set(t *testing.T) { 37 gtest.C(t, func(t *gtest.T) { 38 c := gcache.New() 39 defer c.Close() 40 t.Assert(c.Set(1, 11, 0), nil) 41 v, _ := c.Get(1) 42 t.Assert(v, 11) 43 b, _ := c.Contains(1) 44 t.Assert(b, true) 45 }) 46 } 47 48 func TestCache_GetVar(t *testing.T) { 49 c := gcache.New() 50 defer c.Close() 51 gtest.C(t, func(t *gtest.T) { 52 t.Assert(c.Set(1, 11, 0), nil) 53 v, _ := c.Get(1) 54 t.Assert(v, 11) 55 b, _ := c.Contains(1) 56 t.Assert(b, true) 57 }) 58 gtest.C(t, func(t *gtest.T) { 59 v, _ := c.GetVar(1) 60 t.Assert(v.Int(), 11) 61 v, _ = c.GetVar(2) 62 t.Assert(v.Int(), 0) 63 t.Assert(v.IsNil(), true) 64 t.Assert(v.IsEmpty(), true) 65 }) 66 } 67 68 func TestCache_Set_Expire(t *testing.T) { 69 gtest.C(t, func(t *gtest.T) { 70 cache := gcache.New() 71 t.Assert(cache.Set(2, 22, 100*time.Millisecond), nil) 72 v, _ := cache.Get(2) 73 t.Assert(v, 22) 74 time.Sleep(200 * time.Millisecond) 75 v, _ = cache.Get(2) 76 t.Assert(v, nil) 77 time.Sleep(3 * time.Second) 78 n, _ := cache.Size() 79 t.Assert(n, 0) 80 t.Assert(cache.Close(), nil) 81 }) 82 83 gtest.C(t, func(t *gtest.T) { 84 cache := gcache.New() 85 t.Assert(cache.Set(1, 11, 100*time.Millisecond), nil) 86 v, _ := cache.Get(1) 87 t.Assert(v, 11) 88 time.Sleep(200 * time.Millisecond) 89 v, _ = cache.Get(1) 90 t.Assert(v, nil) 91 }) 92 } 93 94 func TestCache_Update_GetExpire(t *testing.T) { 95 // gcache 96 gtest.C(t, func(t *gtest.T) { 97 key := guid.S() 98 gcache.Set(key, 11, 3*time.Second) 99 expire1, _ := gcache.GetExpire(key) 100 gcache.Update(key, 12) 101 expire2, _ := gcache.GetExpire(key) 102 v, _ := gcache.GetVar(key) 103 t.Assert(v, 12) 104 t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds())) 105 }) 106 // gcache.Cache 107 gtest.C(t, func(t *gtest.T) { 108 cache := gcache.New() 109 cache.Set(1, 11, 3*time.Second) 110 expire1, _ := cache.GetExpire(1) 111 cache.Update(1, 12) 112 expire2, _ := cache.GetExpire(1) 113 v, _ := cache.GetVar(1) 114 t.Assert(v, 12) 115 t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds())) 116 }) 117 } 118 119 func TestCache_UpdateExpire(t *testing.T) { 120 // gcache 121 gtest.C(t, func(t *gtest.T) { 122 key := guid.S() 123 gcache.Set(key, 11, 3*time.Second) 124 defer gcache.Remove(key) 125 oldExpire, _ := gcache.GetExpire(key) 126 newExpire := 10 * time.Second 127 gcache.UpdateExpire(key, newExpire) 128 e, _ := gcache.GetExpire(key) 129 t.AssertNE(e, oldExpire) 130 e, _ = gcache.GetExpire(key) 131 t.Assert(math.Ceil(e.Seconds()), 10) 132 }) 133 // gcache.Cache 134 gtest.C(t, func(t *gtest.T) { 135 cache := gcache.New() 136 cache.Set(1, 11, 3*time.Second) 137 oldExpire, _ := cache.GetExpire(1) 138 newExpire := 10 * time.Second 139 cache.UpdateExpire(1, newExpire) 140 e, _ := cache.GetExpire(1) 141 t.AssertNE(e, oldExpire) 142 143 e, _ = cache.GetExpire(1) 144 t.Assert(math.Ceil(e.Seconds()), 10) 145 }) 146 } 147 148 func TestCache_Keys_Values(t *testing.T) { 149 gtest.C(t, func(t *gtest.T) { 150 c := gcache.New() 151 for i := 0; i < 10; i++ { 152 t.Assert(c.Set(i, i*10, 0), nil) 153 } 154 var ( 155 keys, _ = c.Keys() 156 values, _ = c.Values() 157 ) 158 t.Assert(len(keys), 10) 159 t.Assert(len(values), 10) 160 t.AssertIN(0, keys) 161 t.AssertIN(90, values) 162 }) 163 } 164 165 func TestCache_LRU(t *testing.T) { 166 gtest.C(t, func(t *gtest.T) { 167 cache := gcache.New(2) 168 for i := 0; i < 10; i++ { 169 cache.Set(i, i, 0) 170 } 171 n, _ := cache.Size() 172 t.Assert(n, 10) 173 v, _ := cache.Get(6) 174 t.Assert(v, 6) 175 time.Sleep(4 * time.Second) 176 n, _ = cache.Size() 177 t.Assert(n, 2) 178 v, _ = cache.Get(6) 179 t.Assert(v, 6) 180 v, _ = cache.Get(1) 181 t.Assert(v, nil) 182 t.Assert(cache.Close(), nil) 183 }) 184 } 185 186 func TestCache_LRU_expire(t *testing.T) { 187 gtest.C(t, func(t *gtest.T) { 188 cache := gcache.New(2) 189 t.Assert(cache.Set(1, nil, 1000), nil) 190 n, _ := cache.Size() 191 t.Assert(n, 1) 192 v, _ := cache.Get(1) 193 194 t.Assert(v, nil) 195 }) 196 } 197 198 func TestCache_SetIfNotExist(t *testing.T) { 199 gtest.C(t, func(t *gtest.T) { 200 cache := gcache.New() 201 cache.SetIfNotExist(1, 11, 0) 202 v, _ := cache.Get(1) 203 t.Assert(v, 11) 204 cache.SetIfNotExist(1, 22, 0) 205 v, _ = cache.Get(1) 206 t.Assert(v, 11) 207 cache.SetIfNotExist(2, 22, 0) 208 v, _ = cache.Get(2) 209 t.Assert(v, 22) 210 211 gcache.Removes(g.Slice{1, 2, 3}) 212 gcache.SetIfNotExist(1, 11, 0) 213 v, _ = gcache.Get(1) 214 t.Assert(v, 11) 215 gcache.SetIfNotExist(1, 22, 0) 216 v, _ = gcache.Get(1) 217 t.Assert(v, 11) 218 }) 219 } 220 221 func TestCache_Sets(t *testing.T) { 222 gtest.C(t, func(t *gtest.T) { 223 cache := gcache.New() 224 cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 225 v, _ := cache.Get(1) 226 t.Assert(v, 11) 227 228 gcache.Removes(g.Slice{1, 2, 3}) 229 gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 230 v, _ = cache.Get(1) 231 t.Assert(v, 11) 232 }) 233 } 234 235 func TestCache_GetOrSet(t *testing.T) { 236 gtest.C(t, func(t *gtest.T) { 237 cache := gcache.New() 238 cache.GetOrSet(1, 11, 0) 239 v, _ := cache.Get(1) 240 t.Assert(v, 11) 241 cache.GetOrSet(1, 111, 0) 242 243 v, _ = cache.Get(1) 244 t.Assert(v, 11) 245 gcache.Removes(g.Slice{1, 2, 3}) 246 gcache.GetOrSet(1, 11, 0) 247 248 v, _ = cache.Get(1) 249 t.Assert(v, 11) 250 251 gcache.GetOrSet(1, 111, 0) 252 v, _ = cache.Get(1) 253 t.Assert(v, 11) 254 }) 255 } 256 257 func TestCache_GetOrSetFunc(t *testing.T) { 258 gtest.C(t, func(t *gtest.T) { 259 cache := gcache.New() 260 cache.GetOrSetFunc(1, func() (interface{}, error) { 261 return 11, nil 262 }, 0) 263 v, _ := cache.Get(1) 264 t.Assert(v, 11) 265 266 cache.GetOrSetFunc(1, func() (interface{}, error) { 267 return 111, nil 268 }, 0) 269 v, _ = cache.Get(1) 270 t.Assert(v, 11) 271 272 gcache.Removes(g.Slice{1, 2, 3}) 273 274 gcache.GetOrSetFunc(1, func() (interface{}, error) { 275 return 11, nil 276 }, 0) 277 v, _ = cache.Get(1) 278 t.Assert(v, 11) 279 280 gcache.GetOrSetFunc(1, func() (interface{}, error) { 281 return 111, nil 282 }, 0) 283 v, _ = cache.Get(1) 284 t.Assert(v, 11) 285 }) 286 } 287 288 func TestCache_GetOrSetFuncLock(t *testing.T) { 289 gtest.C(t, func(t *gtest.T) { 290 cache := gcache.New() 291 cache.GetOrSetFuncLock(1, func() (interface{}, error) { 292 return 11, nil 293 }, 0) 294 v, _ := cache.Get(1) 295 t.Assert(v, 11) 296 297 cache.GetOrSetFuncLock(1, func() (interface{}, error) { 298 return 111, nil 299 }, 0) 300 v, _ = cache.Get(1) 301 t.Assert(v, 11) 302 303 gcache.Removes(g.Slice{1, 2, 3}) 304 gcache.GetOrSetFuncLock(1, func() (interface{}, error) { 305 return 11, nil 306 }, 0) 307 v, _ = cache.Get(1) 308 t.Assert(v, 11) 309 310 gcache.GetOrSetFuncLock(1, func() (interface{}, error) { 311 return 111, nil 312 }, 0) 313 v, _ = cache.Get(1) 314 t.Assert(v, 11) 315 }) 316 } 317 318 func TestCache_Clear(t *testing.T) { 319 gtest.C(t, func(t *gtest.T) { 320 cache := gcache.New() 321 cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 322 cache.Clear() 323 n, _ := cache.Size() 324 t.Assert(n, 0) 325 }) 326 } 327 328 func TestCache_SetConcurrency(t *testing.T) { 329 gtest.C(t, func(t *gtest.T) { 330 cache := gcache.New() 331 pool := grpool.New(4) 332 go func() { 333 for { 334 pool.Add(func() { 335 cache.SetIfNotExist(1, 11, 10) 336 }) 337 } 338 }() 339 select { 340 case <-time.After(2 * time.Second): 341 //t.Log("first part end") 342 } 343 344 go func() { 345 for { 346 pool.Add(func() { 347 cache.SetIfNotExist(1, nil, 10) 348 }) 349 } 350 }() 351 select { 352 case <-time.After(2 * time.Second): 353 //t.Log("second part end") 354 } 355 }) 356 } 357 358 func TestCache_Basic(t *testing.T) { 359 gtest.C(t, func(t *gtest.T) { 360 { 361 cache := gcache.New() 362 cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 363 b, _ := cache.Contains(1) 364 t.Assert(b, true) 365 v, _ := cache.Get(1) 366 t.Assert(v, 11) 367 data, _ := cache.Data() 368 t.Assert(data[1], 11) 369 t.Assert(data[2], 22) 370 t.Assert(data[3], nil) 371 n, _ := cache.Size() 372 t.Assert(n, 2) 373 keys, _ := cache.Keys() 374 t.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true) 375 keyStrs, _ := cache.KeyStrings() 376 t.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true) 377 values, _ := cache.Values() 378 t.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true) 379 removeData1, _ := cache.Remove(1) 380 t.Assert(removeData1, 11) 381 n, _ = cache.Size() 382 t.Assert(n, 1) 383 cache.Removes(g.Slice{2}) 384 n, _ = cache.Size() 385 t.Assert(n, 0) 386 } 387 388 gcache.Remove(g.Slice{1, 2, 3}...) 389 { 390 gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 391 b, _ := gcache.Contains(1) 392 t.Assert(b, true) 393 v, _ := gcache.Get(1) 394 t.Assert(v, 11) 395 data, _ := gcache.Data() 396 t.Assert(data[1], 11) 397 t.Assert(data[2], 22) 398 t.Assert(data[3], nil) 399 n, _ := gcache.Size() 400 t.Assert(n, 2) 401 keys, _ := gcache.Keys() 402 t.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true) 403 keyStrs, _ := gcache.KeyStrings() 404 t.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true) 405 values, _ := gcache.Values() 406 t.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true) 407 removeData1, _ := gcache.Remove(1) 408 t.Assert(removeData1, 11) 409 n, _ = gcache.Size() 410 t.Assert(n, 1) 411 gcache.Removes(g.Slice{2}) 412 n, _ = gcache.Size() 413 t.Assert(n, 0) 414 } 415 }) 416 } 417 418 func TestCache_Ctx(t *testing.T) { 419 gtest.C(t, func(t *gtest.T) { 420 cache := gcache.New() 421 cache.Ctx(context.Background()).Sets(g.MapAnyAny{1: 11, 2: 22}, 0) 422 b, _ := cache.Contains(1) 423 t.Assert(b, true) 424 v, _ := cache.Get(1) 425 t.Assert(v, 11) 426 }) 427 }