mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Refactor to use structured logging * Properly formatted with gofmt * created interface Cache, but construction of cache is still coupled. * Implementing cache factories to build caches * Simple redis implementation without error handling. Keys and values by default are string * refactor NewLocalCacheLayer to inject cache factory * Removed redis impl to focus on cache abstraction * CacheFactory injected on sqlsupplier and saved in Store struct * remove useless private method * replace concrete declaration of lru cache to cache abstraction * discard spaces * Renamed factory to provider because concrete implementations of factories may hold an state (such as a redis client for example) * refactor to include all caches in the same package cache and subpackages * method close cache. This method will be used for concrete implementations that need to release resources (close a connection, etc) * closing cacheprovider and releasing resources while closing sql store * fixed merge conflict fail * gofmt files * remove unused property from post_store * naming refactor to avoid stutter. Added godocs on interface * Store doesnt know anything about the cache and provider. Cache provider will be built after loading config and injected in localCacheLayer * fixed broken test * cache provider initialized before RunOldAppInitialization which initializes the localcachelayer * move statusCache to server to initialize it with the new cache provider * update terms_service and channel_layer to have new cacheProvider * gofmt * Add Connect method to the cache provider * mock cacheprovider in user_layer_test Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: mattermod <mattermod@users.noreply.github.com>
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package api4
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
|
|
)
|
|
|
|
const OPEN_GRAPH_METADATA_CACHE_SIZE = 10000
|
|
|
|
var openGraphDataCache = lru.New(OPEN_GRAPH_METADATA_CACHE_SIZE)
|
|
|
|
func (api *API) InitOpenGraph() {
|
|
api.BaseRoutes.OpenGraph.Handle("", api.ApiSessionRequired(getOpenGraphMetadata)).Methods("POST")
|
|
|
|
// Dump the image cache if the proxy settings have changed. (need switch URLs to the correct proxy)
|
|
api.ConfigService.AddConfigListener(func(before, after *model.Config) {
|
|
if (before.ImageProxySettings.Enable != after.ImageProxySettings.Enable) ||
|
|
(before.ImageProxySettings.ImageProxyType != after.ImageProxySettings.ImageProxyType) ||
|
|
(before.ImageProxySettings.RemoteImageProxyURL != after.ImageProxySettings.RemoteImageProxyURL) ||
|
|
(before.ImageProxySettings.RemoteImageProxyOptions != after.ImageProxySettings.RemoteImageProxyOptions) {
|
|
openGraphDataCache.Purge()
|
|
}
|
|
})
|
|
}
|
|
|
|
func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !*c.App.Config().ServiceSettings.EnableLinkPreviews {
|
|
c.Err = model.NewAppError("getOpenGraphMetadata", "api.post.link_preview_disabled.app_error", nil, "", http.StatusNotImplemented)
|
|
return
|
|
}
|
|
|
|
props := model.StringInterfaceFromJson(r.Body)
|
|
|
|
url := ""
|
|
ok := false
|
|
if url, ok = props["url"].(string); len(url) == 0 || !ok {
|
|
c.SetInvalidParam("url")
|
|
return
|
|
}
|
|
|
|
ogJSONGeneric, ok := openGraphDataCache.Get(url)
|
|
if ok {
|
|
w.Write(ogJSONGeneric.([]byte))
|
|
return
|
|
}
|
|
|
|
og := c.App.GetOpenGraphMetadata(url)
|
|
ogJSON, err := og.ToJSON()
|
|
openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 hour
|
|
if err != nil {
|
|
w.Write([]byte(`{"url": ""}`))
|
|
return
|
|
}
|
|
|
|
w.Write(ogJSON)
|
|
}
|