mirror of
https://github.com/chubin/wttr.in
synced 2025-01-12 03:58:45 +00:00
dont cache cyclic requests (#460)
This commit is contained in:
parent
34a58b260d
commit
ef7d46723d
2 changed files with 31 additions and 1 deletions
|
@ -7,15 +7,21 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func processRequest(r *http.Request) responseWithHeader {
|
func processRequest(r *http.Request) responseWithHeader {
|
||||||
var response responseWithHeader
|
var response responseWithHeader
|
||||||
|
|
||||||
foundInCache := false
|
if dontCache(r) {
|
||||||
|
return get(r)
|
||||||
|
}
|
||||||
|
|
||||||
cacheDigest := getCacheDigest(r)
|
cacheDigest := getCacheDigest(r)
|
||||||
|
|
||||||
|
foundInCache := false
|
||||||
|
|
||||||
savePeakRequest(cacheDigest, r)
|
savePeakRequest(cacheDigest, r)
|
||||||
|
|
||||||
cacheBody, ok := lruCache.Get(cacheDigest)
|
cacheBody, ok := lruCache.Get(cacheDigest)
|
||||||
|
@ -109,6 +115,17 @@ func getCacheDigest(req *http.Request) string {
|
||||||
return fmt.Sprintf("%s:%s%s:%s:%s", userAgent, queryHost, queryString, clientIPAddress, lang)
|
return fmt.Sprintf("%s:%s%s:%s:%s", userAgent, queryHost, queryString, clientIPAddress, lang)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return true if request should not be cached
|
||||||
|
func dontCache(req *http.Request) bool {
|
||||||
|
|
||||||
|
// dont cache cyclic requests
|
||||||
|
loc := strings.Split(req.RequestURI, "?")[0]
|
||||||
|
if strings.Contains(loc, ":") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func readUserIP(r *http.Request) string {
|
func readUserIP(r *http.Request) string {
|
||||||
IPAddress := r.Header.Get("X-Real-Ip")
|
IPAddress := r.Header.Get("X-Real-Ip")
|
||||||
if IPAddress == "" {
|
if IPAddress == "" {
|
||||||
|
|
13
lib/cache.py
13
lib/cache.py
|
@ -34,8 +34,16 @@ def get_signature(user_agent, query_string, client_ip_address, lang):
|
||||||
"""
|
"""
|
||||||
Get cache signature based on `user_agent`, `url_string`,
|
Get cache signature based on `user_agent`, `url_string`,
|
||||||
`lang`, and `client_ip_address`
|
`lang`, and `client_ip_address`
|
||||||
|
Return `None` if query should not be cached.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if "?" in query_string:
|
||||||
|
location = query_string.split("?", 1)[0]
|
||||||
|
else:
|
||||||
|
location = query_string
|
||||||
|
if ":" in location:
|
||||||
|
return None
|
||||||
|
|
||||||
signature = "%s:%s:%s:%s" % \
|
signature = "%s:%s:%s:%s" % \
|
||||||
(user_agent, query_string, client_ip_address, lang)
|
(user_agent, query_string, client_ip_address, lang)
|
||||||
print(signature)
|
print(signature)
|
||||||
|
@ -48,6 +56,9 @@ def get(signature):
|
||||||
the `_update_answer` function.
|
the `_update_answer` function.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not signature:
|
||||||
|
return None
|
||||||
|
|
||||||
value_record = CACHE.get(signature)
|
value_record = CACHE.get(signature)
|
||||||
if not value_record:
|
if not value_record:
|
||||||
return None
|
return None
|
||||||
|
@ -69,6 +80,8 @@ def store(signature, value):
|
||||||
"""
|
"""
|
||||||
Store in cache `value` for `signature`
|
Store in cache `value` for `signature`
|
||||||
"""
|
"""
|
||||||
|
if not signature:
|
||||||
|
return value
|
||||||
|
|
||||||
if len(value) >= MIN_SIZE_FOR_FILECACHE:
|
if len(value) >= MIN_SIZE_FOR_FILECACHE:
|
||||||
value_to_store = _store_in_file(signature, value)
|
value_to_store = _store_in_file(signature, value)
|
||||||
|
|
Loading…
Reference in a new issue