[GH-13074] Migrate TermsOfServiceCache from store/sqlstore/terms_of_service.go to the new store/localcachelayer (#13205)

* migrated termsOfServiceCache from sqlstore to localcachelayer, and mocked store tests

* revert gitignore local change

* fixed caching in termsOfService Get, and added basic tests for termsOfServiceCache

* added a test for cache save, and fixed call to Store tests

* fixed GeLatest termsOfService from cache test

* added license headers to terms_of_service cache files

* using doStandardReadFromCache and doStandardAddToCache when reading and writing to cache

* removed unused variable, termsOfServiceCacheName

* added special key for the latest termsOfService value in termsOfServiceCache

* updated license information on localcachelayer termsOfServiceCache files

* fixed not updating latest termsOfServiceCache on Get by ID, and invalidating cache cluster on termsOfServiceCache save

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Sanele T. Mahlalela
2019-12-20 14:51:54 +02:00
коммит произвёл Jesús Espino
родитель f41c90657e
Коммит de9a0197e2
6 изменённых файлов: 231 добавлений и 45 удалений

Просмотреть файл

@@ -10,7 +10,6 @@ import (
"github.com/mattermost/mattermost-server/v5/einterfaces"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/mattermost-server/v5/utils"
)
type SqlTermsOfServiceStore struct {
@@ -18,12 +17,6 @@ type SqlTermsOfServiceStore struct {
metrics einterfaces.MetricsInterface
}
var termsOfServiceCache = utils.NewLru(model.TERMS_OF_SERVICE_CACHE_SIZE)
const (
termsOfServiceCacheName = "TermsOfServiceStore"
)
func NewSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.TermsOfServiceStore {
s := SqlTermsOfServiceStore{sqlStore, metrics}
@@ -55,28 +48,10 @@ func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*mod
return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service.save.app_error", nil, "terms_of_service_id="+termsOfService.Id+",err="+err.Error(), http.StatusInternalServerError)
}
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
if allowFromCache {
if termsOfServiceCache.Len() != 0 {
if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
return cacheItem.(*model.TermsOfService), nil
}
}
}
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
var termsOfService *model.TermsOfService
err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1")
@@ -87,28 +62,10 @@ func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfSe
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
if allowFromCache {
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
}
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
if allowFromCache {
if termsOfServiceCache.Len() != 0 {
if cacheItem, ok := termsOfServiceCache.Get(id); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
return cacheItem.(*model.TermsOfService), nil
}
}
}
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
obj, err := s.GetReplica().Get(model.TermsOfService{}, id)
if err != nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)