这是我的memcache获取代码&设置(所有内容都指向_mc_get)
def _mc_get(key, retrieve_func):
value = memcache.get(key)
if value is not None:
logging.debug("Memcache: hit %s", key)
return value
logging.debug("Memcache: missing + adding %s", key)
value = retrieve_func()
if not memcache.add(key, value, 10):
logging.error('Memcache set failed.')
return value
def mc_get_all_orders():
return _mc_get('Order:all', Order.query().fetch)
def mc_get_tasks_by_order(order_key):
return _mc_get('Order:all', Task.query(Task.order == order_key).order(Task.action).fetch)
def mc_get_tasks_by_orders(order_keys):
return _mc_get('Order:order_id:%s' % sorted([k.id() for k in order_keys]),
Task.query(Task.order.IN(order_keys)).order(Task.action).fetch)
def mc_get_all_producetypes():
return _mc_get('ProduceType:all', ProduceType.query().fetch)
此功能在appengine上花费> 40秒。
@app.route('/orders/schedule/all/', methods=['GET', 'POST'])
@login_required
def generate_schedule_for_all_orders():
"""Merge all the orders together in one big schedule
TODO: put range by default.
TODO: think of default empty scenario
"""
orders = key_dict(mc_get_all_orders())
# TODO: somehow there's a lot of orphaned tasks in development. For now,
# we will just query for non orphaned tasks to prevent errors piling up
tasks = mc_get_tasks_by_orders(orders.keys())
ds = sorted([t.date for t in tasks])
start, end = ds[0], ds[-1]
dates = range_for_dates(start, end)
return render_template('backward_schedule.html',
tasks=tasks,
orders=orders,
producetypes=key_dict(mc_get_all_producetypes()),
dates=dates,
display_order_info=True)
我想知道为什么这花了这么长时间,所以我在我的_mc_get上插入了logging.debug语句,以查看我是否正在点击或丢失内存缓存。令我惊讶的是,如果等待时间超过3秒,我在memcache上缺少100%:(这适用于我的开发和生产服务器)
记录首次访问该页面:注意时间
DEBUG 2014-06-11 23:16:11,765 mc.py:27] Memcache: missing + adding Order:all
DEBUG 2014-06-11 23:16:11,808 mc.py:27] Memcache: missing + adding Order:order_id:[4578366418059264L, 4741094138970112L, 5260063627280384L, 5664683906301952L, 5673479999324160L, 5761440929546240L]
DEBUG 2014-06-11 23:16:12,255 mc.py:27] Memcache: missing + adding ProduceType:all
INFO 2014-06-11 23:16:13,517 recording.py:665] Saved; key: __appstats__:071700, part: 105 bytes, full: 64362 bytes, overhead: 0.002 + 0.010; link: http://localhost:8080/_ah/stats/details?time=1402528571756
第二次访问页面(第一次访问完成加载后重新加载)
INFO 2014-06-11 23:16:13,534 module.py:639] default: "GET /orders/schedule/all/ HTTP/1.1" 200 1236559
DEBUG 2014-06-11 23:16:15,638 mc.py:24] Memcache: hit Order:all
DEBUG 2014-06-11 23:16:15,699 mc.py:24] Memcache: hit Order:order_id:[4578366418059264L, 4741094138970112L, 5260063627280384L, 5664683906301952L, 5673479999324160L, 5761440929546240L]
DEBUG 2014-06-11 23:16:15,706 mc.py:24] Memcache: hit ProduceType:all
INFO 2014-06-11 23:16:16,937 recording.py:665] Saved; key: __appstats__:075600, part: 56 bytes, full: 15643 bytes, overhead: 0.000 + 0.004; link: http://localhost:8080/_ah/stats/details?time=1402528575619
第三次访问同一页面(只需点击刷新):(在第二次访问同一页面后,这是字面 4秒)
INFO 2014-06-11 23:16:17,112 module.py:639] default: "GET /orders/schedule/all/ HTTP/1.1" 200 1236559
DEBUG 2014-06-11 23:16:23,362 mc.py:27] Memcache: missing + adding Order:all
DEBUG 2014-06-11 23:16:23,415 mc.py:27] Memcache: missing + adding Order:order_id:[4578366418059264L, 4741094138970112L, 5260063627280384L, 5664683906301952L, 5673479999324160L, 5761440929546240L]
DEBUG 2014-06-11 23:16:23,888 mc.py:27] Memcache: missing + adding ProduceType:all
INFO 2014-06-11 23:16:25,153 recording.py:665] Saved; key: __appstats__:083300, part: 105 bytes, full: 64339 bytes, overhead: 0.002 + 0.011; link: http://localhost:8080/_ah/stats/details?time=1402528583353
INFO 2014-06-11 23:16:25,169 module.py:639] default: "GET /orders/schedule/all/ HTTP/1.1" 200 1236559
为什么我的memcache到期如此之快?
答案 0 :(得分:4)
对于memcache条目无法保证,理论上它可以随时被驱逐,因为内存压力。
您使用的是memcache.add,如果不存在,则只设置该值。你的超时是10秒,memcache是在23:16:13添加的,当你在23:16:23访问它时会丢失。这是预期的,因为它大约超过10秒。它永远不会打印内存缓存添加失败,因为如果找到该值,该函数会立即返回。
mc_get_all_orders和mc_get_tasks_by_order使用相同的密钥,但查询不同,有点奇怪