summaryrefslogtreecommitdiffstats
path: root/store/sql_channel_store.go
diff options
context:
space:
mode:
Diffstat (limited to 'store/sql_channel_store.go')
-rw-r--r--store/sql_channel_store.go126
1 files changed, 74 insertions, 52 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go
index eb150a63c..a860fea73 100644
--- a/store/sql_channel_store.go
+++ b/store/sql_channel_store.go
@@ -6,21 +6,26 @@ package store
import (
"database/sql"
+ l4g "github.com/alecthomas/log4go"
"github.com/go-gorp/gorp"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
)
const (
- MISSING_CHANNEL_ERROR = "store.sql_channel.get_by_name.missing.app_error"
- MISSING_CHANNEL_MEMBER_ERROR = "store.sql_channel.get_member.missing.app_error"
- CHANNEL_EXISTS_ERROR = "store.sql_channel.save_channel.exists.app_error"
+ MISSING_CHANNEL_ERROR = "store.sql_channel.get_by_name.missing.app_error"
+ MISSING_CHANNEL_MEMBER_ERROR = "store.sql_channel.get_member.missing.app_error"
+ CHANNEL_EXISTS_ERROR = "store.sql_channel.save_channel.exists.app_error"
+ ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE = model.SESSION_CACHE_SIZE
+ ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SEC = 900 // 15 mins
)
type SqlChannelStore struct {
*SqlStore
}
+var allChannelMembersForUserCache *utils.Cache = utils.NewLru(ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE)
+
func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore {
s := &SqlChannelStore{sqlStore}
@@ -517,6 +522,8 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel {
}
}
+ s.InvalidateAllChannelMembersForUser(member.UserId)
+
storeChannel <- result
close(storeChannel)
}()
@@ -619,6 +626,33 @@ func (s SqlChannelStore) GetMember(channelId string, userId string) StoreChannel
return storeChannel
}
+func (us SqlChannelStore) InvalidateAllChannelMembersForUser(userId string) {
+ allChannelMembersForUserCache.Remove(userId)
+}
+
+func (us SqlChannelStore) IsUserInChannelUseCache(userId string, channelId string) bool {
+ if cacheItem, ok := allChannelMembersForUserCache.Get(userId); ok {
+ ids := cacheItem.(map[string]string)
+ if _, ok := ids[channelId]; ok {
+ return true
+ } else {
+ return false
+ }
+ }
+
+ if result := <-us.GetAllChannelMembersForUser(userId, true); result.Err != nil {
+ l4g.Error("SqlChannelStore.IsUserInChannelUseCache: " + result.Err.Error())
+ return false
+ } else {
+ ids := result.Data.(map[string]string)
+ if _, ok := ids[channelId]; ok {
+ return true
+ } else {
+ return false
+ }
+ }
+}
+
func (s SqlChannelStore) GetMemberForPost(postId string, userId string) StoreChannel {
storeChannel := make(StoreChannel, 1)
@@ -649,26 +683,43 @@ func (s SqlChannelStore) GetMemberForPost(postId string, userId string) StoreCha
return storeChannel
}
-func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel {
+type allChannelMember struct {
+ ChannelId string
+ Roles string
+}
+
+func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCache bool) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
- count, err := s.GetReplica().SelectInt(`
- SELECT
- count(*)
- FROM
- ChannelMembers,
- Users
- WHERE
- ChannelMembers.UserId = Users.Id
- AND ChannelMembers.ChannelId = :ChannelId
- AND Users.DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId})
+ if allowFromCache {
+ if cacheItem, ok := allChannelMembersForUserCache.Get(userId); ok {
+ result.Data = cacheItem.(map[string]string)
+ storeChannel <- result
+ close(storeChannel)
+ return
+ }
+ }
+
+ var data []allChannelMember
+ _, err := s.GetReplica().Select(&data, "SELECT ChannelId, Roles FROM Channels, ChannelMembers WHERE Channels.Id = ChannelMembers.ChannelId AND ChannelMembers.UserId = :UserId AND Channels.DeleteAt = 0", map[string]interface{}{"UserId": userId})
+
if err != nil {
- result.Err = model.NewLocAppError("SqlChannelStore.GetMemberCount", "store.sql_channel.get_member_count.app_error", nil, "channel_id="+channelId+", "+err.Error())
+ result.Err = model.NewLocAppError("SqlChannelStore.GetAllChannelMembersForUser", "store.sql_channel.get_channels.get.app_error", nil, "userId="+userId+", err="+err.Error())
} else {
- result.Data = count
+
+ ids := make(map[string]string)
+ for i := range data {
+ ids[data[i].ChannelId] = data[i].Roles
+ }
+
+ result.Data = ids
+
+ if allowFromCache {
+ allChannelMembersForUserCache.AddWithExpiresInSecs(userId, ids, ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SEC)
+ }
}
storeChannel <- result
@@ -678,55 +729,26 @@ func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel {
return storeChannel
}
-func (s SqlChannelStore) GetExtraMembers(channelId string, limit int) StoreChannel {
+func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
- var members []model.ExtraMember
- var err error
-
- if limit != -1 {
- _, err = s.GetReplica().Select(&members, `
- SELECT
- Id,
- Nickname,
- Email,
- ChannelMembers.Roles,
- Username
- FROM
- ChannelMembers,
- Users
- WHERE
- ChannelMembers.UserId = Users.Id
- AND Users.DeleteAt = 0
- AND ChannelId = :ChannelId
- LIMIT :Limit`, map[string]interface{}{"ChannelId": channelId, "Limit": limit})
- } else {
- _, err = s.GetReplica().Select(&members, `
+ count, err := s.GetReplica().SelectInt(`
SELECT
- Id,
- Nickname,
- Email,
- ChannelMembers.Roles,
- Username
+ count(*)
FROM
ChannelMembers,
Users
WHERE
ChannelMembers.UserId = Users.Id
- AND Users.DeleteAt = 0
- AND ChannelId = :ChannelId`, map[string]interface{}{"ChannelId": channelId})
- }
-
+ AND ChannelMembers.ChannelId = :ChannelId
+ AND Users.DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
- result.Err = model.NewLocAppError("SqlChannelStore.GetExtraMembers", "store.sql_channel.get_extra_members.app_error", nil, "channel_id="+channelId+", "+err.Error())
+ result.Err = model.NewLocAppError("SqlChannelStore.GetMemberCount", "store.sql_channel.get_member_count.app_error", nil, "channel_id="+channelId+", "+err.Error())
} else {
- for i := range members {
- members[i].Sanitize(utils.Cfg.GetSanitizeOptions())
- }
- result.Data = members
+ result.Data = count
}
storeChannel <- result