-
Notifications
You must be signed in to change notification settings - Fork 701
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(freecache): v2 finish #702
Conversation
pkg/cache/xfreecache/v2/config.go
Outdated
) | ||
|
||
// DefaultConfig 返回默认配置 缓存容量256MB | ||
func DefaultConfig() Config { |
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.
Config统一都用*Config
pkg/cache/xfreecache/v2/config.go
Outdated
} | ||
|
||
// SetName 设置缓存名称 | ||
func (c Config) SetName(name string) Config { |
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.
这些Set方法可以先去掉,变量已经export出来了,后面有必要再加呢?
} | ||
|
||
type localStorage struct { | ||
req Config |
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.
req->config
} | ||
|
||
func Test_cache_GetAndSetCacheData(t *testing.T) { | ||
oneCache := NewLocalCache[string, Student](DefaultConfig(). |
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.
有没有可能把API设计为DefaultConfig().Build()? 和其他组件的API保持一致
|
||
// ParseSize parses a size string. | ||
// Valid time units are "b" (or "byte"), "kb", "mb", "gb", "tb". | ||
func ParseSize(s string) (Size, error) { |
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.
这个方法需要补充单测,或者考虑用其他的开源库也可以
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.
pkg/cache/xfreecache/v2/config.go
Outdated
func DefaultConfig(size Size) *Config { | ||
once.Do(func() { | ||
if size < 512*KB { | ||
size = 256 * MB |
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.
也可以限制下最大内存
pkg/cache/xfreecache/v2/config.go
Outdated
if size < 512*KB { | ||
size = 256 * MB | ||
} | ||
innerCache = freecache.NewCache(int(size)) |
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.
这个NewCache可以在解析完配置之后再做,DefaultConfig只设置默认配置
pkg/cache/xfreecache/v2/cache.go
Outdated
if val, ok := v[id]; ok { | ||
cacheData = val | ||
} | ||
data, innerErr := json.Marshal(cacheData) |
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.
这个是否判断下,如果是proto.Message,就用proto来编解码
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.
可以加个benchmark对比下proto和json两种对象的性能
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## master #702 +/- ##
==========================================
- Coverage 53.33% 53.33% -0.01%
==========================================
Files 191 191
Lines 10545 10547 +2
==========================================
+ Hits 5624 5625 +1
Misses 4498 4498
- Partials 423 424 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
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.
可以统一格式化下import的路径,然后我这边就没问题了
pkg/cache/xfreecache/v2/cache.go
Outdated
|
||
// id去重 | ||
ids = lo.Uniq(ids) | ||
idsNone := make([]K, 0) |
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.
这里可以考虑预分配len(ids)的内存
pkg/cache/xfreecache/v2/cache.go
Outdated
cacheKey := c.getKey(key, id) | ||
if resT, innerErr := c.GetCacheData(cacheKey); innerErr == nil && resT != nil { | ||
var value V | ||
if msg, ok := any(value).(proto.Message); ok { // Constrained to proto.Message |
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.
可以考虑封装下unmarshal和marshal的方法,参数是any
1、格式化import的路径 2、预分配len(ids)的内存 3、封装unmarshal和marshal方法
补充bench test
add sync.Pool
add sync.Pool
change interface{} to any
pkg/cache/xfreecache/v2/encode.go
Outdated
msgType := reflect.TypeOf(msg).Elem() | ||
|
||
// Make a new one, and throw it back into T | ||
msg = reflect.New(msgType).Interface().(proto.Message) |
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.
这里可以用sync.Pool缓存一下,对比看看
Describe what this PR does / why we need it
Does this pull request fix one issue?
Describe how you did it
Describe how to verify it
Special notes for reviews