Skip to content

Commit

Permalink
自动缓存工具
Browse files Browse the repository at this point in the history
  • Loading branch information
hwholiday committed Jun 18, 2021
1 parent 859aa3c commit 87bd078
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ require (
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/oauth2.v3 v3.12.0
gorm.io/driver/mysql v1.0.5
gorm.io/gorm v1.21.3
)

replace google.golang.org/grpc v1.35.0 => google.golang.org/grpc v1.26.0
52 changes: 44 additions & 8 deletions sql-redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"github.com/go-redis/redis"
"github.com/golang/groupcache/singleflight"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"time"
)
Expand All @@ -18,22 +20,55 @@ type cache struct {
g singleflight.Group
}

/*create table tests
(
id int auto_increment,
user_name varchar(20) null,
pwd varchar(30) null,
create_time bigint null,
update_time bigint null,
constraint test_pk
primary key (id)
);*/
//INSERT INTO test.tests (user_name, pwd, create_time, update_time) VALUES ('123123', '123123123', 123123123, 123123123);

type Test struct {
ID int `gorm:"primaryKey" json:"id"`
UserName string `json:"user_name"`
Pwd string `json:"pwd"`
CreateTime int64 `json:"create_time"`
UpdateTime int64 `json:"update_time"`
}

func main() {
rds := redis.NewClient(&redis.Options{
Addr: "172.12.12.165:6379",
})
sql := "root:+eN(2dFc5qu.@tcp(172.12.12.165:3306)/test?timeout=5s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8,utf8mb4"
db, err := gorm.Open(mysql.Open(sql), nil)
if err != nil {
panic(err)
}
var c = &cache{
rds: rds,
}
var str string
err := c.Take(&str, "user:info:2", func(v interface{}) error {
*v.(*string) = "我是你爸爸"
var test Test
if err = c.Take(&test, "user:info:2", func(v interface{}) error {
var t Test
if err = db.Model(&Test{}).Where("id = ?", 2).Last(&t).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errNotFind
} else {
return err
}
}
*v.(*Test) = t
return nil
})
if err != nil {
panic(err)
}); err != nil {
fmt.Println(err)
return
}
fmt.Println(str)
fmt.Println("test", test)
}

func (c *cache) Take(v interface{}, key string, query func(v interface{}) error) error {
Expand All @@ -55,6 +90,7 @@ func (c *cache) Take2(v interface{}, key string, query func(v interface{}) error
//err == errNotFind
//从DB里面读取
if err = query(v); err == errNotFind {
fmt.Println("query", err)
if err = c.Set(key, "*"); err != nil {
log.Println("set redis key : ", key, " err :", err.Error())
}
Expand Down Expand Up @@ -85,7 +121,7 @@ func (c *cache) Get(key string, v interface{}) error {
if err != nil {
return err
}
if data == "*" {
if data == "\"*\"" {
return errPlaceholder
}
if err = json.Unmarshal([]byte(data), v); err == nil {
Expand Down

0 comments on commit 87bd078

Please sign in to comment.