2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-01-16 13:35:16 +08:00
|
|
|
// Package gcache provides high performance and concurrent-safe in-memory cache for process.
|
2017-11-23 10:21:28 +08:00
|
|
|
package gcache
|
|
|
|
|
2019-12-04 14:42:09 +08:00
|
|
|
import "time"
|
|
|
|
|
2019-06-10 21:23:49 +08:00
|
|
|
// Default cache object.
|
2018-09-18 09:45:06 +08:00
|
|
|
var cache = New()
|
2018-03-27 23:10:27 +08:00
|
|
|
|
2019-07-20 13:17:38 +08:00
|
|
|
// Set sets cache with <key>-<value> pair, which is expired after <duration>.
|
2019-12-08 22:55:32 +08:00
|
|
|
// It does not expire if <duration> <= 0.
|
2019-12-04 14:42:09 +08:00
|
|
|
func Set(key interface{}, value interface{}, duration time.Duration) {
|
2019-07-09 13:15:53 +08:00
|
|
|
cache.Set(key, value, duration)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// SetIfNotExist sets cache with <key>-<value> pair if <key> does not exist in the cache,
|
2019-12-08 22:55:32 +08:00
|
|
|
// which is expired after <duration>. It does not expire if <duration> <= 0.
|
2019-12-04 14:42:09 +08:00
|
|
|
func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool {
|
2019-07-09 13:15:53 +08:00
|
|
|
return cache.SetIfNotExist(key, value, duration)
|
2018-09-07 19:08:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-20 13:17:38 +08:00
|
|
|
// Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
|
|
|
|
//
|
2019-12-08 22:55:32 +08:00
|
|
|
// It does not expire if <duration> <= 0.
|
2019-12-04 14:42:09 +08:00
|
|
|
func Sets(data map[interface{}]interface{}, duration time.Duration) {
|
2019-07-09 13:15:53 +08:00
|
|
|
cache.Sets(data, duration)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Get returns the value of <key>.
|
|
|
|
// It returns nil if it does not exist or its value is nil.
|
2018-09-15 16:40:13 +08:00
|
|
|
func Get(key interface{}) interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Get(key)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// GetOrSet returns the value of <key>,
|
|
|
|
// or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache.
|
2019-07-20 13:17:38 +08:00
|
|
|
// The key-value pair expires after <duration>.
|
|
|
|
//
|
2019-12-08 22:55:32 +08:00
|
|
|
// It does not expire if <duration> <= 0.
|
2019-12-04 14:42:09 +08:00
|
|
|
func GetOrSet(key interface{}, value interface{}, duration time.Duration) interface{} {
|
2019-07-09 13:15:53 +08:00
|
|
|
return cache.GetOrSet(key, value, duration)
|
2018-09-07 19:08:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-08 22:55:32 +08:00
|
|
|
// GetOrSetFunc returns the value of <key>, or sets <key> with result of function <f>
|
|
|
|
// and returns its result if <key> does not exist in the cache. The key-value pair expires
|
|
|
|
// after <duration>. It does not expire if <duration> <= 0.
|
2019-12-04 14:42:09 +08:00
|
|
|
func GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration) interface{} {
|
2019-07-09 13:15:53 +08:00
|
|
|
return cache.GetOrSetFunc(key, f, duration)
|
2018-09-07 19:08:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-08 22:55:32 +08:00
|
|
|
// GetOrSetFuncLock returns the value of <key>, or sets <key> with result of function <f>
|
|
|
|
// and returns its result if <key> does not exist in the cache. The key-value pair expires
|
|
|
|
// after <duration>. It does not expire if <duration> <= 0.
|
2019-06-10 23:54:40 +08:00
|
|
|
//
|
|
|
|
// Note that the function <f> is executed within writing mutex lock.
|
2019-12-04 14:42:09 +08:00
|
|
|
func GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{} {
|
2019-07-09 13:15:53 +08:00
|
|
|
return cache.GetOrSetFuncLock(key, f, duration)
|
2018-09-20 09:28:45 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Contains returns true if <key> exists in the cache, or else returns false.
|
2018-09-15 16:40:13 +08:00
|
|
|
func Contains(key interface{}) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Contains(key)
|
2018-09-07 19:08:42 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Remove deletes the <key> in the cache, and returns its value.
|
2018-09-18 00:01:10 +08:00
|
|
|
func Remove(key interface{}) interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Remove(key)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Removes deletes <keys> in the cache.
|
2019-05-08 17:21:18 +08:00
|
|
|
func Removes(keys []interface{}) {
|
2019-06-19 09:06:52 +08:00
|
|
|
cache.Removes(keys)
|
2018-02-26 16:28:01 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Data returns a copy of all key-value pairs in the cache as map type.
|
2018-12-04 20:23:48 +08:00
|
|
|
func Data() map[interface{}]interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Data()
|
2018-12-04 20:23:48 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Keys returns all keys in the cache as slice.
|
2018-09-15 16:40:13 +08:00
|
|
|
func Keys() []interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Keys()
|
2018-03-27 17:53:46 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// KeyStrings returns all keys in the cache as string slice.
|
2018-09-15 16:40:13 +08:00
|
|
|
func KeyStrings() []string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.KeyStrings()
|
2018-09-15 16:40:13 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Values returns all values in the cache as slice.
|
2018-03-27 17:53:46 +08:00
|
|
|
func Values() []interface{} {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Values()
|
2018-03-27 17:53:46 +08:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:54:40 +08:00
|
|
|
// Size returns the size of the cache.
|
2018-03-27 17:53:46 +08:00
|
|
|
func Size() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return cache.Size()
|
2018-03-27 17:53:46 +08:00
|
|
|
}
|