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"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func processRequest(r *http.Request) responseWithHeader {
|
||||
var response responseWithHeader
|
||||
|
||||
foundInCache := false
|
||||
if dontCache(r) {
|
||||
return get(r)
|
||||
}
|
||||
|
||||
cacheDigest := getCacheDigest(r)
|
||||
|
||||
foundInCache := false
|
||||
|
||||
savePeakRequest(cacheDigest, r)
|
||||
|
||||
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 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 {
|
||||
IPAddress := r.Header.Get("X-Real-Ip")
|
||||
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`,
|
||||
`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" % \
|
||||
(user_agent, query_string, client_ip_address, lang)
|
||||
print(signature)
|
||||
|
@ -48,6 +56,9 @@ def get(signature):
|
|||
the `_update_answer` function.
|
||||
"""
|
||||
|
||||
if not signature:
|
||||
return None
|
||||
|
||||
value_record = CACHE.get(signature)
|
||||
if not value_record:
|
||||
return None
|
||||
|
@ -69,6 +80,8 @@ def store(signature, value):
|
|||
"""
|
||||
Store in cache `value` for `signature`
|
||||
"""
|
||||
if not signature:
|
||||
return value
|
||||
|
||||
if len(value) >= MIN_SIZE_FOR_FILECACHE:
|
||||
value_to_store = _store_in_file(signature, value)
|
||||
|
|
Loading…
Reference in a new issue