-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules_sql.go
42 lines (34 loc) · 919 Bytes
/
rules_sql.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
38
39
40
41
42
package sql
import (
"encoding/json"
"fmt"
"github.com/ZerNico/Maya/go_bot/modules/utils/caching"
"github.com/ZerNico/Maya/go_bot/modules/utils/error_handling"
)
type Rules struct {
ChatId string `gorm:"primary_key" json:"chat_id"`
Rules string `json:"rules"`
}
func GetChatRules(chatId string) *Rules {
ruleJson, err := caching.CACHE.Get(fmt.Sprintf("rules_%v", chatId))
if err != nil {
go cacheRules(chatId)
return nil
}
var rules *Rules
_ = json.Unmarshal(ruleJson, &rules)
return rules
}
func SetChatRules(chatId, rules string) {
defer func(chatId string) {
go cacheRules(chatId)
}(chatId)
SESSION.Save(&Rules{ChatId: chatId, Rules: rules})
}
func cacheRules(chatId string) {
rules := &Rules{}
SESSION.Where("chat_id = ?", chatId).Find(&rules)
ruleJson, _ := json.Marshal(&rules)
err := caching.CACHE.Set(fmt.Sprintf("rules_%v", chatId), ruleJson)
error_handling.HandleErr(err)
}