github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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 "strconv" 8 "strings" 9 10 "github.com/vnforks/kid/v5/model" 11 "github.com/vnforks/kid/v5/store" 12 13 "fmt" 14 ) 15 16 type LocalCachePostStore struct { 17 store.PostStore 18 rootStore *LocalCacheStore 19 } 20 21 func (s *LocalCachePostStore) handleClusterInvalidateLastPostTime(msg *model.ClusterMessage) { 22 if msg.Data == CLEAR_CACHE_MESSAGE_DATA { 23 s.rootStore.lastPostTimeCache.Purge() 24 } else { 25 s.rootStore.lastPostTimeCache.Remove(msg.Data) 26 } 27 } 28 29 func (s *LocalCachePostStore) handleClusterInvalidateLastPosts(msg *model.ClusterMessage) { 30 if msg.Data == CLEAR_CACHE_MESSAGE_DATA { 31 s.rootStore.postLastPostsCache.Purge() 32 } else { 33 s.rootStore.postLastPostsCache.Remove(msg.Data) 34 } 35 } 36 37 func (s LocalCachePostStore) ClearCaches() { 38 s.rootStore.doClearCacheCluster(s.rootStore.lastPostTimeCache) 39 s.rootStore.doClearCacheCluster(s.rootStore.postLastPostsCache) 40 s.PostStore.ClearCaches() 41 42 if s.rootStore.metrics != nil { 43 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Post Time - Purge") 44 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Posts Cache - Purge") 45 } 46 } 47 48 func (s LocalCachePostStore) InvalidateLastPostTimeCache(classId string) { 49 s.rootStore.doInvalidateCacheCluster(s.rootStore.lastPostTimeCache, classId) 50 51 // Keys are "{classid}{limit}" and caching only occurs on limits of 30 and 60 52 s.rootStore.doInvalidateCacheCluster(s.rootStore.postLastPostsCache, classId+"30") 53 s.rootStore.doInvalidateCacheCluster(s.rootStore.postLastPostsCache, classId+"60") 54 55 s.PostStore.InvalidateLastPostTimeCache(classId) 56 57 if s.rootStore.metrics != nil { 58 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Post Time - Remove by Class Id") 59 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Last Posts Cache - Remove by Class Id") 60 } 61 } 62 63 func (s LocalCachePostStore) GetEtag(classId string, allowFromCache bool) string { 64 if allowFromCache { 65 if lastTime := s.rootStore.doStandardReadCache(s.rootStore.lastPostTimeCache, classId); lastTime != nil { 66 return fmt.Sprintf("%v.%v", model.CurrentVersion, lastTime.(int64)) 67 } 68 } 69 70 result := s.PostStore.GetEtag(classId, allowFromCache) 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, classId, lastTime) 77 78 return result 79 } 80 81 func (s LocalCachePostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFromCache bool) (*model.PostList, *model.AppError) { 82 if allowFromCache { 83 // If the last post in the class's time is less than or equal to the time we are getting posts since, 84 // we can safely return no posts. 85 if lastTime := s.rootStore.doStandardReadCache(s.rootStore.lastPostTimeCache, options.ClassId); lastTime != nil && lastTime.(int64) <= options.Time { 86 list := model.NewPostList() 87 return list, nil 88 } 89 } 90 91 list, err := s.PostStore.GetPostsSince(options, allowFromCache) 92 93 latestUpdate := options.Time 94 if err == nil { 95 for _, p := range list.ToSlice() { 96 if latestUpdate < p.UpdateAt { 97 latestUpdate = p.UpdateAt 98 } 99 } 100 s.rootStore.doStandardAddToCache(s.rootStore.lastPostTimeCache, options.ClassId, latestUpdate) 101 } 102 103 return list, err 104 } 105 106 func (s LocalCachePostStore) GetPosts(options model.GetPostsOptions, allowFromCache bool) (*model.PostList, *model.AppError) { 107 if !allowFromCache { 108 return s.PostStore.GetPosts(options, allowFromCache) 109 } 110 111 offset := options.PerPage * options.Page 112 // Caching only occurs on limits of 30 and 60, the common limits requested by MM clients 113 if offset == 0 && (options.PerPage == 60 || options.PerPage == 30) { 114 if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.postLastPostsCache, fmt.Sprintf("%s%v", options.ClassId, options.PerPage)); cacheItem != nil { 115 return cacheItem.(*model.PostList), nil 116 } 117 } 118 119 list, err := s.PostStore.GetPosts(options, false) 120 if err != nil { 121 return nil, err 122 } 123 124 // Caching only occurs on limits of 30 and 60, the common limits requested by MM clients 125 if offset == 0 && (options.PerPage == 60 || options.PerPage == 30) { 126 s.rootStore.doStandardAddToCache(s.rootStore.postLastPostsCache, fmt.Sprintf("%s%v", options.ClassId, options.PerPage), list) 127 } 128 129 return list, err 130 }