github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/random_expired_cache_test.go (about) 1 // The package is migrated from beego, you can get from following link: 2 // import( 3 // "github.com/beego/beego/v2/client/cache" 4 // ) 5 // Copyright 2023. All Rights Reserved. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 19 package cache 20 21 import ( 22 "context" 23 "fmt" 24 "math/rand" 25 "strings" 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestRandomExpireCache(t *testing.T) { 33 bm, err := NewCache("memory", `{"interval":20}`) 34 assert.Nil(t, err) 35 36 cache := NewRandomExpireCache(bm) 37 // should not be nil 38 assert.NotNil(t, cache.(*RandomExpireCache).offset) 39 40 timeoutDuration := 3 * time.Second 41 42 if err = cache.Put(context.Background(), "Leon Ding", 22, timeoutDuration); err != nil { 43 t.Error("set Error", err) 44 } 45 46 // testing random expire cache 47 time.Sleep(timeoutDuration + 3 + time.Second) 48 49 if res, _ := cache.IsExist(context.Background(), "Leon Ding"); !res { 50 t.Error("check err") 51 } 52 53 if v, _ := cache.Get(context.Background(), "Leon Ding"); v.(int) != 22 { 54 t.Error("get err") 55 } 56 57 assert.Nil(t, cache.Delete(context.Background(), "Leon Ding")) 58 res, _ := cache.IsExist(context.Background(), "Leon Ding") 59 assert.False(t, res) 60 61 assert.Nil(t, cache.Put(context.Background(), "Leon Ding", "author", timeoutDuration)) 62 63 assert.Nil(t, cache.Delete(context.Background(), "astaxie")) 64 res, _ = cache.IsExist(context.Background(), "astaxie") 65 assert.False(t, res) 66 67 assert.Nil(t, cache.Put(context.Background(), "astaxie", "author", timeoutDuration)) 68 69 res, _ = cache.IsExist(context.Background(), "astaxie") 70 assert.True(t, res) 71 72 v, _ := cache.Get(context.Background(), "astaxie") 73 assert.Equal(t, "author", v) 74 75 assert.Nil(t, cache.Put(context.Background(), "astaxie1", "author1", timeoutDuration)) 76 77 res, _ = cache.IsExist(context.Background(), "astaxie1") 78 assert.True(t, res) 79 80 vv, _ := cache.GetMulti(context.Background(), []string{"astaxie", "astaxie1"}) 81 assert.Equal(t, 2, len(vv)) 82 assert.Equal(t, "author", vv[0]) 83 assert.Equal(t, "author1", vv[1]) 84 85 vv, err = cache.GetMulti(context.Background(), []string{"astaxie0", "astaxie1"}) 86 assert.Equal(t, 2, len(vv)) 87 assert.Nil(t, vv[0]) 88 assert.Equal(t, "author1", vv[1]) 89 90 assert.NotNil(t, err) 91 assert.True(t, strings.Contains(err.Error(), "key isn't exist")) 92 } 93 94 func TestWithRandomExpireOffsetFunc(t *testing.T) { 95 bm, err := NewCache("memory", `{"interval":20}`) 96 assert.Nil(t, err) 97 98 magic := -time.Duration(rand.Int()) 99 cache := NewRandomExpireCache(bm, WithRandomExpireOffsetFunc(func() time.Duration { 100 return magic 101 })) 102 // offset should return the magic value 103 assert.Equal(t, magic, cache.(*RandomExpireCache).offset()) 104 } 105 106 func ExampleNewRandomExpireCache() { 107 mc := NewMemoryCache() 108 // use the default strategy which will generate random time offset (range: [3s,8s)) expired 109 c := NewRandomExpireCache(mc) 110 // so the expiration will be [1m3s, 1m8s) 111 err := c.Put(context.Background(), "hello", "world", time.Minute) 112 if err != nil { 113 panic(err) 114 } 115 116 c = NewRandomExpireCache(mc, 117 // based on the expiration 118 WithRandomExpireOffsetFunc(func() time.Duration { 119 val := rand.Int31n(100) 120 fmt.Printf("calculate offset") 121 return time.Duration(val) * time.Second 122 })) 123 124 // so the expiration will be [1m0s, 1m100s) 125 err = c.Put(context.Background(), "hello", "world", time.Minute) 126 if err != nil { 127 panic(err) 128 } 129 130 // Output: 131 // calculate offset 132 }