I’m currently transitioning my GAE application from the Python 2.5 runtime to the Python 2.7 runtime with “ThreadSafe” enabled.
Currently my data access methods look something like:
@staticmethod
def GetByPermaLinkId(permaLinkId):
result = memcache.get("entry-by-permalink-id:" + permaLinkId)
if result is None:
result = db.get(permaLinkId)
memcache.set("entry-by-permalink-id:" + permaLinkId, result)
return result
So I suppose I have two questions.
-
Is my data access method completely ridiculous?
-
Is my data access method okay for use in “ThreadSafe” applications?
I’m super new to App Engine and Python developement, feedback is much appreciated.
Thanks!
Caching frequently requested data is a good idea, however you really want to take a look at the Python Style Guide PEP8 http://www.python.org/dev/peps/pep-0008/
Yes, your method is ok. Thread safety in python is usually an issue of multiple threads accessing shared data. You are using memcache to handle the shared data and memcache in this use case is thread safe as far as I know. Would you instead have tried to cache your result in a global variable then there would have been a race condition, in which case you could use theading.Lock to avoid it.