github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/store/localcachelayer/post_layer.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package localcachelayer 5 6 import ( 7 "fmt" 8 "strconv" 9 "strings" 10 11 "github.com/mattermost/mattermost-server/v5/model" 12 "github.com/mattermost/mattermost-server/v5/store" 13 ) 14 15 type LocalCachePostStore struct { 16 store.PostStore 17 rootStore *LocalCacheStore 18 } 19 20 func (s *LocalCachePostStore) handleClusterInvalidateLastPostTime(msg *model.ClusterMessage) { 21 if msg.Data == ClearCacheMessageData { 22 s.rootStore.lastPostTimeCache.Purge() 23 } else { 24 s.rootStore.lastPostTimeCache.Remove(msg.Data) 25 } 26 } 27 28 func (s *LocalCachePostStore) handleClusterInvalidateLastPosts(msg *model.ClusterMessage) { 29 if msg.Data == ClearCacheMessageData { 30 s.rootStore.postLastPostsCache.Purge() 31 } else { 32 s.rootStore.postLastPostsCache.Remove(msg.Data) 33 } 34 } 35 36 func (s LocalCachePostStore) ClearCaches() { 37 s.rootStore.doClearCacheCluster(s.rootStore.lastPostTimeCache) 38 s.rootStore.doClearCacheCluster(s.rootStore.postLastPostsCache) 39 s.PostStore.ClearCaches() 40 41 if s.rootStore.metrics != nil { 42 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Post Time - Purge") 43 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Posts Cache - Purge") 44 } 45 } 46 47 func (s LocalCachePostStore) InvalidateLastPostTimeCache(channelId string) { 48 s.rootStore.doInvalidateCacheCluster(s.rootStore.lastPostTimeCache, channelId) 49 50 // Keys are "{channelid}{limit}" and caching only occurs on limits of 30 and 60 51 s.rootStore.doInvalidateCacheCluster(s.rootStore.postLastPostsCache, channelId+"30") 52 s.rootStore.doInvalidateCacheCluster(s.rootStore.postLastPostsCache, channelId+"60") 53 54 s.PostStore.InvalidateLastPostTimeCache(channelId) 55 56 if s.rootStore.metrics != nil { 57 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Post Time - Remove by Channel Id") 58 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Posts Cache - Remove by Channel Id") 59 } 60 } 61 62 func (s LocalCachePostStore) GetEtag(channelId string, allowFromCache, collapsedThreads bool) string { 63 if allowFromCache { 64 var lastTime int64 65 if err := s.rootStore.doStandardReadCache(s.rootStore.lastPostTimeCache, channelId, &lastTime); err == nil { 66 return fmt.Sprintf("%v.%v", model.CurrentVersion, lastTime) 67 } 68 } 69 70 result := s.PostStore.GetEtag(channelId, allowFromCache, collapsedThreads) 71 72 splittedResult := strings.Split(result, ".") 73 74 lastTime, _ := strconv.ParseInt((splittedResult[len(splittedResult)-1]), 10, 64) 75 76 s.rootStore.doStandardAddToCache(s.rootStore.lastPostTimeCache, channelId, lastTime) 77 78 return result 79 } 80 81 func (s LocalCachePostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFromCache bool) (*model.PostList, error) { 82 if allowFromCache { 83 // If the last post in the channel's time is less than or equal to the time we are getting posts since, 84 // we can safely return no posts. 85 var lastTime int64 86 if err := s.rootStore.doStandardReadCache(s.rootStore.lastPostTimeCache, options.ChannelId, &lastTime); err == nil && lastTime <= options.Time { 87 list := model.NewPostList() 88 return list, nil 89 } 90 } 91 92 list, err := s.PostStore.GetPostsSince(options, allowFromCache) 93 94 latestUpdate := options.Time 95 if err == nil { 96 for _, p := range list.ToSlice() { 97 if latestUpdate < p.UpdateAt { 98 latestUpdate = p.UpdateAt 99 } 100 } 101 s.rootStore.doStandardAddToCache(s.rootStore.lastPostTimeCache, options.ChannelId, latestUpdate) 102 } 103 104 return list, err 105 } 106 107 func (s LocalCachePostStore) GetPosts(options model.GetPostsOptions, allowFromCache bool) (*model.PostList, error) { 108 if !allowFromCache { 109 return s.PostStore.GetPosts(options, allowFromCache) 110 } 111 112 offset := options.PerPage * options.Page 113 // Caching only occurs on limits of 30 and 60, the common limits requested by MM clients 114 if offset == 0 && (options.PerPage == 60 || options.PerPage == 30) { 115 var cacheItem *model.PostList 116 if err := s.rootStore.doStandardReadCache(s.rootStore.postLastPostsCache, fmt.Sprintf("%s%v", options.ChannelId, options.PerPage), &cacheItem); err == nil { 117 return cacheItem, nil 118 } 119 } 120 121 list, err := s.PostStore.GetPosts(options, false) 122 if err != nil { 123 return nil, err 124 } 125 126 // Caching only occurs on limits of 30 and 60, the common limits requested by MM clients 127 if offset == 0 && (options.PerPage == 60 || options.PerPage == 30) { 128 s.rootStore.doStandardAddToCache(s.rootStore.postLastPostsCache, fmt.Sprintf("%s%v", options.ChannelId, options.PerPage), list) 129 } 130 131 return list, err 132 }