Reputation: 4419
I have been using Memcache in Google App Engine (Python) for some time now and generally it works very well. In the last few days though I have noticed that with some code like the below example that when it is updated right after I renew a database entry that it doesn't get it in time. Is this due to the length of time that it takes to store the entry in the database? Is there any solution?
# I store the comment here
c = Comment(content = content, submitter = submitter, group_id=int(group_id), user_id = user_id)
c.put()
# Right after I store the comment I refresh the cache
comment_cache(int(group_id), True)
Often the newest comment is not in the cache.
Upvotes: 0
Views: 292
Reputation: 21
I had the same problem as you.
When I added a value in my database, I updated my cache but as the query took long time to run, my last inserted value was not inserted in the cache.
My solution : I had a function that updates my cache, and now I added the value that I wanted to put in my database as parameter like that :
def get_values_from_cache_or_database(particular_value = None, update = True):
key = 'my_key'
values = memcache.get(key)
if values is None or update:
values = db.GqlQuery("SELECT * FROM Table")
values = list(values)
if update:
if particular_value not in values:
# if we are here, particular_value isn't in your data base (because time
# request is long) but we want the particular_value in the cache so we add
# it manually
values.append(particular_value)
memcache.set(key, values)
return values
So, for example we put a value "value1" like that :
value1.put()
We call this function to refresh the cache with "value1" as parameter :
get_values_from_cache_or_database(value1, True)
And then we'll get your cache with the latest value added in !
Upvotes: 1
Reputation: 16882
Due to eventual consistency, if comment_cache()
runs a query (i.e., doesn't fetch by key), then what you describe is expected.
Some solutions:
comment_cache()
to take c
as a parameter so that it explicitly knows about it: comment_cache(int(group_id), True, c)
.comment_cache()
in a task queue. There is still no guarantee that it will pick up the new comment, but since it will be run some time later, it might.Upvotes: 2