migrate direct usage of lru to cache2 (#14508)

Automatic Merge
Этот коммит содержится в:
Siyuan Liu
2020-06-02 09:01:30 -04:00
коммит произвёл GitHub
родитель 18cd3a1d07
Коммит 6a5dd550c8
4 изменённых файлов: 71 добавлений и 55 удалений

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

@@ -5,14 +5,17 @@ package api4
import (
"net/http"
"time"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
"github.com/mattermost/mattermost-server/v5/services/cache2"
)
const OPEN_GRAPH_METADATA_CACHE_SIZE = 10000
var openGraphDataCache = lru.New(OPEN_GRAPH_METADATA_CACHE_SIZE)
var openGraphDataCache = cache2.NewLRU(&cache2.LRUOptions{
Size: OPEN_GRAPH_METADATA_CACHE_SIZE,
})
func (api *API) InitOpenGraph() {
api.BaseRoutes.OpenGraph.Handle("", api.ApiSessionRequired(getOpenGraphMetadata)).Methods("POST")
@@ -43,15 +46,16 @@ func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
ogJSONGeneric, ok := openGraphDataCache.Get(url)
if ok {
w.Write(ogJSONGeneric.([]byte))
var ogJSONGeneric []byte
err := openGraphDataCache.Get(url, &ogJSONGeneric)
if err == nil {
w.Write(ogJSONGeneric)
return
}
og := c.App.GetOpenGraphMetadata(url)
ogJSON, err := og.ToJSON()
openGraphDataCache.AddWithExpiresInSecs(url, ogJSON, 3600) // Cache would expire after 1 hour
openGraphDataCache.SetWithExpiry(url, ogJSON, 1*time.Hour)
if err != nil {
w.Write([]byte(`{"url": ""}`))
return

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

@@ -16,7 +16,7 @@ import (
"github.com/mattermost/mattermost-server/v5/audit"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
"github.com/mattermost/mattermost-server/v5/services/cache2"
"github.com/mattermost/mattermost-server/v5/services/filesstore"
)
@@ -26,7 +26,9 @@ const (
MAX_SERVER_BUSY_SECONDS = 86400
)
var redirectLocationDataCache = lru.New(REDIRECT_LOCATION_CACHE_SIZE)
var redirectLocationDataCache = cache2.NewLRU(&cache2.LRUOptions{
Size: REDIRECT_LOCATION_CACHE_SIZE,
})
func (api *API) InitSystem() {
api.BaseRoutes.System.Handle("/ping", api.ApiHandler(getSystemPing)).Methods("GET")
@@ -405,8 +407,9 @@ func getRedirectLocation(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if location, ok := redirectLocationDataCache.Get(url); ok {
m["location"] = location.(string)
var location string
if err := redirectLocationDataCache.Get(url, &location); err == nil {
m["location"] = location
w.Write([]byte(model.MapToJson(m)))
return
}
@@ -419,7 +422,7 @@ func getRedirectLocation(c *Context, w http.ResponseWriter, r *http.Request) {
res, err := client.Head(url)
if err != nil {
// Cache failures to prevent retries.
redirectLocationDataCache.AddWithExpiresInSecs(url, "", 3600) // Expires after 1 hour
redirectLocationDataCache.SetWithExpiry(url, "", 1*time.Hour)
// Always return a success status and a JSON string to limit information returned to client.
w.Write([]byte(model.MapToJson(m)))
return
@@ -429,8 +432,8 @@ func getRedirectLocation(c *Context, w http.ResponseWriter, r *http.Request) {
res.Body.Close()
}()
location := res.Header.Get("Location")
redirectLocationDataCache.AddWithExpiresInSecs(url, location, 3600) // Expires after 1 hour
location = res.Header.Get("Location")
redirectLocationDataCache.SetWithExpiry(url, location, 1*time.Hour)
m["location"] = location
w.Write([]byte(model.MapToJson(m)))