mò %ÜGc@s'dZdkZdkZdkZdkZdkZdkZdklZdk l Z l Z dk Z dk Z dddZ defd„ƒYZdfd „ƒYZhZd efd „ƒYZd efd „ƒYZd„Zde efd„ƒYZdadkZdkZdkZd„ZdS(s Module Use ========== Use the session by calling the get(), put(), and delete() methods. The following example stores, uses, and then deletes the username: In other words, it is very similar to a dictionary. You can pretend the session module is just a dictionary specific to the user accessing the page. The session "dictionary" contents will change automatically to match the user's values for the browser accessing your page. IMPORTANT NOTE 1: You should only put small, temporary objects into the session. While the session will take anything that can be pickled, remember that it must be stored in a dbm hash. Large objects will slow things down considerably; avoid them. Store strings, ints, and simple objects. For example, if you want to store a User object, store the primary key (user id) to the User object. On each request, reload the User object from your database using the user id stored in the session. IMPORTANT NOTE 2: This module adds a _lastaccess key to each session. It uses this value to know when the session needs to be cleaned out. Don't use this key name. In other words, don't call session['_lastaccess'] = anything. IMPORTANT NOTE 3: If you are running Spyce in mod_python, cgi, or fastcgi installation modes, you *should not* use the memory option. Use the dbm method for these installation modes. While dbm is slower and takes disk space, an in-memory cache will cause update errors since you'll get multiple caches! This is because mod_python, cgi, and fastcgi create multiple instances of your application -- you'll have an cache in *each* instance. In addition, dbm-method sessions survive a server reboot, memory-method sessions do not. The Internals ============= Sessions are regular Python dictionaries. The standard python "shelve" module is used to save these dictionaries to disk. Since sessions are temporary and shouldn't be moved from installation to installation, the module uses the highest possible pickle protocol for speed. Whenever a session is saved to a file, the last checked timestamp is used to see if the file should be cleaned. If enough time has passed, all old sessions are deleted from the shelf. The module cleans every CLEAN_INTERVAL seconds (set to every 24 hours right now). If you want a different CLEAN_INTERVAL, just change this constant at the top of the file. I didn't make this accessible publicly because I don't think most users care how often it cleans sessions out. Note that files are not actually ever cleaned until something is set within them. Sessions are never actually created until your program puts data in them. In other words, they are created just in time. Since many site visitors may never get session data set, this provides a great efficiency since no disk access is required for them. Your program will think they have a session, but as long as you don't call put(), they won't get a session internally. The module is thread-safe. A series of file locks is used to lock shelves when they are being used or cleaned. However, if multiple instances of Spyce are created, it is remotely possible that sessions might blast each other. I'm not sure how to solve this without additional disk access. If anyone wants to help here please do. (note that the regular session module has this problem, too.) N(s DictMixin(s spyceModules moduleFinderi<itSessionNotFoundErrorcBstZRS(N(t__name__t __module__(((t3/home/holguin2/public_html/spyce/modules/session.pyRtst SessionStorecBs>tZd„Zd„Zd„Zd„Zd„Zd„ZRS(NcCstiƒ|_dS(N(ttimetselft _last_cleaned(R((Rt__init__xscCs tƒ‚dS(N(tNotImplementedError(Rt sessionidtexpires((RtloadzscCs tƒ‚dS(N(R (RR tsessionR ((Rtsave}scCs tƒ‚dS(N(R (RR ((RtclearscCs tƒ‚dS(N(R (RR ((Rt is_expiredscCs tƒ‚dS(N(R (R((Rtlistƒs(RRRR RRRR(((RRws      t MemoryStorecBs5tZd„Zd„Zd„Zd„Zd„ZRS(NcCsti|hd|<ƒS(Nt_expires(t memory_cachetgetR R (RR R ((RR ‰scCstiƒ|d<|t|¦sid(tspycet getServerRtgent spyceLockt MultiLockRtlock(RR"R((RRt_init¢s  cCstii|id|ƒS(Nt spysession(tostpathtjoinRRR (RR ((Rt _filename¨scCs’t|dƒp|iƒn|i|ƒ}|ii|ƒz"ti |dt dt i ƒ}Wd|ii|ƒXd|jo||d