@@ -11,6 +11,7 @@ package gcache
1111
1212import (
1313 "context"
14+ "sync"
1415 "time"
1516
1617 "github.com/gogf/gf/v2/container/gvar"
@@ -22,23 +23,25 @@ type Func = func(ctx context.Context) (value any, err error)
2223// DurationNoExpire represents the cache key-value pair that never expires.
2324const DurationNoExpire = time .Duration (0 )
2425
25- // Default cache object.
26- var defaultCache = New ()
26+ // defaultCache returns the lazily-initialized default cache instance using sync.OnceValue.
27+ var defaultCache = sync .OnceValue (func () * Cache {
28+ return New ()
29+ })
2730
2831// Set sets cache with `key`-`value` pair, which is expired after `duration`.
2932//
3033// It does not expire if `duration` == 0.
3134// It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
3235func Set (ctx context.Context , key any , value any , duration time.Duration ) error {
33- return defaultCache .Set (ctx , key , value , duration )
36+ return defaultCache () .Set (ctx , key , value , duration )
3437}
3538
3639// SetMap batch sets cache with key-value pairs by `data` map, which is expired after `duration`.
3740//
3841// It does not expire if `duration` == 0.
3942// It deletes the keys of `data` if `duration` < 0 or given `value` is nil.
4043func SetMap (ctx context.Context , data map [any ]any , duration time.Duration ) error {
41- return defaultCache .SetMap (ctx , data , duration )
44+ return defaultCache () .SetMap (ctx , data , duration )
4245}
4346
4447// SetIfNotExist sets cache with `key`-`value` pair which is expired after `duration`
@@ -48,7 +51,7 @@ func SetMap(ctx context.Context, data map[any]any, duration time.Duration) error
4851// It does not expire if `duration` == 0.
4952// It deletes the `key` if `duration` < 0 or given `value` is nil.
5053func SetIfNotExist (ctx context.Context , key any , value any , duration time.Duration ) (bool , error ) {
51- return defaultCache .SetIfNotExist (ctx , key , value , duration )
54+ return defaultCache () .SetIfNotExist (ctx , key , value , duration )
5255}
5356
5457// SetIfNotExistFunc sets `key` with result of function `f` and returns true
@@ -60,7 +63,7 @@ func SetIfNotExist(ctx context.Context, key any, value any, duration time.Durati
6063// It does not expire if `duration` == 0.
6164// It deletes the `key` if `duration` < 0 or given `value` is nil.
6265func SetIfNotExistFunc (ctx context.Context , key any , f Func , duration time.Duration ) (bool , error ) {
63- return defaultCache .SetIfNotExistFunc (ctx , key , f , duration )
66+ return defaultCache () .SetIfNotExistFunc (ctx , key , f , duration )
6467}
6568
6669// SetIfNotExistFuncLock sets `key` with result of function `f` and returns true
@@ -72,14 +75,14 @@ func SetIfNotExistFunc(ctx context.Context, key any, f Func, duration time.Durat
7275// Note that it differs from function `SetIfNotExistFunc` is that the function `f` is executed within
7376// writing mutex lock for concurrent safety purpose.
7477func SetIfNotExistFuncLock (ctx context.Context , key any , f Func , duration time.Duration ) (bool , error ) {
75- return defaultCache .SetIfNotExistFuncLock (ctx , key , f , duration )
78+ return defaultCache () .SetIfNotExistFuncLock (ctx , key , f , duration )
7679}
7780
7881// Get retrieves and returns the associated value of given `key`.
7982// It returns nil if it does not exist, or its value is nil, or it's expired.
8083// If you would like to check if the `key` exists in the cache, it's better using function Contains.
8184func Get (ctx context.Context , key any ) (* gvar.Var , error ) {
82- return defaultCache .Get (ctx , key )
85+ return defaultCache () .Get (ctx , key )
8386}
8487
8588// GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and
@@ -90,7 +93,7 @@ func Get(ctx context.Context, key any) (*gvar.Var, error) {
9093// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
9194// if `value` is a function and the function result is nil.
9295func GetOrSet (ctx context.Context , key any , value any , duration time.Duration ) (* gvar.Var , error ) {
93- return defaultCache .GetOrSet (ctx , key , value , duration )
96+ return defaultCache () .GetOrSet (ctx , key , value , duration )
9497}
9598
9699// GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of
@@ -101,7 +104,7 @@ func GetOrSet(ctx context.Context, key any, value any, duration time.Duration) (
101104// It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing
102105// if `value` is a function and the function result is nil.
103106func GetOrSetFunc (ctx context.Context , key any , f Func , duration time.Duration ) (* gvar.Var , error ) {
104- return defaultCache .GetOrSetFunc (ctx , key , f , duration )
107+ return defaultCache () .GetOrSetFunc (ctx , key , f , duration )
105108}
106109
107110// GetOrSetFuncLock retrieves and returns the value of `key`, or sets `key` with result of
@@ -115,12 +118,12 @@ func GetOrSetFunc(ctx context.Context, key any, f Func, duration time.Duration)
115118// Note that it differs from function `GetOrSetFunc` is that the function `f` is executed within
116119// writing mutex lock for concurrent safety purpose.
117120func GetOrSetFuncLock (ctx context.Context , key any , f Func , duration time.Duration ) (* gvar.Var , error ) {
118- return defaultCache .GetOrSetFuncLock (ctx , key , f , duration )
121+ return defaultCache () .GetOrSetFuncLock (ctx , key , f , duration )
119122}
120123
121124// Contains checks and returns true if `key` exists in the cache, or else returns false.
122125func Contains (ctx context.Context , key any ) (bool , error ) {
123- return defaultCache .Contains (ctx , key )
126+ return defaultCache () .Contains (ctx , key )
124127}
125128
126129// GetExpire retrieves and returns the expiration of `key` in the cache.
@@ -129,18 +132,18 @@ func Contains(ctx context.Context, key any) (bool, error) {
129132// It returns 0 if the `key` does not expire.
130133// It returns -1 if the `key` does not exist in the cache.
131134func GetExpire (ctx context.Context , key any ) (time.Duration , error ) {
132- return defaultCache .GetExpire (ctx , key )
135+ return defaultCache () .GetExpire (ctx , key )
133136}
134137
135138// Remove deletes one or more keys from cache, and returns its value.
136139// If multiple keys are given, it returns the value of the last deleted item.
137140func Remove (ctx context.Context , keys ... any ) (value * gvar.Var , err error ) {
138- return defaultCache .Remove (ctx , keys ... )
141+ return defaultCache () .Remove (ctx , keys ... )
139142}
140143
141144// Removes deletes `keys` in the cache.
142145func Removes (ctx context.Context , keys []any ) error {
143- return defaultCache .Removes (ctx , keys )
146+ return defaultCache () .Removes (ctx , keys )
144147}
145148
146149// Update updates the value of `key` without changing its expiration and returns the old value.
@@ -149,95 +152,95 @@ func Removes(ctx context.Context, keys []any) error {
149152// It deletes the `key` if given `value` is nil.
150153// It does nothing if `key` does not exist in the cache.
151154func Update (ctx context.Context , key any , value any ) (oldValue * gvar.Var , exist bool , err error ) {
152- return defaultCache .Update (ctx , key , value )
155+ return defaultCache () .Update (ctx , key , value )
153156}
154157
155158// UpdateExpire updates the expiration of `key` and returns the old expiration duration value.
156159//
157160// It returns -1 and does nothing if the `key` does not exist in the cache.
158161// It deletes the `key` if `duration` < 0.
159162func UpdateExpire (ctx context.Context , key any , duration time.Duration ) (oldDuration time.Duration , err error ) {
160- return defaultCache .UpdateExpire (ctx , key , duration )
163+ return defaultCache () .UpdateExpire (ctx , key , duration )
161164}
162165
163166// Size returns the number of items in the cache.
164167func Size (ctx context.Context ) (int , error ) {
165- return defaultCache .Size (ctx )
168+ return defaultCache () .Size (ctx )
166169}
167170
168171// Data returns a copy of all key-value pairs in the cache as map type.
169172// Note that this function may lead lots of memory usage, you can implement this function
170173// if necessary.
171174func Data (ctx context.Context ) (map [any ]any , error ) {
172- return defaultCache .Data (ctx )
175+ return defaultCache () .Data (ctx )
173176}
174177
175178// Keys returns all keys in the cache as slice.
176179func Keys (ctx context.Context ) ([]any , error ) {
177- return defaultCache .Keys (ctx )
180+ return defaultCache () .Keys (ctx )
178181}
179182
180183// KeyStrings returns all keys in the cache as string slice.
181184func KeyStrings (ctx context.Context ) ([]string , error ) {
182- return defaultCache .KeyStrings (ctx )
185+ return defaultCache () .KeyStrings (ctx )
183186}
184187
185188// Values returns all values in the cache as slice.
186189func Values (ctx context.Context ) ([]any , error ) {
187- return defaultCache .Values (ctx )
190+ return defaultCache () .Values (ctx )
188191}
189192
190193// MustGet acts like Get, but it panics if any error occurs.
191194func MustGet (ctx context.Context , key any ) * gvar.Var {
192- return defaultCache .MustGet (ctx , key )
195+ return defaultCache () .MustGet (ctx , key )
193196}
194197
195198// MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
196199func MustGetOrSet (ctx context.Context , key any , value any , duration time.Duration ) * gvar.Var {
197- return defaultCache .MustGetOrSet (ctx , key , value , duration )
200+ return defaultCache () .MustGetOrSet (ctx , key , value , duration )
198201}
199202
200203// MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
201204func MustGetOrSetFunc (ctx context.Context , key any , f Func , duration time.Duration ) * gvar.Var {
202- return defaultCache .MustGetOrSetFunc (ctx , key , f , duration )
205+ return defaultCache () .MustGetOrSetFunc (ctx , key , f , duration )
203206}
204207
205208// MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
206209func MustGetOrSetFuncLock (ctx context.Context , key any , f Func , duration time.Duration ) * gvar.Var {
207- return defaultCache .MustGetOrSetFuncLock (ctx , key , f , duration )
210+ return defaultCache () .MustGetOrSetFuncLock (ctx , key , f , duration )
208211}
209212
210213// MustContains acts like Contains, but it panics if any error occurs.
211214func MustContains (ctx context.Context , key any ) bool {
212- return defaultCache .MustContains (ctx , key )
215+ return defaultCache () .MustContains (ctx , key )
213216}
214217
215218// MustGetExpire acts like GetExpire, but it panics if any error occurs.
216219func MustGetExpire (ctx context.Context , key any ) time.Duration {
217- return defaultCache .MustGetExpire (ctx , key )
220+ return defaultCache () .MustGetExpire (ctx , key )
218221}
219222
220223// MustSize acts like Size, but it panics if any error occurs.
221224func MustSize (ctx context.Context ) int {
222- return defaultCache .MustSize (ctx )
225+ return defaultCache () .MustSize (ctx )
223226}
224227
225228// MustData acts like Data, but it panics if any error occurs.
226229func MustData (ctx context.Context ) map [any ]any {
227- return defaultCache .MustData (ctx )
230+ return defaultCache () .MustData (ctx )
228231}
229232
230233// MustKeys acts like Keys, but it panics if any error occurs.
231234func MustKeys (ctx context.Context ) []any {
232- return defaultCache .MustKeys (ctx )
235+ return defaultCache () .MustKeys (ctx )
233236}
234237
235238// MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
236239func MustKeyStrings (ctx context.Context ) []string {
237- return defaultCache .MustKeyStrings (ctx )
240+ return defaultCache () .MustKeyStrings (ctx )
238241}
239242
240243// MustValues acts like Values, but it panics if any error occurs.
241244func MustValues (ctx context.Context ) []any {
242- return defaultCache .MustValues (ctx )
245+ return defaultCache () .MustValues (ctx )
243246}
0 commit comments