Pacific73 / gorm-cache

gorm v2的即插即用、无需修改代码的旁路缓存。An easy-to-use look-aside cache solution for gorm v2 users.
Apache License 2.0
111 stars 29 forks source link

并发情况下处理耗时慢慢累积,最终超时 #29

Closed fonoisrev closed 10 months ago

fonoisrev commented 11 months ago

版本v1.1.0

使用内存缓存,如下配置

       cache.NewGorm2Cache(&config.CacheConfig{
        CacheLevel:           config.CacheLevelAll,
        CacheStorage:         config.CacheStorageMemory,
        InvalidateWhenUpdate: true,  // when you create/update/delete objects, invalidate cache
        CacheTTL:             10000, // 10s
        CacheSize:            5000,
        // exceeds this number, then don't cache
    }

AfterUpdate方法耗时会慢慢累积,最终会导致数据读写操作均超时

以下是go trace看到的结果

首先可以看到AfterUpdate是耗时大户

image

大量的goroutine在AfterUpdate

image

scheduler wait

image

可以看到阻塞在写channel

image

对应的代码

image

这里为什么采用deletables chan *Item这样的设计?

asjdf commented 11 months ago

实际上你已经跟到了ccache包里了,不是这个包的问题。另外这个往channel加是为了把链表里的item删掉

Pacific73 commented 11 months ago

感谢楼上asjdf的评论,如果是因为ccache包的问题,您可以自行fork替换一下cache层的实现。我因为日常工作繁忙,投入时间不会很多。欢迎提交pr!