summaryrefslogtreecommitdiffstats
path: root/store/sql_post_store.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2016-12-13 22:24:24 -0500
committerenahum <nahumhbl@gmail.com>2016-12-14 00:24:24 -0300
commit86fb0d87a3a09b237bec124ce0e74cd05aa164be (patch)
treedab6753888596cc2ba8df8c273ced364bff6b26d /store/sql_post_store.go
parent2e58f7504b79fc85d43468ffa06954a23221ea32 (diff)
downloadchat-86fb0d87a3a09b237bec124ce0e74cd05aa164be.tar.gz
chat-86fb0d87a3a09b237bec124ce0e74cd05aa164be.tar.bz2
chat-86fb0d87a3a09b237bec124ce0e74cd05aa164be.zip
Adding caching layer to some posts calls (#4779)
Diffstat (limited to 'store/sql_post_store.go')
-rw-r--r--store/sql_post_store.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/store/sql_post_store.go b/store/sql_post_store.go
index 900aeeeb6..44ae58b32 100644
--- a/store/sql_post_store.go
+++ b/store/sql_post_store.go
@@ -17,6 +17,17 @@ type SqlPostStore struct {
*SqlStore
}
+const (
+ POSTS_ETAG_CACHE_SIZE = 25000
+ POSTS_ETAG_CACHE_SEC = 900 // 15 minutes
+)
+
+var postEtagCache = utils.NewLru(CHANNEL_MEMBERS_COUNTS_CACHE_SIZE)
+
+func ClearPostCaches() {
+ postEtagCache.Purge()
+}
+
func NewSqlPostStore(sqlStore *SqlStore) PostStore {
s := &SqlPostStore{sqlStore}
@@ -210,12 +221,25 @@ type etagPosts struct {
UpdateAt int64
}
-func (s SqlPostStore) GetEtag(channelId string) StoreChannel {
+func (s SqlPostStore) InvalidatePostEtagCache(channelId string) {
+ postEtagCache.Remove(channelId)
+}
+
+func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
+ if allowFromCache {
+ if cacheItem, ok := postEtagCache.Get(channelId); ok {
+ result.Data = cacheItem.(string)
+ storeChannel <- result
+ close(storeChannel)
+ return
+ }
+ }
+
var et etagPosts
err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = :ChannelId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"ChannelId": channelId})
if err != nil {
@@ -224,6 +248,8 @@ func (s SqlPostStore) GetEtag(channelId string) StoreChannel {
result.Data = fmt.Sprintf("%v.%v.%v", model.CurrentVersion, et.Id, et.UpdateAt)
}
+ postEtagCache.AddWithExpiresInSecs(channelId, result.Data.(string), POSTS_ETAG_CACHE_SEC)
+
storeChannel <- result
close(storeChannel)
}()