diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-12-22 18:14:54 -0500 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2016-12-22 15:14:54 -0800 |
commit | ffd6ccde2ed4b1d374d0835c5638a189df60a679 (patch) | |
tree | 5b67ceff84678b04cdae08cef554086c4145324e /store/sql_post_store.go | |
parent | ca61b4bed94ea98530bf4f1b2f7585fe86ef80cd (diff) | |
download | chat-ffd6ccde2ed4b1d374d0835c5638a189df60a679.tar.gz chat-ffd6ccde2ed4b1d374d0835c5638a189df60a679.tar.bz2 chat-ffd6ccde2ed4b1d374d0835c5638a189df60a679.zip |
Adding caching of last 60 posts. (#4880)
Diffstat (limited to 'store/sql_post_store.go')
-rw-r--r-- | store/sql_post_store.go | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 9c1faf4fc..c1aaee3e6 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -21,12 +21,17 @@ type SqlPostStore struct { const ( LAST_POST_TIME_CACHE_SIZE = 25000 LAST_POST_TIME_CACHE_SEC = 900 // 15 minutes + + LAST_POSTS_CACHE_SIZE = 1000 + LAST_POSTS_CACHE_SEC = 900 // 15 minutes ) var lastPostTimeCache = utils.NewLru(LAST_POST_TIME_CACHE_SIZE) +var lastPostsCache = utils.NewLru(LAST_POSTS_CACHE_SIZE) func ClearPostCaches() { lastPostTimeCache.Purge() + lastPostsCache.Purge() } func NewSqlPostStore(sqlStore *SqlStore) PostStore { @@ -224,6 +229,7 @@ type etagPosts struct { func (s SqlPostStore) InvalidateLastPostTimeCache(channelId string) { lastPostTimeCache.Remove(channelId) + lastPostsCache.Remove(channelId) } func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) StoreChannel { @@ -381,11 +387,12 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) StoreChannel { return storeChannel } -func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreChannel { +func (s SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) go func() { result := StoreResult{} + metrics := einterfaces.GetMetricsInterface() if limit > 1000 { result.Err = model.NewLocAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_posts.app_error", nil, "channelId="+channelId) @@ -394,6 +401,27 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreCha return } + if allowFromCache && offset == 0 && limit == 60 { + if cacheItem, ok := lastPostsCache.Get(channelId); ok { + if metrics != nil { + metrics.IncrementMemCacheHitCounter("Last Posts Cache") + } + + result.Data = cacheItem.(*model.PostList) + storeChannel <- result + close(storeChannel) + return + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Last Posts Cache") + } + } + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Last Posts Cache") + } + } + rpc := s.getRootPosts(channelId, offset, limit) cpc := s.getParentsPosts(channelId, offset, limit) @@ -418,6 +446,10 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreCha list.MakeNonNil() + if offset == 0 && limit == 60 { + lastPostsCache.AddWithExpiresInSecs(channelId, list, LAST_POSTS_CACHE_SEC) + } + result.Data = list } |