python - 在 Heroku 上使用 Memcache 时,我应该配置 Beaker 的 `session.lock_dir` 吗?

标签 python heroku memcached pyramid beaker

我正在将 Pyramid 应用程序的 session 配置从 cookie 切换到 ext:memcached。我的应用程序托管在 Heroku 上,我已经配置了他们的 memcache addon根据 their documentation .

我从Beaker documentation了解到指定 session.lock_dir 对于防止 dog pile effect 是必不可少的.明确地说:我必须提供目录的文件路径。 Beaker 然后将其用作某种锁,以防止多个客户端同时尝试设置相同的值。

对我来说,这听起来像是糟糕的架构。 Memcache 的主要优点之一是它作为共享的外部服务工作。将我的应用程序进程绑定(bind)到磁盘绑定(bind)锁似乎是错误的方法。

同样,在 Heroku 上,我有一个 ephemeral每个“dyno”的文件系统(我理解是每个进程的意思)。因此,虽然我可以提供一个 lock_dir 目录路径,但如果每个进程使用不同的目录,那么这会保护我免受狗堆效应的影响吗?

我不确定我是否应该:

  • 指定一个 lock_dir 而不用担心它
  • 尝试 fork Beaker 并以某种方式对其进行修补

另外,我很想知道其他语言/框架在这里使用什么模式。这只是一个 Beaker 问题还是其他非文件绑定(bind)设置会受到狗桩效应的影响?

提前致谢

詹姆斯。

最佳答案

不幸的是,根据我的经验,由于缺少分布式锁,Beaker 在多主机基础设施上运行不佳。如果您在 2 台主机上运行的网络应用程序中使用烧杯,狗堆效应仍然会发生(但是可以通过将来自同一用户的所有请求粘贴到一台服务器来避免这种情况,但它不适用于所有平台)。

有时您可以忽略狗堆效应,有时则不能。例如。如果你缓存全局数据,狗堆效应是痛苦的。如果缓存用户数据,有时可以忽略它。

在你的情况下,我会忽略它。 Beakers 允许您在没有任何锁定目录的情况下使用 Memcache 扩展。在这种情况下,Beaker 锁仅使用线程锁按进程工作。

证明如下:

from beaker.middleware import SessionMiddleware
from werkzeug.wrappers import Response
from werkzeug.serving import run_simple


def simple_app(environ, start_response):
    # Get the session object from the environ
    session = environ['beaker.session']

    # Check to see if a value is in the session
    user = 'user_id' in session

    # Set some other session variable
    session['user_id'] = 10
    session.save()

    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['User is logged in: %s' % user]

# Configure the SessionMiddleware
session_opts = {
    'session.type': 'ext:memcached',
    'session.url': '127.0.0.1:11211',
    'session.auto': True,
    'session.cookie_expires': True,
}
wsgi_app = SessionMiddleware(simple_app, session_opts)


if __name__ == '__main__':
    run_simple('127.0.0.1', 5000, wsgi_app, use_debugger=True, use_reloader=True)

如果 dog pile effect 仍然是您的问题,我建议您从 Beaker 迁移到其他工具。例如。 dogpile.cache

关于python - 在 Heroku 上使用 Memcache 时,我应该配置 Beaker 的 `session.lock_dir` 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11833245/

相关文章:

python - pandas 为每一行绘制线段

git - SSL 证书 : Invalid certificate chain

algorithm - Memcache 驱逐监控系统

python - 获取缓存结果后的 Django/Python 排序

ruby - 安装 ruby​​ gem : memcached 后看不到二进制文件

python - 使用 nltk 的 CSV 中最常见的 ngram

python - 大文件中的字符串插入

python - 如何与向量一起遍历矩阵?

node.js - 你如何在同一个端口上运行 express 和 socket.io

email - 在 Heroku 上接收电子邮件