GAE Python Practice

cache 機制(2種)

cache
1
2
3
4
5
class Test2(webapp2.RequestHandler):

def get(self):
self.response.headers.add_header('cache-control','public, max-age=7200') # 2hr cache

cache
1
2
3
- url: /images
static_dir: static/images

Min Pending Latency:

當較多的request進來時,要等多久去開啟instance

Max Idle Instances:
就是instance

class Account(ndb.Model):
username = ndb.StringProperty()
userid = ndb.IntegerProperty()
email = ndb.StringProperty()

parent –> children

rev_key = ndb.Key(‘Account’, ‘Sandy’, ‘Message’, ‘greeting’, ‘Revision’, ‘2’)
rev_key.id() # 2

sandy_key = ndb.Key(‘Account’, ‘Sandy’)
sandy_key = ndb.Key(Account, ‘Sandy’)

k1 = ndb.Key(‘Account’, ‘Sandy’, ‘Message’, ‘greetings’, ‘Revision’, ‘2’)
k2 = ndb.Key(Revision, ‘2’, parent=ndb.Key(‘Account’, ‘Sandy’, ‘Message’, ‘greetings’))
k3 = ndb.Key(Revision, ‘2’, parent=ndb.Key(Account, ‘Sandy’, Message, ‘greetings’))

Creating Entities

sandy = Account(username=’Sandy’,userid=123,email=’sandy@gmail.com’)
sandy_key = sandy.put()

Retrieving Entities from Keys

sandy = sandy_key.get()

rev_key.kind() # returns “Revision”
rev_key.id() # returns “2”
rev_key.urlsafe()

Updating Entities

sandy = key.get()
sandy.email = ‘sandy@gmail.co.uk’
sandy.put()

Deleting Entities

sandy.key.delete()

Expando Models

class Mine(ndb.Expando):
pass

e = Mine()
e.foo = 1
e.bar = ‘blah’
e.tags = [‘exp’, ‘and’, ‘oh’]
e.put()
print e._properties
{‘foo’: GenericProperty(‘foo’), ‘bar’: GenericProperty(‘bar’),
‘tags’: GenericProperty(‘tags’, repeated=True)}

Query

account_query = Account.query(Account.username == ‘Sandy’)
accounts = account_query.fetch()

mine_query = Mine.query(ndb.GenericProperty(‘foo’) == 1)
mines = mine_query.fetch(10)

如果知道id的話,這種查詢比較快速

Account.get_by_id(…)

property == value
property < value
property <= value
property > value
property >= value
property != value
property.IN([value1, value2])

Specifying Sort Orders

qry = Greeting.query().order(Greeting.message, -Greeting.userid)
qry = Greeting.query().order(Greeting.message).order(-Greeting.userid)