forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
37 lines (31 loc) · 952 Bytes
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package manager
import (
"chat/globals"
"chat/utils"
"fmt"
"github.com/gin-gonic/gin"
"time"
)
type CacheProps struct {
Message []globals.Message `json:"message" required:"true"`
Model string `json:"model" required:"true"`
Reversible bool `json:"reversible"`
}
type CacheData struct {
Message string `json:"message"`
}
func ExtractCacheData(c *gin.Context, props *CacheProps) *CacheData {
hash := utils.Md5Encrypt(utils.Marshal(props))
data, err := utils.GetCacheFromContext(c).Get(c, fmt.Sprintf(":niodata:%s", hash)).Result()
if err == nil && data != "" {
return utils.UnmarshalForm[CacheData](data)
}
return nil
}
func SaveCacheData(c *gin.Context, props *CacheProps, data *CacheData) {
if !globals.IsFreeModel(props.Model) {
return
}
hash := utils.Md5Encrypt(utils.Marshal(props))
utils.GetCacheFromContext(c).Set(c, fmt.Sprintf(":niodata:%s", hash), utils.Marshal(data), time.Hour*12)
}