Skip to content

Instantly share code, notes, and snippets.

@syakesaba
Created August 12, 2014 14:50
Show Gist options
  • Save syakesaba/3f9f840911c859f1ce4f to your computer and use it in GitHub Desktop.
Save syakesaba/3f9f840911c859f1ce4f to your computer and use it in GitHub Desktop.
ndb.Modelでプロパテぃが全てSerializableな時のmemcache。
@classmethod
def get_cached_univs_info(cls):
"""
returns cached universities infomation.
@return: dict universities. all ["univ_name"]["key"] are defined but value can be None.
@rtype: dict
"""
univs_info = memcache.get("univs_info") # @UndefinedVariable
if univs_info is None:
univs = cls.query().fetch()
raw_univs_info = {
univ.name:{
"name":univ.name,
"repo_url":univ.repo_url,
"other_research":univ.other_research,
"how_to_access":univ.how_to_access,
"group":univ.group,
"comment":univ.comment,
"keyword":univ.keyword,
"homepage_url":univ.homepage_url,
"country":univ.country,
"prefecture":univ.prefecture,
"address":univ.address,
"fields_of_research":univ.fields_of_research,
"deviation":univ.deviation,
}
for univ in univs} #: :type univ: models.University
univs_info = json.dumps(raw_univs_info)
memcache.add("univs_info",univs_info) # @UndefinedVariable
return json.loads(univs_info)
@classmethod
def refresh_cached_univs_info(cls):
memcache.delete("univs_info") #@UndefinedVariable
def _post_put_hook(self, future):
self.refresh_cached_univs_info()
@classmethod
def _post_delete_hook(cls, key, future):
cls.refresh_cached_univs_info()
#univs_infoをhook中に更新すればもっと効率がよくなる。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment