Created
February 3, 2011 00:12
-
-
Save maraca/808794 to your computer and use it in GitHub Desktop.
Fetching with datastore & memcached
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Copyright 2007 Google Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp import util | |
from google.appengine.ext import db | |
from google.appengine.api import memcache | |
import time | |
class User(db.Model): | |
"""Models """ | |
name = db.StringProperty(required=True) | |
class MainHandler(webapp.RequestHandler): | |
def get(self): | |
init_time = time.time() | |
users = memcache.get('user_list') | |
if users is None: | |
users = User.all() | |
memcache.add('user_list', users, 8) | |
self.response.out.write('users fetched from datastore in :') | |
else: | |
self.response.out.write('users fetched from memcached in :') | |
end_time = time.time() | |
self.response.out.write('%f milliseconds ' % | |
(end_time - init_time) ) | |
class PopulateHandler(webapp.RequestHandler): | |
def get(self): | |
for user in range(1000): | |
User(name=str(time.time())).put() | |
def main(): | |
application = webapp.WSGIApplication([ | |
('/', MainHandler), | |
('/populate/', PopulateHandler), | |
], | |
debug=True) | |
util.run_wsgi_app(application) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment