We can now use our
store variable to set and retrieve values from our
testApp application's session store. Below is a demonstration of the functional interface:
store = session.store('testApp') >>> store.set(key='first',value='This is the fist key to be set!') >>> print store.get(key='first') This is the fist key to be set! >>> print store.keys() ['first'] >>> store.delete(key='first') >>> store.has_key(key='first') 0
Alternatively we can treat the
store object as a dictionary:
>>> store['first'] = 'This is the fist key to be set!' >>> print store['first'] This is the fist key to be set! >>> print store.keys() ['first'] >>> del store['first'] >>> store.has_key('first') 0
Both versions behave in exactly the same way and any Python value that can be pickled by the pickle module can be set and retrieved from the store so you can store strings, numbers and even classes and all the information will be available for each request until you remove it or the session expires.
One other useful method of the
Store object is the empty() method. This is used to remove all information from an application's session store. This is a better way of removing information than using the
Manager's destroy() method since destroy() will also remove all the information from other application's stores which might cause those applications to crash if the store is currently being accessed.