Reputation: 786
I'm making a blog using Google App Engine, which for now just allows me to post entries and see them listed out. I was following the Web Development course by Steve Huffman on Udacity where he showed a simple caching technique to decrease the database queries. Here's my code (which he instructed):
CACHE = {}
def top_entries():
key = 'top'
# logging.error(CACHE)
if key in CACHE:
entries = CACHE[key]
else:
logging.error("DB QUERY")
entries = db.GqlQuery('select * from Entries order by created desc limit 10')
entries = list(entries)
CACHE[key] = entries
return entries
class MainPage(webapp2.RequestHandler):
def get(self):
entries = top_entries()
self.response.write(render_str('mainpage.html', entries=entries))
mainpage.html
is just the main page that lists the 10 most recent entries.
Basically, CACHE
is a dictionary that stores a key that identifies a particular database query (in this case, listing out the top 10 entries) and on further calls to the same query, the application simply looks in that dictionary without making a database call.
What SHOULD happen is this:
I load the page for the first time and DB QUERY
is printed on my console since it's a database call. The result of the query is stored in CACHE
.
I reload and DB QUERY
isn't printed since the cache has the key.
The above DOES happen but only if I have a fresh new tab to work on. If I already have a tab where I was working on this app, and I shut down the server, reboot it and then try reloading the page again, the above doesn't work. What happens is that step 1 happens twice and then step 2. It seems like the key isn't stored in the CACHE
the first time I reload the page, but on the second time.
Here's the console log if the above paragraph is hard to follow:
The first time I boot the server and start working on a fresh tab:
PS C:\Users\IBM_ADMIN> python `C:\Program Files (x86)\Google\google_appengine\dev_appserver.py' 'C:\Users\IBM_ADMIN\Downloads\Udacity\Web Development\Blog Project'
INFO 2016-06-16 14:31:04,000 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2016-06-16 14:31:06,525 api_server.py:205] Starting API server at: http://localhost:53305
INFO 2016-06-16 14:31:06,545 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2016-06-16 14:31:06,552 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2016-06-16 09:01:15,358 blog.py:134] DB QUERY
INFO 2016-06-16 14:31:15,529 module.py:787] default: "GET / HTTP/1.1" 200 1849
INFO 2016-06-16 14:31:17,984 module.py:787] default: "GET / HTTP/1.1" 200 1849
INFO 2016-06-16 14:31:45,944 shutdown.py:45] Shutting down.
INFO 2016-06-16 14:31:46,040 api_server.py:648] Applying all pending transactions and saving the datastore
INFO 2016-06-16 14:31:46,042 api_server.py:651] Saving search indexes
And this is what happens when I don't open a fresh tab but start working on the old tab.
PS C:\Users\IBM_ADMIN> python `C:\Program Files (x86)\Google\google_appengine\dev_appserver.py' 'C:\Users\IBM_ADMIN\Downloads\Udacity\Web Development\Blog Project'
INFO 2016-06-16 14:31:56,470 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2016-06-16 14:31:58,637 api_server.py:205] Starting API server at: http://localhost:53318
INFO 2016-06-16 14:31:58,651 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2016-06-16 14:31:58,657 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2016-06-16 09:02:08,336 blog.py:134] DB QUERY
INFO 2016-06-16 14:32:08,526 module.py:787] default: "GET / HTTP/1.1" 200 1849
ERROR 2016-06-16 09:02:12,538 blog.py:134] DB QUERY
INFO 2016-06-16 14:32:12,684 module.py:787] default: "GET / HTTP/1.1" 200 1849
INFO 2016-06-16 14:32:16,822 module.py:787] default: "GET / HTTP/1.1" 200 1849
INFO 2016-06-16 14:32:21,428 shutdown.py:45] Shutting down.
INFO 2016-06-16 14:32:21,430 api_server.py:648] Applying all pending transactions and saving the datastore
INFO 2016-06-16 14:32:21,430 api_server.py:651] Saving search indexes
DB QUERY
gets printed out twice instead of once. I can't think of any reason why it would behave this way. (Maybe because of my browser cookies?)
Upvotes: 0
Views: 74
Reputation: 6278
You are caching at instance level and you can have number of instances or an instance can be recycled (shut down) and a new one instantiated on a request.
Try to use Memcache if you want to share cache between instances: https://cloud.google.com/appengine/docs/python/memcache/
You can combine both approaches (instance cache + memcache)
Upvotes: 3