内存缓存无法正常工作

时间:2015-01-25 16:05:46

标签: python django google-app-engine memcached

我们在Python中有一个小型的Google App Engine应用程序,我们正在使用memcache。但memcache密钥在memcache.delete之后仍然存在,并且当我希望它返回一个字符串(“undefined”)时,memcache也返回一个数字(0)。这是我的代码:

check_feature.py:

import sys
sys.path.insert(0, 'libs')

import webapp2
import json
from google.appengine.api import memcache
from models.shard_counter import GeneralCounterShard
from models.check_feature_limit import CheckFeatureLimit


class CheckFeatureHandler(webapp2.RequestHandler):
    def get_number_of_users_enabled(self, feature_name):
        """
        Get the number of users enabled for the given feature name.
        """
        number_of_users_enabled_undefined = "undefined"
        number_of_users_enabled = memcache.get(key=feature_name)
        if (number_of_users_enabled is None):
            check_feature_limit = None
            check_feature_limits = CheckFeatureLimit.gql("WHERE feature_name=:1 ORDER BY last_modified DESC LIMIT 1", feature_name)
            if (check_feature_limits.count() > 0):
                check_feature_limit = check_feature_limits.get()
            if (check_feature_limit):
                number_of_users_enabled = check_feature_limit.number_of_users_enabled
            if (number_of_users_enabled is None):
                number_of_users_enabled = number_of_users_enabled_undefined
            memcache.add(key=feature_name, value=number_of_users_enabled, time=3600)
        if (number_of_users_enabled == number_of_users_enabled_undefined):
            number_of_users_enabled = None
        return number_of_users_enabled

admin.py:

import sys
sys.path.insert(0, 'libs')

import webapp2
import json
import requests
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from models.shard_counter import GeneralCounterShard
from models.check_feature_limit import CheckFeatureLimit

template.register_template_library("tags.tags")


class AdminHandler(webapp2.RequestHandler):
    def get(self):
        self.post()

    def post(self):
        params = {}
        number_of_users_enabled_dict = {}
        number_of_users_dict = {}
        r = requests.get(url="http://jsons.[part_of_link_suppressed].com.s3.amazonaws.com/flags.json")
        flags = json.loads(r.text)
        if ((flags) and ("depending_on_counter" in flags) and (len(flags["depending_on_counter"]) > 0)):
            for feature_name in flags["depending_on_counter"]:
                check_feature_limit = None
                check_feature_limits = CheckFeatureLimit.gql("WHERE feature_name=:1 ORDER BY last_modified DESC LIMIT 1", feature_name)
                if (check_feature_limits.count() > 0):
                    check_feature_limit = check_feature_limits.get()
                number_of_users_enabled = self.request.get(feature_name + "_number_of_users_enabled")
                if (number_of_users_enabled):
                    number_of_users_enabled = int(number_of_users_enabled)
                    if (not(check_feature_limit)):
                        check_feature_limit = CheckFeatureLimit(feature_name=feature_name)
                    check_feature_limit.number_of_users_enabled = number_of_users_enabled
                    check_feature_limit.put()
                    memcache.delete(key=feature_name) # I don't think it works.
                number_of_users_enabled = None
                if (check_feature_limit):
                    number_of_users_enabled = check_feature_limit.number_of_users_enabled
                if (not(number_of_users_enabled is None)):
                    number_of_users_enabled_dict[feature_name] = number_of_users_enabled
                number_of_users = GeneralCounterShard.get_count(feature_name)
                number_of_users_dict[feature_name] = number_of_users
            params["depending_on_counter"] = flags["depending_on_counter"]
            params["number_of_users_enabled_dict"] = number_of_users_enabled_dict
            params["number_of_users_dict"] = number_of_users_dict
        html = template.render("admin/admin.html", params)
        self.response.out.write(html)

app = webapp2.WSGIApplication([
    ("/admin", AdminHandler)
], debug=True)

test_counter_feature_1(要启用的用户数)的值为2,test_counter_feature_2为4且test_counter_feature_3未定义(没有对象),但在memcache中,值分别为2,3和0,即使我保存后也是如此表单(因此应删除memcache)。问题是什么?我希望test_counter_feature_3的值为“未定义”,而不是0.保存表单后应删除其他两个值。

1 个答案:

答案 0 :(得分:1)

好的,我发现了问题。 GeneralCounterShard也将相同的密钥保存到memcache,因此我重命名了密钥,现在一切正常。新密钥为feature_name + "_number_of_users_enabled",而不是之前的feature_name。所以我用新密钥替换了所有对memcache的调用,现在它可以工作了。谢谢!