views:

106

answers:

1

I need to store settings for my Google App Engine project. Currently I have:

class Settings(db.Model):
    rate = db.IntegerProperty(default=4)
    ...

And when I want to use it:

Settings.get_or_insert('settings')

This feels clumsy so is there a better way (without using Django)?

+2  A: 

Please clarify what does "feel clumsy" to you about this -- that's not very clear to me.

The datastore is the way to persist updatable data in App Engine (blobstore's for huge blobs, memcache's not guaranteed persistent). If your settings can't be changed by the application of course you can just put them in your own custom .yaml file (or whatever, but yaml's how App Engine's own configuration files are already stored anyway...;-); just remember all such files are read-only from the application's viewpoint. YAML is conveniently available to App Engine apps for parsing their own .yaml (but "read-only") files.

Alex Martelli
feels clumsy to me because I am forcing a model to have just a single instance
Plumo
Richard, there is nothing at all wrong with having a singleton that is back by the datastore. The one thing that you really want to consider is that for performance reasons, you'll want to keep this entity in memcache, and write it to datastore whenever it changes.
Adam Crossland
@Adam, I entirely agree -- memcache's not guaranteed to _stay_ around, but it will be faster if it happens to;-). @Richard, point is, the datastore's all you got (for persistent guaranteed storage). If you have many different "bits and pieces" that you need to persist, you could use an amorphous model to store any one of them (as a pickled blob), but that's not necessarily any more elegant.
Alex Martelli
thanks very much for the comments. I will make a class to wrap around the model and keep the data in memcache.
Plumo
Memcache is superfluous. You only have one settings object - store it in a module level or class level variable instead. Nothing is faster than local memory access.
Nick Johnson
@Nick, if you're serving multiple requests at once, each of these simultaneous processes would have to load the config data from the datastore, if it wasn't _also_ in memcache (which they all can read from). So while (of course) using the cooy you already have in memory (if any) is fastest, that doesn't make memcache _superfluous_ it's a helpful addition to the memory+datastore combination for an app with sufficiently-high QPS peaks (doesn't take much: if typical response time is several seconds, even a fractional QPS peak can already have you serving w/multiple processes). Right?
Alex Martelli
@Alex Given the relative infrequency of startup requests to regular requests, memcaching as well as storing it locally won't gain you much. My main point, though, is that Adam's comment suggests memcaching the config, when storing it on the local instance is _much_ faster for something that's small and required by every single request.
Nick Johnson
Alex Martelli