reduce store boiler plate (#7585)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
12501673d0
Коммит
363568b4eb
@@ -49,15 +49,9 @@ func (es SqlEmojiStore) CreateIndexesIfNotExists() {
|
||||
}
|
||||
|
||||
func (es SqlEmojiStore) Save(emoji *model.Emoji) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
emoji.PreSave()
|
||||
if result.Err = emoji.IsValid(); result.Err != nil {
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -66,28 +60,17 @@ func (es SqlEmojiStore) Save(emoji *model.Emoji) store.StoreChannel {
|
||||
} else {
|
||||
result.Data = emoji
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (es SqlEmojiStore) Get(id string, allowFromCache bool) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if allowFromCache {
|
||||
if cacheItem, ok := emojiCache.Get(id); ok {
|
||||
if es.metrics != nil {
|
||||
es.metrics.IncrementMemCacheHitCounter("Emoji")
|
||||
}
|
||||
result.Data = cacheItem.(*model.Emoji)
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
} else {
|
||||
if es.metrics != nil {
|
||||
@@ -118,20 +101,11 @@ func (es SqlEmojiStore) Get(id string, allowFromCache bool) store.StoreChannel {
|
||||
emojiCache.AddWithExpiresInSecs(id, emoji, EMOJI_CACHE_SEC)
|
||||
}
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (es SqlEmojiStore) GetByName(name string) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var emoji *model.Emoji
|
||||
|
||||
if err := es.GetReplica().SelectOne(&emoji,
|
||||
@@ -146,20 +120,11 @@ func (es SqlEmojiStore) GetByName(name string) store.StoreChannel {
|
||||
} else {
|
||||
result.Data = emoji
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (es SqlEmojiStore) GetList(offset, limit int) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var emoji []*model.Emoji
|
||||
|
||||
if _, err := es.GetReplica().Select(&emoji,
|
||||
@@ -174,20 +139,11 @@ func (es SqlEmojiStore) GetList(offset, limit int) store.StoreChannel {
|
||||
} else {
|
||||
result.Data = emoji
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
func (es SqlEmojiStore) Delete(id string, time int64) store.StoreChannel {
|
||||
storeChannel := make(store.StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := store.StoreResult{}
|
||||
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if sqlResult, err := es.GetMaster().Exec(
|
||||
`Update
|
||||
Emoji
|
||||
@@ -203,10 +159,5 @@ func (es SqlEmojiStore) Delete(id string, time int64) store.StoreChannel {
|
||||
}
|
||||
|
||||
emojiCache.Remove(id)
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user