Added caching
This commit is contained in:
parent
d061860fa2
commit
091f6a455e
1
go.mod
1
go.mod
@ -11,6 +11,7 @@ require (
|
|||||||
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 // indirect
|
github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 // indirect
|
||||||
github.com/grokify/html-strip-tags-go v0.0.1
|
github.com/grokify/html-strip-tags-go v0.0.1
|
||||||
github.com/joho/godotenv v1.3.0
|
github.com/joho/godotenv v1.3.0
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/rivo/uniseg v0.2.0
|
github.com/rivo/uniseg v0.2.0
|
||||||
github.com/tmdvs/Go-Emoji-Utils v1.1.0
|
github.com/tmdvs/Go-Emoji-Utils v1.1.0
|
||||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -14,6 +14,8 @@ github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q
|
|||||||
github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
|
github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
|
||||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/tmdvs/Go-Emoji-Utils v1.1.0 h1:gtPix7HZPrd49+MNDcuRLvv4xVNxCE5wgjqyuvmbyYg=
|
github.com/tmdvs/Go-Emoji-Utils v1.1.0 h1:gtPix7HZPrd49+MNDcuRLvv4xVNxCE5wgjqyuvmbyYg=
|
||||||
|
15
main.go
15
main.go
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/ChimeraCoder/anaconda"
|
"github.com/ChimeraCoder/anaconda"
|
||||||
strip "github.com/grokify/html-strip-tags-go"
|
strip "github.com/grokify/html-strip-tags-go"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
"github.com/rivo/uniseg"
|
"github.com/rivo/uniseg"
|
||||||
emoji "github.com/tmdvs/Go-Emoji-Utils"
|
emoji "github.com/tmdvs/Go-Emoji-Utils"
|
||||||
)
|
)
|
||||||
@ -36,6 +37,8 @@ var (
|
|||||||
accessTokenSecret string
|
accessTokenSecret string
|
||||||
consumerKey string
|
consumerKey string
|
||||||
consumerSecret string
|
consumerSecret string
|
||||||
|
|
||||||
|
ch *cache.Cache
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -46,6 +49,8 @@ func init() {
|
|||||||
accessTokenSecret = os.Getenv("ACCESS_TOKEN_SECRET")
|
accessTokenSecret = os.Getenv("ACCESS_TOKEN_SECRET")
|
||||||
consumerKey = os.Getenv("CONSUMER_KEY")
|
consumerKey = os.Getenv("CONSUMER_KEY")
|
||||||
consumerSecret = os.Getenv("CONSUMER_SECRET")
|
consumerSecret = os.Getenv("CONSUMER_SECRET")
|
||||||
|
|
||||||
|
ch = cache.New(24*time.Hour, 48*time.Hour)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -78,6 +83,10 @@ func getTweet(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result, found := ch.Get(id)
|
||||||
|
if !found {
|
||||||
|
fmt.Println("No cached tweet found, generating new...")
|
||||||
tweet, err := api.GetTweet(i, nil)
|
tweet, err := api.GetTweet(i, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err := err.(type) {
|
switch err := err.(type) {
|
||||||
@ -96,8 +105,12 @@ func getTweet(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
processTweet(&tweet)
|
processTweet(&tweet)
|
||||||
|
|
||||||
|
result = renderTemplate(tweet, false)
|
||||||
|
ch.Set(id, result, cache.DefaultExpiration)
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-type", "image/svg+xml")
|
w.Header().Set("Content-type", "image/svg+xml")
|
||||||
_, err = w.Write(renderTemplate(tweet, false))
|
_, err = w.Write(result.([]byte))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
|
Loading…
Reference in New Issue
Block a user