github.com/wangyougui/gf/v2@v2.6.5/os/gcache/gcache_z_unit_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/wangyougui/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gcache_test 10 11 import ( 12 "context" 13 "math" 14 "testing" 15 "time" 16 17 "github.com/wangyougui/gf/v2/container/gset" 18 "github.com/wangyougui/gf/v2/frame/g" 19 "github.com/wangyougui/gf/v2/os/gcache" 20 "github.com/wangyougui/gf/v2/os/grpool" 21 "github.com/wangyougui/gf/v2/test/gtest" 22 "github.com/wangyougui/gf/v2/util/guid" 23 ) 24 25 var ( 26 ctx = context.Background() 27 ) 28 29 func TestCache_GCache_Set(t *testing.T) { 30 gtest.C(t, func(t *gtest.T) { 31 t.AssertNil(gcache.Set(ctx, 1, 11, 0)) 32 defer gcache.Remove(ctx, g.Slice{1, 2, 3}...) 33 v, _ := gcache.Get(ctx, 1) 34 t.Assert(v, 11) 35 b, _ := gcache.Contains(ctx, 1) 36 t.Assert(b, true) 37 }) 38 } 39 40 func TestCache_Set(t *testing.T) { 41 gtest.C(t, func(t *gtest.T) { 42 c := gcache.New() 43 defer c.Close(ctx) 44 t.Assert(c.Set(ctx, 1, 11, 0), nil) 45 v, _ := c.Get(ctx, 1) 46 t.Assert(v, 11) 47 b, _ := c.Contains(ctx, 1) 48 t.Assert(b, true) 49 }) 50 } 51 52 func TestCache_Set_Expire(t *testing.T) { 53 gtest.C(t, func(t *gtest.T) { 54 cache := gcache.New() 55 t.Assert(cache.Set(ctx, 2, 22, 100*time.Millisecond), nil) 56 v, _ := cache.Get(ctx, 2) 57 t.Assert(v, 22) 58 time.Sleep(200 * time.Millisecond) 59 v, _ = cache.Get(ctx, 2) 60 t.Assert(v, nil) 61 time.Sleep(3 * time.Second) 62 n, _ := cache.Size(ctx) 63 t.Assert(n, 0) 64 t.Assert(cache.Close(ctx), nil) 65 }) 66 67 gtest.C(t, func(t *gtest.T) { 68 cache := gcache.New() 69 t.Assert(cache.Set(ctx, 1, 11, 100*time.Millisecond), nil) 70 v, _ := cache.Get(ctx, 1) 71 t.Assert(v, 11) 72 time.Sleep(200 * time.Millisecond) 73 v, _ = cache.Get(ctx, 1) 74 t.Assert(v, nil) 75 }) 76 } 77 78 func TestCache_Update(t *testing.T) { 79 // gcache 80 gtest.C(t, func(t *gtest.T) { 81 key := guid.S() 82 t.AssertNil(gcache.Set(ctx, key, 11, 3*time.Second)) 83 expire1, _ := gcache.GetExpire(ctx, key) 84 oldValue, exist, err := gcache.Update(ctx, key, 12) 85 t.AssertNil(err) 86 t.Assert(oldValue, 11) 87 t.Assert(exist, true) 88 89 expire2, _ := gcache.GetExpire(ctx, key) 90 v, _ := gcache.Get(ctx, key) 91 t.Assert(v, 12) 92 t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds())) 93 }) 94 // gcache.Cache 95 gtest.C(t, func(t *gtest.T) { 96 cache := gcache.New() 97 t.AssertNil(cache.Set(ctx, 1, 11, 3*time.Second)) 98 99 oldValue, exist, err := cache.Update(ctx, 1, 12) 100 t.AssertNil(err) 101 t.Assert(oldValue, 11) 102 t.Assert(exist, true) 103 104 expire1, _ := cache.GetExpire(ctx, 1) 105 expire2, _ := cache.GetExpire(ctx, 1) 106 v, _ := cache.Get(ctx, 1) 107 t.Assert(v, 12) 108 t.Assert(math.Ceil(expire1.Seconds()), math.Ceil(expire2.Seconds())) 109 }) 110 } 111 112 func TestCache_UpdateExpire(t *testing.T) { 113 // gcache 114 gtest.C(t, func(t *gtest.T) { 115 key := guid.S() 116 t.AssertNil(gcache.Set(ctx, key, 11, 3*time.Second)) 117 defer gcache.Remove(ctx, key) 118 oldExpire, _ := gcache.GetExpire(ctx, key) 119 newExpire := 10 * time.Second 120 oldExpire2, err := gcache.UpdateExpire(ctx, key, newExpire) 121 t.AssertNil(err) 122 t.AssertIN(oldExpire2, g.Slice{oldExpire, `2.999s`}) 123 124 e, _ := gcache.GetExpire(ctx, key) 125 t.AssertNE(e, oldExpire) 126 e, _ = gcache.GetExpire(ctx, key) 127 t.Assert(math.Ceil(e.Seconds()), 10) 128 }) 129 // gcache.Cache 130 gtest.C(t, func(t *gtest.T) { 131 cache := gcache.New() 132 t.AssertNil(cache.Set(ctx, 1, 11, 3*time.Second)) 133 oldExpire, _ := cache.GetExpire(ctx, 1) 134 newExpire := 10 * time.Second 135 oldExpire2, err := cache.UpdateExpire(ctx, 1, newExpire) 136 t.AssertNil(err) 137 t.AssertIN(oldExpire2, g.Slice{oldExpire, `2.999s`}) 138 139 e, _ := cache.GetExpire(ctx, 1) 140 t.AssertNE(e, oldExpire) 141 142 e, _ = cache.GetExpire(ctx, 1) 143 t.Assert(math.Ceil(e.Seconds()), 10) 144 }) 145 } 146 147 func TestCache_Keys_Values(t *testing.T) { 148 gtest.C(t, func(t *gtest.T) { 149 c := gcache.New() 150 for i := 0; i < 10; i++ { 151 t.Assert(c.Set(ctx, i, i*10, 0), nil) 152 } 153 var ( 154 keys, _ = c.Keys(ctx) 155 values, _ = c.Values(ctx) 156 ) 157 t.Assert(len(keys), 10) 158 t.Assert(len(values), 10) 159 t.AssertIN(0, keys) 160 t.AssertIN(90, values) 161 }) 162 } 163 164 func TestCache_LRU(t *testing.T) { 165 gtest.C(t, func(t *gtest.T) { 166 cache := gcache.New(2) 167 for i := 0; i < 10; i++ { 168 t.AssertNil(cache.Set(ctx, i, i, 0)) 169 } 170 n, _ := cache.Size(ctx) 171 t.Assert(n, 10) 172 v, _ := cache.Get(ctx, 6) 173 t.Assert(v, 6) 174 time.Sleep(4 * time.Second) 175 g.Log().Debugf(ctx, `items after lru: %+v`, cache.MustData(ctx)) 176 n, _ = cache.Size(ctx) 177 t.Assert(n, 2) 178 v, _ = cache.Get(ctx, 6) 179 t.Assert(v, 6) 180 v, _ = cache.Get(ctx, 1) 181 t.Assert(v, nil) 182 t.Assert(cache.Close(ctx), 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(ctx, 1, nil, 1000), nil) 190 n, _ := cache.Size(ctx) 191 t.Assert(n, 1) 192 v, _ := cache.Get(ctx, 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 ok, err := cache.SetIfNotExist(ctx, 1, 11, 0) 202 t.AssertNil(err) 203 t.Assert(ok, true) 204 205 v, _ := cache.Get(ctx, 1) 206 t.Assert(v, 11) 207 208 ok, err = cache.SetIfNotExist(ctx, 1, 22, 0) 209 t.AssertNil(err) 210 t.Assert(ok, false) 211 212 v, _ = cache.Get(ctx, 1) 213 t.Assert(v, 11) 214 215 ok, err = cache.SetIfNotExist(ctx, 2, 22, 0) 216 t.AssertNil(err) 217 t.Assert(ok, true) 218 219 v, _ = cache.Get(ctx, 2) 220 t.Assert(v, 22) 221 222 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 223 ok, err = gcache.SetIfNotExist(ctx, 1, 11, 0) 224 t.AssertNil(err) 225 t.Assert(ok, true) 226 227 v, _ = gcache.Get(ctx, 1) 228 t.Assert(v, 11) 229 230 ok, err = gcache.SetIfNotExist(ctx, 1, 22, 0) 231 t.AssertNil(err) 232 t.Assert(ok, false) 233 234 v, _ = gcache.Get(ctx, 1) 235 t.Assert(v, 11) 236 }) 237 } 238 239 func TestCache_SetIfNotExistFunc(t *testing.T) { 240 gtest.C(t, func(t *gtest.T) { 241 cache := gcache.New() 242 exist, err := cache.SetIfNotExistFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 243 return 11, nil 244 }, 0) 245 t.AssertNil(err) 246 t.Assert(exist, true) 247 248 v, _ := cache.Get(ctx, 1) 249 t.Assert(v, 11) 250 251 exist, err = cache.SetIfNotExistFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 252 return 22, nil 253 }, 0) 254 t.AssertNil(err) 255 t.Assert(exist, false) 256 257 v, _ = cache.Get(ctx, 1) 258 t.Assert(v, 11) 259 }) 260 gtest.C(t, func(t *gtest.T) { 261 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 262 263 ok, err := gcache.SetIfNotExistFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 264 return 11, nil 265 }, 0) 266 t.AssertNil(err) 267 t.Assert(ok, true) 268 269 v, _ := gcache.Get(ctx, 1) 270 t.Assert(v, 11) 271 272 ok, err = gcache.SetIfNotExistFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 273 return 22, nil 274 }, 0) 275 t.AssertNil(err) 276 t.Assert(ok, false) 277 278 v, _ = gcache.Get(ctx, 1) 279 t.Assert(v, 11) 280 }) 281 } 282 283 func TestCache_SetIfNotExistFuncLock(t *testing.T) { 284 gtest.C(t, func(t *gtest.T) { 285 cache := gcache.New() 286 exist, err := cache.SetIfNotExistFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 287 return 11, nil 288 }, 0) 289 t.AssertNil(err) 290 t.Assert(exist, true) 291 292 v, _ := cache.Get(ctx, 1) 293 t.Assert(v, 11) 294 295 exist, err = cache.SetIfNotExistFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 296 return 22, nil 297 }, 0) 298 t.AssertNil(err) 299 t.Assert(exist, false) 300 301 v, _ = cache.Get(ctx, 1) 302 t.Assert(v, 11) 303 }) 304 gtest.C(t, func(t *gtest.T) { 305 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 306 307 exist, err := gcache.SetIfNotExistFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 308 return 11, nil 309 }, 0) 310 t.AssertNil(err) 311 t.Assert(exist, true) 312 313 v, _ := gcache.Get(ctx, 1) 314 t.Assert(v, 11) 315 316 exist, err = gcache.SetIfNotExistFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 317 return 22, nil 318 }, 0) 319 t.AssertNil(err) 320 t.Assert(exist, false) 321 322 v, _ = gcache.Get(ctx, 1) 323 t.Assert(v, 11) 324 }) 325 } 326 327 func TestCache_SetMap(t *testing.T) { 328 gtest.C(t, func(t *gtest.T) { 329 cache := gcache.New() 330 t.AssertNil(cache.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, 0)) 331 v, _ := cache.Get(ctx, 1) 332 t.Assert(v, 11) 333 334 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 335 t.AssertNil(gcache.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, 0)) 336 v, _ = cache.Get(ctx, 1) 337 t.Assert(v, 11) 338 }) 339 } 340 341 func TestCache_GetOrSet(t *testing.T) { 342 gtest.C(t, func(t *gtest.T) { 343 cache := gcache.New() 344 value, err := cache.GetOrSet(ctx, 1, 11, 0) 345 t.AssertNil(err) 346 t.Assert(value, 11) 347 348 v, _ := cache.Get(ctx, 1) 349 t.Assert(v, 11) 350 value, err = cache.GetOrSet(ctx, 1, 111, 0) 351 t.AssertNil(err) 352 t.Assert(value, 11) 353 354 v, _ = cache.Get(ctx, 1) 355 t.Assert(v, 11) 356 }) 357 358 gtest.C(t, func(t *gtest.T) { 359 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 360 value, err := gcache.GetOrSet(ctx, 1, 11, 0) 361 t.AssertNil(err) 362 t.Assert(value, 11) 363 364 v, err := gcache.Get(ctx, 1) 365 t.AssertNil(err) 366 t.Assert(v, 11) 367 368 value, err = gcache.GetOrSet(ctx, 1, 111, 0) 369 t.AssertNil(err) 370 t.Assert(value, 11) 371 372 v, err = gcache.Get(ctx, 1) 373 t.AssertNil(err) 374 t.Assert(v, 11) 375 }) 376 } 377 378 func TestCache_GetOrSetFunc(t *testing.T) { 379 gtest.C(t, func(t *gtest.T) { 380 cache := gcache.New() 381 cache.GetOrSetFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 382 return 11, nil 383 }, 0) 384 v, _ := cache.Get(ctx, 1) 385 t.Assert(v, 11) 386 387 cache.GetOrSetFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 388 return 111, nil 389 }, 0) 390 v, _ = cache.Get(ctx, 1) 391 t.Assert(v, 11) 392 393 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 394 395 gcache.GetOrSetFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 396 return 11, nil 397 }, 0) 398 v, _ = cache.Get(ctx, 1) 399 t.Assert(v, 11) 400 401 gcache.GetOrSetFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 402 return 111, nil 403 }, 0) 404 v, _ = cache.Get(ctx, 1) 405 t.Assert(v, 11) 406 }) 407 } 408 409 func TestCache_GetOrSetFuncLock(t *testing.T) { 410 gtest.C(t, func(t *gtest.T) { 411 cache := gcache.New() 412 cache.GetOrSetFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 413 return 11, nil 414 }, 0) 415 v, _ := cache.Get(ctx, 1) 416 t.Assert(v, 11) 417 418 cache.GetOrSetFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 419 return 111, nil 420 }, 0) 421 v, _ = cache.Get(ctx, 1) 422 t.Assert(v, 11) 423 424 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 425 gcache.GetOrSetFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 426 return 11, nil 427 }, 0) 428 v, _ = cache.Get(ctx, 1) 429 t.Assert(v, 11) 430 431 gcache.GetOrSetFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { 432 return 111, nil 433 }, 0) 434 v, _ = cache.Get(ctx, 1) 435 t.Assert(v, 11) 436 }) 437 } 438 439 func TestCache_Clear(t *testing.T) { 440 gtest.C(t, func(t *gtest.T) { 441 cache := gcache.New() 442 cache.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, 0) 443 cache.Clear(ctx) 444 n, _ := cache.Size(ctx) 445 t.Assert(n, 0) 446 }) 447 } 448 449 func TestCache_SetConcurrency(t *testing.T) { 450 gtest.C(t, func(t *gtest.T) { 451 cache := gcache.New() 452 pool := grpool.New(4) 453 go func() { 454 for { 455 pool.Add(ctx, func(ctx context.Context) { 456 cache.SetIfNotExist(ctx, 1, 11, 10) 457 }) 458 } 459 }() 460 select { 461 case <-time.After(2 * time.Second): 462 // t.Log("first part end") 463 } 464 465 go func() { 466 for { 467 pool.Add(ctx, func(ctx context.Context) { 468 cache.SetIfNotExist(ctx, 1, nil, 10) 469 }) 470 } 471 }() 472 select { 473 case <-time.After(2 * time.Second): 474 // t.Log("second part end") 475 } 476 }) 477 } 478 479 func TestCache_Basic(t *testing.T) { 480 gtest.C(t, func(t *gtest.T) { 481 { 482 cache := gcache.New() 483 cache.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, 0) 484 b, _ := cache.Contains(ctx, 1) 485 t.Assert(b, true) 486 v, _ := cache.Get(ctx, 1) 487 t.Assert(v, 11) 488 data, _ := cache.Data(ctx) 489 t.Assert(data[1], 11) 490 t.Assert(data[2], 22) 491 t.Assert(data[3], nil) 492 n, _ := cache.Size(ctx) 493 t.Assert(n, 2) 494 keys, _ := cache.Keys(ctx) 495 t.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true) 496 keyStrs, _ := cache.KeyStrings(ctx) 497 t.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true) 498 values, _ := cache.Values(ctx) 499 t.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true) 500 removeData1, _ := cache.Remove(ctx, 1) 501 t.Assert(removeData1, 11) 502 n, _ = cache.Size(ctx) 503 t.Assert(n, 1) 504 505 cache.Remove(ctx, 2) 506 n, _ = cache.Size(ctx) 507 t.Assert(n, 0) 508 } 509 510 gcache.Remove(ctx, g.Slice{1, 2, 3}...) 511 { 512 gcache.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, 0) 513 b, _ := gcache.Contains(ctx, 1) 514 t.Assert(b, true) 515 v, _ := gcache.Get(ctx, 1) 516 t.Assert(v, 11) 517 data, _ := gcache.Data(ctx) 518 t.Assert(data[1], 11) 519 t.Assert(data[2], 22) 520 t.Assert(data[3], nil) 521 n, _ := gcache.Size(ctx) 522 t.Assert(n, 2) 523 keys, _ := gcache.Keys(ctx) 524 t.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true) 525 keyStrs, _ := gcache.KeyStrings(ctx) 526 t.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true) 527 values, _ := gcache.Values(ctx) 528 t.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true) 529 removeData1, _ := gcache.Remove(ctx, 1) 530 t.Assert(removeData1, 11) 531 n, _ = gcache.Size(ctx) 532 t.Assert(n, 1) 533 gcache.Remove(ctx, 2) 534 n, _ = gcache.Size(ctx) 535 t.Assert(n, 0) 536 } 537 }) 538 } 539 540 func TestCache_Removes(t *testing.T) { 541 gtest.C(t, func(t *gtest.T) { 542 cache := gcache.New() 543 t.AssertNil(cache.Set(ctx, 1, 11, 0)) 544 t.AssertNil(cache.Set(ctx, 2, 22, 0)) 545 t.AssertNil(cache.Set(ctx, 3, 33, 0)) 546 t.AssertNil(cache.Removes(ctx, g.Slice{2, 3})) 547 548 ok, err := cache.Contains(ctx, 1) 549 t.AssertNil(err) 550 t.Assert(ok, true) 551 552 ok, err = cache.Contains(ctx, 2) 553 t.AssertNil(err) 554 t.Assert(ok, false) 555 }) 556 557 gtest.C(t, func(t *gtest.T) { 558 t.AssertNil(gcache.Set(ctx, 1, 11, 0)) 559 t.AssertNil(gcache.Set(ctx, 2, 22, 0)) 560 t.AssertNil(gcache.Set(ctx, 3, 33, 0)) 561 t.AssertNil(gcache.Removes(ctx, g.Slice{2, 3})) 562 563 ok, err := gcache.Contains(ctx, 1) 564 t.AssertNil(err) 565 t.Assert(ok, true) 566 567 ok, err = gcache.Contains(ctx, 2) 568 t.AssertNil(err) 569 t.Assert(ok, false) 570 }) 571 } 572 573 func TestCache_Basic_Must(t *testing.T) { 574 gtest.C(t, func(t *gtest.T) { 575 defer gcache.Remove(ctx, g.Slice{1, 2, 3, 4}...) 576 577 t.AssertNil(gcache.Set(ctx, 1, 11, 0)) 578 v := gcache.MustGet(ctx, 1) 579 t.Assert(v, 11) 580 gcache.MustGetOrSet(ctx, 2, 22, 0) 581 v = gcache.MustGet(ctx, 2) 582 t.Assert(v, 22) 583 584 gcache.MustGetOrSetFunc(ctx, 3, func(ctx context.Context) (value interface{}, err error) { 585 return 33, nil 586 }, 0) 587 v = gcache.MustGet(ctx, 3) 588 t.Assert(v, 33) 589 590 gcache.GetOrSetFuncLock(ctx, 4, func(ctx context.Context) (value interface{}, err error) { 591 return 44, nil 592 }, 0) 593 v = gcache.MustGet(ctx, 4) 594 t.Assert(v, 44) 595 596 t.Assert(gcache.MustContains(ctx, 1), true) 597 598 t.AssertNil(gcache.Set(ctx, 1, 11, 3*time.Second)) 599 expire := gcache.MustGetExpire(ctx, 1) 600 t.AssertGE(expire, 0) 601 602 n := gcache.MustSize(ctx) 603 t.Assert(n, 4) 604 605 data := gcache.MustData(ctx) 606 t.Assert(len(data), 4) 607 608 keys := gcache.MustKeys(ctx) 609 t.Assert(len(keys), 4) 610 611 keyStrings := gcache.MustKeyStrings(ctx) 612 t.Assert(len(keyStrings), 4) 613 614 values := gcache.MustValues(ctx) 615 t.Assert(len(values), 4) 616 }) 617 } 618 619 func TestCache_NewWithAdapter(t *testing.T) { 620 gtest.C(t, func(t *gtest.T) { 621 cache := gcache.NewWithAdapter(gcache.NewAdapterMemory()) 622 t.AssertNE(cache, nil) 623 }) 624 }