【发布时间】:2012-03-21 19:45:34
【问题描述】:
我找到了许多旧版本 CherryPy 的示例,但它们都引用了在cherrypy 3.2.2 中找不到的导入模块。 Looking in the documentation,我发现了一个关于 storage_type 内置功能的参考(“ram”、“file”、“postgresql”之一)。
【问题讨论】:
我找到了许多旧版本 CherryPy 的示例,但它们都引用了在cherrypy 3.2.2 中找不到的导入模块。 Looking in the documentation,我发现了一个关于 storage_type 内置功能的参考(“ram”、“file”、“postgresql”之一)。
【问题讨论】:
你可以先看看
https://github.com/3kwa/cherrys
这家伙如何编写自己的会话类并覆盖一些方法。他是为 redis 而不是 MySQL 做的。您将为 MySQL 编写方法。 “cherrpy/lib/sessions.py”中的cherrypy中已经存在一个非常相似的类:
class PostgresqlSession(Session)
这与您想要的非常相似。我会说,采用“3kwa”的实现方法,而不是他的 RedisSession 类,而是从“cherrpy/lib/sessions.py”复制 PostgresqlSession 类并更改以匹配正确的 MySQL 语法。
可能的路径是:
从上面的链接下载“cherrys.py”并重命名为“mysqlsession.py”。用“cherrpy/lib/sessions.py”中的“PostgresqlSession(Session)”覆盖“RedisSessions(Session)”并重命名为“MySQLSession(Session)”。一定要加
locks = {}
def acquire_lock(self):
"""Acquire an exclusive lock on the currently-loaded session data."""
self.locked = True
self.locks.setdefault(self.id, threading.RLock()).acquire()
def release_lock(self):
"""Release the lock on the currently-loaded session data."""
self.locks[self.id].release()
self.locked = False
到您的新“MySQLSession”类(就像在 RedisSession(Session) 中完成的一样。更改 PostgreSQL 语法以匹配 MySQL 语法(这应该不难)。将“mysqlsession.py”放在某处在您的项目目录下并在应用程序中导入
import mysqlsession
并使用
cherrypy.lib.sessions.MySQLSession = mysqlsession.MySQLSession
在您的应用程序的初始化中。在配置中
tools.sessions.storage_type : 'mysql'
以及与“PostgreSQL”类一样的参数(如主机、端口等)。
我可能一直都是错的。但这就是我试图解决这个问题的方法。
【讨论】: