Go实战全家桶之62:go小数据全缓存

用例

func (self *TestWebsiteUtmSuite) Test009_QueryAll2Cache() {
    var ret = apiservice.FindBeanQueryDomainCache().CacheListAll()
    golog.Info(ret)
}
func (self *TestWebsiteUtmSuite) Test010_ListAll2Cache2Domain() {
    var ret = apiservice.FindBeanQueryDomainCache().QueryOf("lpmto.ichub.com")
    var ret1 = apiservice.FindBeanQueryDomainCache().QueryOf("lpmto.ichub.com")

    golog.Info(ret, ret1, ret == ret1)
}
实现
package apiservice

import (
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/ichublog/golog"
    "git.ichub.com/general/webcli120/goweb/pagemodel"
    "git.ichub.com/general/webcli120/goweb/webclient/eswebclient/webfacade"
    "github.com/olivere/elastic/v7"
    "website-utm/domain/es/esentity/eswebsite"
)

type QueryDomainCache struct {
    basedto.BaseEntitySingle
    qq *webfacade.WebFacade[*eswebsite.ServiceDomainEs]
}

func NewQueryDomainCache() *QueryDomainCache {
    var qdc = &QueryDomainCache{}
    qdc.Init()
    return qdc

}

func (self *QueryDomainCache) Init() {
    self.qq = self.webFacade()
}

func (self *QueryDomainCache) QueryOfShopId(shopId int64) *pagemodel.IchubResult[*eswebsite.ServiceDomainEs] {

    return self.CacheListAllOf("shopId", shopId)

}
func (self *QueryDomainCache) QueryOf(domainName string) *pagemodel.IchubResult[*eswebsite.ServiceDomainEs] {

    return self.CacheListAllOf("domainName", domainName)

}
func (self *QueryDomainCache) webFacade() *webfacade.WebFacade[*eswebsite.ServiceDomainEs] {
    var q = elastic.NewBoolQuery()
    q.Filter(elastic.NewTermQuery("active", true))
    q.Filter(elastic.NewRangeQuery("website_id").Gt(0))
    var qq = eswebsite.NewWebFacadeServiceDomainEsOf(q)
    qq.SetSource("website_id,website_name,domain_name,shop_id")
    qq.SortOne("type", true)
    qq.LogInfoQuery(q)
    return qq
}
func (self *QueryDomainCache) CacheListAll() *pagemodel.PageResult[*eswebsite.ServiceDomainEs] {

    var ret = self.qq.CacheListAll()
    golog.Info(ret)
    return ret
}

// *webfacade.WebFacade[*ServiceDomainEs] {
func (self *QueryDomainCache) CacheListAllOf(field string, someValue any) *pagemodel.IchubResult[*eswebsite.ServiceDomainEs] {

    var ret = self.qq.CacheListAllOf(field, someValue)
    golog.Info(ret)
    return ret

}
func (self *WebFacade[T]) CacheListAllOf(fieldName string, value any) *pagemodel.IchubResult[T] {

    var result = pagemodel.DefaultIchubResult[T]()
    var ret = self.CacheListAll()
    result.Code = ret.Code
    result.Msg = ret.Msg
    if ret.ExistRecord() {
       for i := range ret.Data {
          if gconv.String(self.GetStruField(ret.Data[i], fieldName)) == gconv.String(value) {
             result.Data = ret.Data[i]
             result.Exist = true
             break
          }
       }
    }
    return result

}
func (self *WebFacade[T]) CacheListAll() *pagemodel.PageResult[T] {

    var ret, exist = escache.CacheGetOf[pagemodel.PageResult[T]](self.IndexKeyAll())
    if exist {
       return ret
    }
    ret = self.GeneralQueryMax()
    if ret.IsFailed() {
       golog.Error("ListAll2Cache ret:", ret)
    }
    if ret.ExistRecord() {
       escache.CacheSetOf(self.IndexKeyAll(), ret)
    }
    return ret
}
func (self *WebFacade[T]) GetStruField(some any, field string) any {

    // 使用反射获取字段值
    val := reflect.ValueOf(some)
    if val.Interface() == nil {
       golog.Error("someField value is nil", some)
       return "val is nil"
    }
    if val.Kind() == reflect.Ptr {
       val = val.Elem()
    }
    // 获取字段值
    return val.FieldByName(field).Interface()

}
cacheapi
package apicache

import (
    "git.ichub.com/general/webcli120/goconfig/base/fileutils"
    "git.ichub.com/general/webcli120/goconfig/gocache/ichubcache"
    "git.ichub.com/general/webcli120/goconfig/ichubconfig"
    "time"
)

const UtmCacheKey = "website-utm:"

var (
    expireTimeTest   = 10 * time.Minute
    expireTimeMaster = 30 * time.Minute

    ApiCache = ichubcache.DefaultOf(UtmCacheKey, GetExpireTime())
)

func GetExpireTime() time.Duration {
    var expireTime = expireTimeMaster
    if fileutils.IfWindows() {
       expireTime = 120 * time.Second
    } else if ichubconfig.IfNotMaster() {
       expireTime = expireTimeTest
    }

    return expireTime
}
func CacheGetOf[T any](key string) (*T, bool) {
    data, found := ichubcache.CacheGetOf[T](ApiCache, key)
    return data, found
}

