濮阳杆衣贸易有限公司

主頁 > 知識庫 > 在 Golang 中實(shí)現(xiàn) Cache::remember 方法詳解

在 Golang 中實(shí)現(xiàn) Cache::remember 方法詳解

熱門標(biāo)簽:鄭州智能語音電銷機(jī)器人價格 上海極信防封電銷卡價格 湛江crm外呼系統(tǒng)排名 重慶慶云企業(yè)400電話到哪申請 仙桃400電話辦理 地圖標(biāo)注免費(fèi)定制店 不封卡外呼系統(tǒng) 寧波語音外呼系統(tǒng)公司 宿遷便宜外呼系統(tǒng)代理商

項目需要把部分代碼移植到 Golang , 之前用 Laravel 封裝的寫起來很舒服,在 Golang 里只能自動動手實(shí)現(xiàn).
一開始想的是使用 interface 實(shí)現(xiàn),但是遇到了一個坑, Golang 里的組合是一個虛假的繼承

package main
 
import "fmt"
 
type Person interface {
 Say()
 Name()
}
 
type Parent struct {
}
 
func (s *Parent) Say() {
 fmt.Println("i am " + s.Name())
}
 
func (s *Parent) Name() string {
 return "parent"
}
 
type Child struct {
 Parent
}
 
func (s *Child) Name() string {
 return "child"
}
 
type Child1 struct {
 Parent
}
 
func main() {
 
 var c Child
 // i am parent
 c.Say()
 
 var c1 Child1
 // i am parent
 c1.Say()
}
  • 如上 c.say() 代碼,在別的語言里應(yīng)該是輸出 i am child 才對, 而 Golang 不一樣,查了一下 Golang 的資料才能理解 https://golang.org/ref/spec#Selectors
  • 大致意思是說,通過 x.f 調(diào)用 f 方法或者屬性時,從當(dāng)前或者嵌套匿名結(jié)構(gòu)體由淺到深的去調(diào)用,而不會去尋找上級
  • 比如 child1 沒有 Say 方法,會進(jìn)入到匿名結(jié)構(gòu)體 Parent 找到 Say 方法,然后調(diào)用
  • 而 child 也沒有 Say 方法,同樣去調(diào)用 Parent 的 Say 方法,這時候 Say 是通過 Parent 調(diào)用的, 當(dāng)在 Say 里調(diào)用 s.Name 方法,并不能找到 child , 所以還是會調(diào)用到 Parent 的 Name 方法
  • 然后自己整理和同事一起寫了大致的 remember 方法
import (
 "context"
 "encoding/json"
 "fmt"
 "github.com/gin-gonic/gin"
 "time"
)
 
// redis 操作已經(jīng)簡化
func CacheGet(c context.Context, t interface{}, cacheKey string, callQuery func() error) error {
 
 // 此處通過 redis 獲取數(shù)據(jù), 如果存在數(shù)據(jù), 那么直接返回
 dataBytes, err := redis.Get(c, cacheKey).Bytes()
 if err == nil {
  if err := json.Unmarshal(dataBytes, t); err == nil {
   return nil
  }
 }
 
 // 當(dāng) redis 沒有數(shù)據(jù), 那么調(diào)用此方法修改 t,
 if err := callQuery(); err != nil {
 
  return err
 }
 
 // 這里把修改之后的 t 存儲到 redis, 下次使用便可以使用緩存
 dataBytes, err = json.Marshal(t)
 if err == nil {
  redis.Set(c, cacheKey, dataBytes, time.Minute*30)
 }
 return nil
}
 
func handle(c *gin.Context) {
 
 var model models.User
 err := utils.CacheGet(
  c.Request.Context(),
  model,
  fmt.Sprintf("cache_xxx:%s", c.Param("id")),
  func() error {
 
   return db.First(model)
  },
 )
}

到此這篇關(guān)于在 Golang 中實(shí)現(xiàn) Cache::remember 方法的文章就介紹到這了,更多相關(guān)Golang實(shí)現(xiàn) Cache::remember 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:電子產(chǎn)品 遼寧 物業(yè)服務(wù) 青海 海南 儋州 西雙版納 安康

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在 Golang 中實(shí)現(xiàn) Cache::remember 方法詳解》,本文關(guān)鍵詞  在,Golang,中,實(shí)現(xiàn),Cache,remember,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在 Golang 中實(shí)現(xiàn) Cache::remember 方法詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于在 Golang 中實(shí)現(xiàn) Cache::remember 方法詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    寻甸| 油尖旺区| 黔江区| 临潭县| 磐石市| 于田县| 临颍县| 饶平县| 潞西市| 克拉玛依市| 宣威市| 南岸区| 广宁县| 安丘市| 宣恩县| 新晃| 周至县| 衡阳县| 峡江县| 镇宁| 电白县| 沂水县| 新泰市| 太原市| 西宁市| 永春县| 玛沁县| 浙江省| 白山市| 赣州市| 崇礼县| 临邑县| 台州市| 叶城县| 吉安县| 盐津县| 宁国市| 昭苏县| 乡宁县| 那坡县| 赣州市|