会议

Pyramid 和 Pylons 一样使用烧杯会话,但默认情况下不会启用它们。要使用它们,您必须添加“ Pyramid 烧杯”包作为依赖项,并在您的 main() 功能:

config.include("pyramid_beaker")

(To add a dependency, put it in the requires 在setup.py中列出,然后重新安装应用程序。)

The default configuration is in-memory sessions and (I think) no caching. You can customize this by putting configuration settings in your INI file or in the settings 开始时的口述 main() function (before the Configurator is instantiated). Akhet演示使用从 Pylons 配置中借用的以下设置配置烧杯:

 1# Beaker cache
 2cache.regions = default_term, second, short_term, long_term
 3cache.type = memory
 4cache.second.expire = 1
 5cache.short_term.expire = 60
 6cache.default_term.expire = 300
 7cache.long_term.expire = 3600
 8
 9# Beaker sessions
10#session.type = file
11#session.data_dir = %(here)s/data/sessions/data
12#session.lock_dir = %(here)s/data/sessions/lock
13session.type = memory
14session.key = akhet_demo
15session.secret = 0cb243f53ad865a0f70099c0414ffe9cfcfe03ac

要使用类似于pylons中的基于文件的会话,请取消对前三个会话设置的注释,并注释掉“session.type=memory”行。

您应该将“session.secret=”设置为随机字符串。它用于对会话cookie进行数字签名,以防止会话劫持。

烧杯有几个可用的持久性后端,包括内存、文件、SQLAlchemy、Memcached和cookies(它们将每个会话变量存储在客户端cookie中,并且具有大小限制)。现在最流行的部署后端是memcached,它可以作为多个进程和服务器之间的共享存储,从而提供了内存速度和扩展到多服务器集群的能力。 Pylons 默认为基于磁盘的会话。

Beaker plugs into Pyramid's built-in session interface, which is accessed via request.session . Use it like a dict. Unlike raw Beaker sessions, you don't have to call session.save() every time you change something, but you should call session.changed() if you've modified a mutable item in the session; e.g., session["mylist"].append(1) .

The Pyramid session interface also has some extra features. It can store a set of "flash messages" to display on the next page view, which is useful when you want to push a success/failure message and redirect, and the message will be displayed on the target page. 它是基于 webhelpers.flash 这与 Pyramid 是不相容的,因为它依赖于 Pylons 的魔法球。还有一些方法可以设置安全表单令牌,防止表单提交不是来自会话早期请求的表单(因此可能是跨站点伪造攻击)。(注意:Flash消息与Adobe Flash电影播放器无关。)

Sessions chapter in the Pyramid manual for the API of all these features and other features. The Beaker manual will help you configure a backend. The Akhet Demo is an example of using Pyramid with Beaker, and has flash messages.

*注意:*当启用会话时,我有时会在调试工具栏中得到一个异常。它们可能是发行版之间的代码差异。如果发生这种情况,可以禁用工具栏,直到问题得到解决。