func CacheSetOf[T any](key string, object *T) {
    ichubcache.CacheSetOf(ApiCache, key, object)
}
func CacheDeleteOf[T any](key string) {
    ichubcache.DeleteOf(ApiCache, key)
}

func CacheGetOfPage[T any](key string) (*T, bool) {
    data, found := ichubcache.CacheGetOf[T](ApiCache, "page:"+key)
    return data, found
}

func CacheSetOfPage[T any](key string, object *T) {
    ichubcache.CacheSetOf(ApiCache, "page:"+key, object)
}

func CacheGetOfDomainShop(key string) (*ShopDto, bool) {
    return ichubcache.CacheGetOf[ShopDto](ApiCache, "domainshop:"+key)

}

func CacheSetOfDomainShop(key string, shop *ShopDto) {
    if key != "" {
       ichubcache.CacheSetOf(ApiCache, "domainshop:"+key, shop)
    }
}
package ichubcache

import (
    "github.com/patrickmn/go-cache"

    "time"
)

var InstCache = NewCache()

func Delete(Key string) {
    InstCache.Delete(Key)

}
func DeleteOf(gc *IchubCache, Key string) {
    gc.Delete(Key)

}
func CacheGetOf[T any](gc *IchubCache, Key string) (*T, bool) {
    v, found := gc.Get(Key)
    if found {
       return v.(*T), found
    }
    return nil, false
}

func CacheSetOf[T any](gc *IchubCache, Key string, object *T) {
    gc.Set(Key, object, gc.ExpireTime)
}
func CacheGet[T any](Key string) (*T, bool) {

    return CacheGetOf[T](InstCache, Key)
}

func CacheSet[T any](Key string, object *T) {
    CacheSetOf(InstCache, Key, object)

}

type IchubCache struct {
    gocache     *cache.Cache
    Pkey        string
    ExpireTime  time.Duration
    ForceExpire bool
}

func (this *IchubCache) Gocache() *cache.Cache {
    return this.gocache
}

func (this *IchubCache) SetGocache(gocache *cache.Cache) {
    this.gocache = gocache
}

func Default() *IchubCache {
    return NewIchubCache("ichub:")
}
func DefaultOf(Pkey string, expireTime time.Duration) *IchubCache {
    var ic = &IchubCache{
       Pkey:       Pkey,
       ExpireTime: expireTime,
       // 默认过期时间10s;清理间隔30s,即每30s钟会自动清理过期的键值对
       gocache: cache.New(expireTime, cache_clean_time),
    }
    StoreCacheExpire(Pkey, ic)
    return ic
}
func ForceCacheExpire(Pkey string) {
    var ic = LoadCacheExpire(Pkey)
    ic.ForceExpire = true
    ic.gocache.Flush()
}

func NewCache() *IchubCache {
    return Default()
}
func NewIchubCache(Pkey string) *IchubCache {
    return &IchubCache{
       Pkey:       Pkey,
       ExpireTime: Cache_expire_time,
       // 默认过期时间10s;清理间隔30s,即每30s钟会自动清理过期的键值对
       gocache: cache.New(Cache_expire_time, cache_clean_time),
    }
}

func (this *IchubCache) Set(k string, x interface{}, d time.Duration) {
    this.gocache.Set(this.Pkey+k, x, d)
}

func (this *IchubCache) Get(k string) (interface{}, bool) {
    return this.gocache.Get(this.Pkey + k)
}
func (this *IchubCache) Delete(k string) {
    this.gocache.Delete(this.Pkey + k)
}
func (this *IchubCache) Flush() {
    this.gocache.Flush()
}

通用实现方案
package escache

import "git.ichub.com/general/webcli120/goconfig/gocache/ichubcache"

func CacheGetOf[T any]( Key string) (*T, bool) {
    return ichubcache.CacheGetOf[T](ichubcache.InstCache,Key)

}

func CacheSetOf[T any] (Key string, object *T) {

    ichubcache.CacheSetOf[T](ichubcache.InstCache,Key,object)
}
func (self *WebFacade[T]) ListAll2Cache() *pagemodel.PageResult[T] {

    var ret, exist = escache.CacheGetOf[pagemodel.PageResult[T]](self.IndexKeyAll())
    if exist {
       return ret
    }
    ret = self.GeneralQueryMax()
    if ret.IsFailed() {
       golog.Error("ListAll2Cache ret:", ret)
    }
    if ret.ExistRecord() {
       escache.CacheSetOf(self.IndexKeyAll(), ret)
    }
    return ret
}
func (self *WebFacade[T]) CacheFindId(id any) *pagemodel.IchubResult[T] {

    return self.CacheFindKey("id", id)
}
func (self *WebFacade[T]) CacheFindKey(key string, id any) *pagemodel.IchubResult[T] {
    var objectKey = self.IndexKeyId(id)
    var ret, exist = escache.CacheGetOf[pagemodel.IchubResult[T]](objectKey)
    if exist {
       return ret
    }
    ret = self.EsFindKeyId(key, id)
    if ret.IsFailed() {
       golog.Error("EsFindId2Cache ret:", ret)
    }
    if ret.ExistRecord() {
       escache.CacheSetOf(objectKey, ret)
    }
    return ret
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值