Skip to content

Instantly share code, notes, and snippets.

@AK-10
Created August 2, 2019 16:04
Show Gist options
  • Save AK-10/af1324c5c2fcfc37e27cbc7e5d28779e to your computer and use it in GitHub Desktop.
Save AK-10/af1324c5c2fcfc37e27cbc7e5d28779e to your computer and use it in GitHub Desktop.
import (
"errors"
"time"
"github.com/patrickmn/go-cache"
)
const (
messageNumPrefix = "MSG-NUM-"
)
var (
c = cache.New(5*time.Minute, 10*time.Minute)
)
// keyの生成関数を作って特定の値(userIDとか)用の関数にした方がいいかも(下に例あり)
func setValueToGC(key string, value interface{}) {
c.Set(key, value, cache.DefaultExpiration)
}
func getInt64FromGC(chanID int64) (int64, error) {
if lastID, found := c.Get(makeMSGNumKey(chanID)); found {
num := lastID.(int64) // 返したい型にキャストする(型変数使えたらもっと楽になる気がする)
return num, nil
}
return -1, errors.New("go-cache: key not found")
}
// key作成用関数
func makeMSGNumKey(chanID int64) string {
return messageNumPrefix + strconv.Itoa(int(chanID))
}
// メッセージ数キャッシュ用関数
func setMessageNumToGC(chanID, num int64) {
c.Set(makeMSGNumKey(chanID), num, cache.DefaultExpiration)
}
func getMessageNumFromGC(chanID int64) (int64, error) {
if lastID, found := c.Get(makeMSGNumKey(chanID)); found {
num := lastID.(int64)
return num, nil
}
return -1, errors.New("go-cache: key not found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment