pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/cache/examples_test.go (about) 1 package cache 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 "time" 13 ) 14 15 // ////////////////////////////////////////////////////////////////////////////////// // 16 17 func ExampleNew() { 18 cache := New(time.Second, time.Minute) 19 20 cache.Set("test", "ABCD") 21 22 fmt.Println(cache.Get("test")) 23 // Output: ABCD 24 } 25 26 func ExampleCache_Set() { 27 cache := New(time.Second, time.Minute) 28 29 cache.Set("test", "ABCD") 30 31 fmt.Println(cache.Get("test")) 32 // Output: ABCD 33 } 34 35 func ExampleCache_Has() { 36 cache := New(time.Second, time.Minute) 37 38 cache.Set("test", "ABCD") 39 40 fmt.Println(cache.Has("test")) 41 // Output: true 42 } 43 44 func ExampleCache_Get() { 45 cache := New(time.Second, time.Minute) 46 47 cache.Set("test", "ABCD") 48 49 fmt.Println(cache.Get("test")) 50 // Output: ABCD 51 } 52 53 func ExampleCache_Size() { 54 cache := New(time.Second, time.Minute) 55 56 cache.Set("test", "ABCD") 57 58 fmt.Println(cache.Size()) 59 // Output: 1 60 } 61 62 func ExampleCache_Expired() { 63 cache := New(time.Second, time.Minute) 64 65 cache.Set("test", "ABCD") 66 67 fmt.Println(cache.Expired()) 68 // Output: 0 69 } 70 71 func ExampleCache_GetWithExpiration() { 72 cache := New(time.Second, time.Minute) 73 74 cache.Set("test", "ABCD") 75 76 item, exp := cache.GetWithExpiration("test") 77 78 fmt.Println(item, exp.String()) 79 } 80 81 func ExampleCache_Delete() { 82 cache := New(time.Second, time.Minute) 83 84 cache.Set("test", "ABCD") 85 cache.Delete("test") 86 87 fmt.Println(cache.Get("test")) 88 // Output: <nil> 89 } 90 91 func ExampleCache_Flush() { 92 cache := New(time.Second, time.Minute) 93 94 cache.Set("test", "ABCD") 95 cache.Flush() 96 97 fmt.Println(cache.Get("test")) 98 // Output: <nil> 99 }