db-read() -> 100ms
if request in cache,
return cache[request]
cashe is a hashtable
basic cache algorithm is below
import time
def complex_computation(a, b):
time.sleep(.5)
return a + b
cache = {}
def cached_computation(a, b):
key = (a, b)
if key in cache:
r = cache[key]
else:
r = complex_computation(a, b)
cache[key] = r
return r
start_time = time.time()
print cached_computation(5, 3)
print "the first computation took %f second" % (time.time() - start_time)
when db query can be cashed in serverside as dictionary like below.
CACHE = {}
def top_arts();
key = 'top'
if key in CACHE:
arts = CHACHE[key]
else:
loggin.error("DB QUERY")
art = db.GqlQuery("SELECT * From Art "
"WHERE ancestor is :1"
"ORDER BY DESC")
arts = list(arts)
CACHE[key] = arts
return arts
when posted, cache cleared.
CACHE = {}
def top_arts(update = False);
key = 'top'
if not update and key in CACHE:
arts = CHACHE[key]
else:
loggin.error("DB QUERY")
art = db.GqlQuery("SELECT * From Art "
"WHERE ancestor is :1"
"ORDER BY DESC")
arts = list(arts)
CACHE[key] = arts
return arts
DB read should be only submission.
loadbalancer also can handle large traffic.
load balancere
n = -1
def get_server():
global n
n += 1
return SERVERS[n % len(SERVERS)]
handler, url mapping DBmodel