-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: activity functions #2
base: master
Are you sure you want to change the base?
Changes from 14 commits
bffca07
78ae7a2
85cadfb
7306f09
2c23802
93e8069
2dad571
86f701a
51dc4a2
24064cd
721d689
99975b4
96b4a99
9c0b187
1d4c51e
c6e3356
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.idea | ||
/config/conf.yaml | ||
/docs/思.md | ||
/test |
Large diffs are not rendered by default.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 从go语言的接口规范来看一般接口在调用方定义,因为不管底层实现如何,调用方需要的方法大多数情况只是实现的方法,这种在被调用方定义的方式耦合度比较高。可以看看去年陈诚分享的文章 |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,59 +2,51 @@ package cache | |||||||||||||||||||
|
||||||||||||||||||||
import ( | ||||||||||||||||||||
"context" | ||||||||||||||||||||
"github.com/raiki02/EG/internal/dao" | ||||||||||||||||||||
"github.com/raiki02/EG/internal/model" | ||||||||||||||||||||
"github.com/raiki02/EG/tools" | ||||||||||||||||||||
"github.com/redis/go-redis/v9" | ||||||||||||||||||||
"time" | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
// 先找缓存,缓存没有再找数据库 | ||||||||||||||||||||
// TODO: scpoe: 活动,帖子,jwt | ||||||||||||||||||||
type CacheHdl interface { | ||||||||||||||||||||
Get(ctx context.Context, key string) ([]model.Activity, error) | ||||||||||||||||||||
Set(ctx context.Context, key string, val []model.Activity) error | ||||||||||||||||||||
Del(ctx context.Context, key string) error | ||||||||||||||||||||
Get(context.Context, string) ([]interface{}, error) | ||||||||||||||||||||
Set(context.Context, string, []interface{}) error | ||||||||||||||||||||
Del(context.Context, string) error | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// 操作顺序 router -> controller -> cache -> dao | ||||||||||||||||||||
type Cache struct { | ||||||||||||||||||||
rdb *redis.Client | ||||||||||||||||||||
//操作评论的点赞和评论 | ||||||||||||||||||||
cd dao.CommentDAOHdl | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func NewCache(rdb *redis.Client, cd dao.CommentDAOHdl) CacheHdl { | ||||||||||||||||||||
return &Cache{rdb: rdb, cd: cd} | ||||||||||||||||||||
func NewCache(rdb *redis.Client) *Cache { | ||||||||||||||||||||
return &Cache{ | ||||||||||||||||||||
rdb: rdb, | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func (c *Cache) Get(ctx context.Context, key string) ([]model.Activity, error) { | ||||||||||||||||||||
key = "activity:" + key | ||||||||||||||||||||
func (c *Cache) Get(ctx context.Context, key string) ([]interface{}, error) { | ||||||||||||||||||||
b, err := c.rdb.Get(ctx, key).Bytes() | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return nil, err | ||||||||||||||||||||
} | ||||||||||||||||||||
return jsonTOstrs(b), nil | ||||||||||||||||||||
return jsonTOstrus(b), nil | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func (c *Cache) Set(ctx context.Context, key string, val []model.Activity) error { | ||||||||||||||||||||
key = "activity:" + key | ||||||||||||||||||||
return c.rdb.Set(ctx, key, strsTOjson(val), 48*time.Hour).Err() | ||||||||||||||||||||
func (c *Cache) Set(ctx context.Context, key string, val []interface{}) error { | ||||||||||||||||||||
return c.rdb.Set(ctx, key, Tojson(val), 48*time.Hour).Err() | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func (c *Cache) Del(ctx context.Context, key string) error { | ||||||||||||||||||||
return c.rdb.Del(ctx, key).Err() | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// redis的val不能存结构体,转换成json格式储存 | ||||||||||||||||||||
func strsTOjson(as []model.Activity) []byte { | ||||||||||||||||||||
return []byte(tools.Marshal(as)) | ||||||||||||||||||||
func Tojson(in []interface{}) []byte { | ||||||||||||||||||||
return []byte(tools.Marshal(in)) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func jsonTOstrs(b []byte) []model.Activity { | ||||||||||||||||||||
res := tools.Unmarshal(b, []model.Activity{}) | ||||||||||||||||||||
as, ok := res.([]model.Activity) | ||||||||||||||||||||
if !ok { | ||||||||||||||||||||
return nil | ||||||||||||||||||||
} | ||||||||||||||||||||
return as | ||||||||||||||||||||
func jsonTOstrus(b []byte) (out []interface{}) { | ||||||||||||||||||||
return nil | ||||||||||||||||||||
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deserialization function is incomplete. Example fix: func jsonTOstrus(b []byte) (out []interface{}) {
- return nil
+ err := json.Unmarshal(b, &out)
+ if err != nil {
+ return nil
+ }
+ return out
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid hardcoding database credentials.
The MySQL password should not be hardcoded in the source code. Instead, load it from environment variables or a secure configuration file.
Apply this diff to use environment variables:
Add these imports: