mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user