| 10 | | <section id="sessionfeaturs"> |
|---|
| 11 | | <title>Session Features</title> |
|---|
| 12 | | <itemizedlist> |
|---|
| 13 | | <listitem>Multiple Storage Types</listitem> |
|---|
| 14 | | <listitem>Thread Safety</listitem> |
|---|
| 15 | | <listitem>Easy to extend</listitem> |
|---|
| 16 | | <listitem>Session Caching</listitem> |
|---|
| 17 | | <listitem>Multiple Named Sessions</listitem> |
|---|
| 18 | | </itemizedlist> |
|---|
| 19 | | </section> |
|---|
| 20 | | <section id="sessionconfig"> |
|---|
| 21 | | <title>Session Configuration</title> |
|---|
| 22 | | </section> |
|---|
| 23 | | <section id="usingsessions"> |
|---|
| 24 | | <title>Using Sessions</title> |
|---|
| 25 | | <section> |
|---|
| 26 | | <title>SessionDicts</title> |
|---|
| 27 | | <para> All session data is accessed through an instance of a SessionDict class. SesssionDicts |
|---|
| 28 | | provide a dictionary like interface. </para> |
|---|
| 29 | | <para> The following dictionary methods are provided: <itemizedlist> |
|---|
| 30 | | <listitem>__setitem__(key, value)</listitem> |
|---|
| 31 | | <listitem>__getitem__(key) </listitem> |
|---|
| 32 | | <listitem>get(key, default) </listitem> |
|---|
| 33 | | <listitem>__iter__()</listitem> |
|---|
| 34 | | </itemizedlist> SessionDicts also provide the following attributes: <itemizedlist> |
|---|
| 35 | | <listitem>sessionKey (read only)</listitem> |
|---|
| 36 | | <listitem>lastAccess (read only)</listitem> |
|---|
| 37 | | <listitem>createdAt (read only)</listitem> |
|---|
| 38 | | <listitem>timeout </listitem> |
|---|
| 39 | | </itemizedlist> |
|---|
| 40 | | </para> |
|---|
| 41 | | </section> |
|---|
| 42 | | <!-- end SessionDict section --> |
|---|
| 43 | | <section> |
|---|
| 44 | | <!-- cherrypy.sessions section --> |
|---|
| 45 | | <title>cherrypy.session</title> |
|---|
| 46 | | <para> cherrypy.session is the single point of access for all session data. At the beginning |
|---|
| 47 | | of each request, the sessionFilter checks for any sessions that may have been defined for |
|---|
| 48 | | the requested portion of the site. For each defined session, a SessionDict is created and is |
|---|
| 49 | | added as an attribute of cherrypy.session. </para> |
|---|
| 50 | | <para> For example, given a session named "checkoutSession", all data for this session will be |
|---|
| 51 | | accessible through: "cherrypy.sessions.checkoutSession" </para> |
|---|
| 52 | | <example> |
|---|
| 53 | | <title>cherrypy.session</title> |
|---|
| 54 | | <para> This code iterates over a list of items in a shopping cart, and computes The total |
|---|
| 55 | | cost of all items. </para> |
|---|
| 56 | | <programlisting> |
|---|
| 57 | | totalCost = 0.0 |
|---|
| 58 | | |
|---|
| 59 | | for item in cherrypy.sessions.checkoutSession['shoppingCart']: |
|---|
| 60 | | totalCost += item.cost |
|---|
| 61 | | </programlisting> |
|---|
| 62 | | </example> |
|---|
| 63 | | <section> |
|---|
| 64 | | <title>The default session</title> |
|---|
| 65 | | <para> By default, the sessionFilter provides a session named "default", which is accessed |
|---|
| 66 | | through "cherrypy.sessions.default". As a shortcut the ".default" can be ommited and |
|---|
| 67 | | "cherrypy.sessions" will behave like a dictionary. Session attributes, such as |
|---|
| 68 | | "cookieName", can only be retrieved through cherrypy.sessions.default. </para> |
|---|
| 69 | | <para> Unless there is a clear advantage to having more that one named session, the default |
|---|
| 70 | | session should be used to store all session data. The default session may (in the future) |
|---|
| 71 | | be optimized to provide superior performance than that of custom named sessions. </para> |
|---|
| 72 | | </section> |
|---|
| 73 | | <!-- end default session section --> |
|---|
| 74 | | </section> |
|---|
| 75 | | <!-- end of cherrypy.sessions section --> |
|---|
| 76 | | <section> |
|---|
| 77 | | <!-- session configuration --> |
|---|
| 78 | | <title>Session Configuration</title> |
|---|
| 79 | | <itemizedlist> |
|---|
| 80 | | <listitem> |
|---|
| 81 | | <section> |
|---|
| 82 | | <title>sessionFilter.on</title> |
|---|
| 83 | | <para> Can be True or False. Will toggle on/off all sessions within the current path. By |
|---|
| 84 | | default the session filter is turned off. </para> |
|---|
| 85 | | </section> |
|---|
| 86 | | </listitem> |
|---|
| 87 | | </itemizedlist> |
|---|
| 88 | | <para> These settings are common to all storage types. </para> |
|---|
| 135 | | <example> |
|---|
| 136 | | <title>Session Configuration</title> |
|---|
| 137 | | <para> </para> |
|---|
| 138 | | <programlisting> |
|---|
| 139 | | class MyBBS: |
|---|
| 140 | | # create a session named 'admin' using the SessionStorageClass |
|---|
| 141 | | def __init__(self): |
|---|
| 142 | | cherrypy.config.update({ |
|---|
| 143 | | 'global' : { 'sessionFilter.on' : True }, |
|---|
| 144 | | '/site/bbs' : { |
|---|
| 145 | | 'sessionFilter.sessionsList' : ['bbs'], |
|---|
| 146 | | 'sessionFilter.bbs.storageType' : 'ram', |
|---|
| 147 | | 'sessionFilter.bbs.timeout' : 25 |
|---|
| 148 | | } |
|---|
| 149 | | }) |
|---|
| 150 | | def admin(self): |
|---|
| 151 | | if cherrypy.session.admin['user'] != "root": |
|---|
| 152 | | return "you are not root" |
|---|
| 153 | | admin.exposed = True |
|---|
| 154 | | </programlisting> |
|---|
| 155 | | </example> |
|---|
| 156 | | </section> |
|---|
| 157 | | <!-- end sessionconfiguration --> |
|---|
| 158 | | </section> |
|---|
| 159 | | <section id="sessionstoragetypes"> |
|---|
| 160 | | <title>Session Storage Types</title> |
|---|
| 161 | | <para>CherryPy includes several session storage backends: ram, file, anydb, sqlobject</para> |
|---|
| 162 | | <section> |
|---|
| 163 | | <title>ram storage</title> |
|---|
| 164 | | <para> The ram adaptor stores all session data in memory. It is the only storage adaptor |
|---|
| 165 | | capable of holding any Python object, including those that are not-picklable, such as file |
|---|
| 166 | | handles, sockets, and running generators. When using ream sessions, keep in mind that all |
|---|
| 167 | | session data is lost when the server shuts down, even during auto-reloads. </para> |
|---|
| 168 | | <para> The ram adaptor has no additional configuration options. </para> |
|---|
| 169 | | </section> |
|---|
| 170 | | <!-- end ram storage section --> |
|---|
| 171 | | <section> |
|---|
| 172 | | <title>file storage</title> |
|---|
| 173 | | <para> The file storage adaptor pickles the data for each session and stores it in separate |
|---|
| 174 | | files. Cleaning up expired sessions is a slow operation, because it requires unpickling |
|---|
| 175 | | every session file. </para> |
|---|
| 176 | | <itemizedlist> |
|---|
| 177 | | <listitem>Additional Configuration Options |
|---|
| 178 | | <itemizedlist> |
|---|
| 179 | | <listitem>storagePath : The path to the directory where the session files will be |
|---|
| 180 | | stored. The default value is ".sessiondata" |
|---|
| 181 | | </listitem> |
|---|
| 182 | | </itemizedlist> |
|---|
| 183 | | </listitem> |
|---|
| 184 | | </itemizedlist> |
|---|
| 185 | | </section> |
|---|
| 186 | | <!-- end file storage section --> |
|---|
| 187 | | <section> |
|---|
| 188 | | <title>anydb storage</title> |
|---|
| 189 | | <para> The anydb storage adaptor stores session data using the shelve module, which in turn |
|---|
| 190 | | uses the anydb module. The andyb adaptor can only be used to store picklable objects. </para> |
|---|
| 191 | | <itemizedlist> |
|---|
| 192 | | <listitem>Additional Configuration Options |
|---|
| 193 | | <itemizedlist> |
|---|
| 194 | | <listitem>storagePath : The path to the directory where the database files will be stored. By default, |
|---|
| 195 | | the filter will create a file based on the session name. |
|---|
| 196 | | </listitem> |
|---|
| 197 | | <listitem>dbFile : An absolute path pointing to the database file to use with the session. |
|---|
| 198 | | </listitem> |
|---|
| 199 | | </itemizedlist> |
|---|
| 200 | | </listitem> |
|---|
| 201 | | </itemizedlist> |
|---|
| 202 | | </section> |
|---|
| 203 | | <!-- end anydb storage section --> |
|---|
| 204 | | <section> |
|---|
| 205 | | <title>Relational Storage</title> |
|---|
| 206 | | <para> With relational database adaptors, all data is stored in a relational database table. |
|---|
| 207 | | Due to the nature of relational databases, there are several restrictions on how data can be |
|---|
| 208 | | stored. Each session is stored as a single row in the session table. The session table must |
|---|
| 209 | | be created with a column for each variable needed by the application. This makes it |
|---|
| 210 | | impossible to store arbitrary key/value pairs, as can be done using several of the other |
|---|
| 211 | | storage adaptors. In addition, relational databases also place restrictions on the type of |
|---|
| 212 | | data that can be stored in each column. Attempting to store the wrong type of data in a |
|---|
| 213 | | session variable will result in an error. </para> |
|---|
| 214 | | <para> Despite these restrictions, storing session data in relational database has many |
|---|
| 215 | | advantages. This approach allows session data to be integrated into an existing relational |
|---|
| 216 | | database, which dynamic web applications often use anyway. This approach allows for a level |
|---|
| 217 | | of scalability not possible with other storage adaptors. In addition, by using a networked |
|---|
| 218 | | database server, session data can be easily shared between a cluster of web servers. </para> |
|---|
| 219 | | <section> |
|---|
| 220 | | <title>sqlobject storage</title> |
|---|
| 221 | | <para> The sqlobject adaptors can be used with any database supported by sqlobject. </para> |
|---|
| 222 | | </section> |
|---|
| 223 | | <!-- end sqlobject storage section --> |
|---|
| 224 | | <section> |
|---|
| 225 | | <title>pydo storage</title> |
|---|
| 226 | | <para> A PyDO2 adaptor will be included in the future. </para> |
|---|
| 227 | | </section> |
|---|
| 228 | | </section> |
|---|
| 229 | | <!-- end relational storage section --> |
|---|
| 230 | | </section> |
|---|
| 231 | | <!-- end sessionstoragetyps section --> |
|---|
| 232 | | <section id="customsessionstorage"> |
|---|
| 233 | | <title>Writing Custom Session Storage Drivers</title> |
|---|
| 234 | | <section> |
|---|
| 235 | | <title>BaseSessionDict</title> |
|---|
| 236 | | <para> BaseSessionDict is the base class for the dictionary-like class used to access session |
|---|
| 237 | | data. SessionDicts implement the most frequently used features of Python dictionaries. DO |
|---|
| 238 | | NOT add new features to a custom SessionDict class, as it could break compatibility with |
|---|
| 239 | | other storage adaptor. Sometimes it is not necessary to write a custom SessionDict class |
|---|
| 240 | | because the sessionFilter includes a SimpleSessionDict. The SimpleSessionDict is used by |
|---|
| 241 | | several storage adaptor and may be adequate for a custom storage adaptor. </para> |
|---|
| 242 | | </section> |
|---|
| 243 | | <section> |
|---|
| 244 | | <title>BaseAdaptor</title> |
|---|
| 245 | | <para> BaseSession is the base class for all session storage adaptor. There are a number of |
|---|
| 246 | | methods that must be implemented by every storage adaptor. </para> |
|---|
| 247 | | </section> |
|---|
| 248 | | </section> |
|---|