Project

General

Profile

PageMaker » History » Version 1

Version 1/17 - Next » - Current version
Elmer de Looff, 2012-04-23 19:08
PageMaker talk about the persistent storage.


PageMaker

Persistent storage between requests

µWeb allows you to store objects in a process-persistent storage. This means that the storage will be properly persistent and available when µWeb is in standalone mode. When running on top of apache, this persistence is only as good as the apache process, which is typically a couple hundred to a few thousand requests.

Default users of the persistent storage

By default, the TemplateParser and the various database connectors are stored in the persistent storage. This has the benefit that pre-parsed templates will not need to be read from disk on subsequent requests. For databases the benefit is that connections need not be made on-the-fly, but can mostly be retrieved from the storage.

Storing persistent values

Storing persistent values is done with the Set method, as follows:

def _PostInit(self):
  if 'connection' not in self.persistent:
    self.persistent.Set('connection', self._MakeConnection())

In the example above, the database connection is only created, and added to the persistent storage, if it's not already present. This way expensive but reusable actions can be optimized by performing them only once (or once every few so many requests, if running on Apache).

Retrieving persistent values

Retrieving stored values works much like this, but uses the Get method:

def DatabaseAccess(self):
  with self.persistent.Get('connection') as cursor:
    cursor.Execute('INSERT INTO `message` SET `text` = "success!"')

This uses the connection we created (or still had) during _PostInit, and uses it to update the database.

In case a key has is not present in the persistent storage (because it wasn't set in the process' lifetime or because it was exlicitly dropped), the Get method has an optional second argument, that is returned when the key is not present:

def FirstVisit(self):
  when = self.persistent.Get('first_visit_time', 'just now')
  return 'Your first visit was %s.' % when

This will return the stored date and time when there was a previously recorded visit, or the text just now if there was no previous time logged.

Finally, the persistent storage has a SetDefault method, that acts much like the similarly named dictionary method. It returns the value for the given key, but if it's not present, it will set the key to the provided value, and return it as well. With this, we can improve on our first-visit tracker, and in one call retrieve or store the first time someone visited:

def FirstVisit(self):
  when = self.persistent.SetDefault('first_visit_time', datetime.datetime.now())
  return 'Your first visit was %s.' % when

Deleting persistent values

If for any reason you need to delete a value from the persistent storage, this can be done using the Del method. The given key name is removed from the storage. N.B.: If the key was already removed from the storage (this can happen if the delete code runs more than once, or the key was not defined in the process' lifetime), no error is raised. It is assumed that removing the key is the only desired action.

def DeletePersistentKey(self, key):
  self.persistent.Del(key)