You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

2 months ago
package memory
import (
"github.com/allegro/bigcache"
"time"
)
var ForexCache *bigcache.BigCache // 外汇行情缓存
// NewBigCache
//
// @Description: 初始化交易-实时数据缓存
// @return *bigcache.BigCache
func NewBigCache() *bigcache.BigCache {
config := bigcache.Config{
// Set the number of partitions, which must be an integer multiple of 2
Shards: 1024,
// After LifeWindow, cached objects are considered inactive, but they are not deleted
LifeWindow: 180 * time.Second,
// After CleanWindow, objects that are considered inactive will be deleted, with 0 representing no operation
CleanWindow: 150 * time.Second,
// Set the maximum number of storage objects, which can only be set during initialization
//MaxEntriesInWindow: 1000 * 10 * 60,
MaxEntriesInWindow: 1,
// The maximum number of bytes for cache objects, which can only be set during initialization
MaxEntrySize: 500,
// Print memory allocation information
Verbose: true,
// Set the maximum cache value (in MB), where 0 represents unlimited
HardMaxCacheSize: 0, //8192
// When the cache expires or is deleted, a callback function can be set with parameters (key, val), and the default is nil
OnRemove: nil,
// When the cache expires or is deleted, a callback function can be set with parameters such as (key, val, reason). The default setting is nil and not set
OnRemoveWithReason: nil,
}
cache, err := bigcache.NewBigCache(config)
if err != nil {
return nil
}
return cache
}
func init() {
ForexCache = NewBigCache()
}
// GetForexCache
//
// @Description: 外汇买一卖一行情
// @param key
// @return []byte
// @return error
func GetForexCache(key string) ([]byte, error) {
conP, err := ForexCache.Get(key)
if err != nil {
return conP, nil
}
return conP, err
}