summaryrefslogtreecommitdiffstats
path: root/api/post.go
diff options
context:
space:
mode:
authorDebanshu Kundu <debanshu.kundu@joshtechnologygroup.com>2017-02-12 22:01:49 +0530
committerJoram Wilander <jwawilander@gmail.com>2017-02-12 11:31:49 -0500
commiteab15da32122ec421d22dbfb3ee2cd1844d07b5c (patch)
tree8d0f26e8c48db93ce0c9407c22c554a6fb4784dd /api/post.go
parent37222ec1b3364e1bd57ce268416edd1832be368c (diff)
downloadchat-eab15da32122ec421d22dbfb3ee2cd1844d07b5c.tar.gz
chat-eab15da32122ec421d22dbfb3ee2cd1844d07b5c.tar.bz2
chat-eab15da32122ec421d22dbfb3ee2cd1844d07b5c.zip
PLT-5455 Added caching for OG metadata of link preview. (#5361)
Diffstat (limited to 'api/post.go')
-rw-r--r--api/post.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/api/post.go b/api/post.go
index d9531b09a..76d813940 100644
--- a/api/post.go
+++ b/api/post.go
@@ -14,6 +14,10 @@ import (
"github.com/mattermost/platform/utils"
)
+const OPEN_GRAPH_METADATA_CACHE_SIZE = 10000
+
+var openGraphDataCache = utils.NewLru(OPEN_GRAPH_METADATA_CACHE_SIZE)
+
func InitPost() {
l4g.Debug(utils.T("api.post.init.debug"))
@@ -432,8 +436,14 @@ func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) {
func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.StringInterfaceFromJson(r.Body)
+ ogJSONGeneric, ok := openGraphDataCache.Get(props["url"])
+ if ok {
+ w.Write(ogJSONGeneric.([]byte))
+ return
+ }
+
url := ""
- ok := false
+ ok = false
if url, ok = props["url"].(string); len(url) == 0 || !ok {
c.SetInvalidParam("getOpenGraphMetadata", "url")
return
@@ -441,10 +451,12 @@ func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
og := app.GetOpenGraphMetadata(url)
- ogJson, err := og.ToJSON()
+ ogJSON, err := og.ToJSON()
if err != nil {
w.Write([]byte(`{"url": ""}`))
return
}
- w.Write(ogJson)
+
+ openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 houre
+ w.Write(ogJSON)
}