123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package common
- import (
- "errors"
- "time"
- "github.com/patrickmn/go-cache"
- )
- var memCache *cache.Cache
- var licenseCache *cache.Cache
- func init() {
- memCache = cache.New(30*time.Second, 3*time.Second) //30秒钟超时,每3秒检查是否有需要超时清除
- licenseCache = cache.New(10*time.Minute, 1*time.Minute)
- }
- func SetMemCache(key string, value interface{}) {
- memCache.Set(key, value, cache.DefaultExpiration)
- }
- func GetMemCache(key string) (interface{}, error) {
- value, found := memCache.Get(key)
- if found {
- return value, nil
- }
- return nil, errors.New("Not Fount, key=" + key)
- }
- func GetMemCacheWithDelete(key string) (interface{}, error) {
- value, found := memCache.Get(key)
- if found {
- go memCache.Delete(key)
- return value, nil
- }
- return nil, errors.New("Not Fount, key=" + key)
- }
- func SetRspCode(url string, httpStatusCode int) {
- licenseCache.SetDefault(url, httpStatusCode)
- }
- func GetRspCode(url string) (int, bool) {
- if v, ok := licenseCache.Get(url); !ok {
- return 0, false
- } else {
- return v.(int), true
- }
- }
|