简体   繁体   English

将会话数据存储在数据库和内存缓存中

[英]Storing session data in database AND memcache

Google App Engine for PHP stores session data in memcache, which is nice and fast (and cheap), but session data from memcache seems to be clearing out after 8-10 min, which is much less than I need it. 用于PHP的Google App Engine将会话数据存储在memcache中,这很好,速度快(而且便宜),但来自memcache的会话数据似乎在8-10分钟后清除,这比我需要的要少得多。 Following Google's advice, 根据谷歌的建议,

...data in App Engine memcache may be flushed periodically, meaning any session information will be lost. ... App Engine内存中的数据可能会定期刷新,这意味着任何会话信息都将丢失。 For longer-lived sessions, it may be preferable to use an alternative storage service such as Cloud SQL. 对于寿命较长的会话,最好使用其他存储服务,例如Cloud SQL。 ( https://developers.google.com/appengine/docs/php/#PHP_Sessions ) https://developers.google.com/appengine/docs/php/#PHP_Sessions

I'm using Zend_Session_SaveHandler_DbTable to store session data in the database. 我正在使用Zend_Session_SaveHandler_DbTable将会话数据存储在数据库中。 However, the downside to this, as far as I can tell, is having to access the database every page request (with latency and cost both going up). 但是,据我所知,其缺点是每个页面请求都必须访问数据库(延迟和成本都会上升)。

my question 我的问题

Is there a way to persist session data in the database but also use memcache so the database isn't hit every single time? 有没有办法在数据库中保存会话数据,但也使用内存缓存,因此数据库不会每次都被点击? (I'm using Zend Framework 1.12) (我正在使用Zend Framework 1.12)

Sure. 当然。 Just specify a custom session_set_save_handler that saves to both, and checks memcache first for loads, falling back to the database if it's not found. 只需指定一个自定义的session_set_save_handler ,它会保存到两者,并首先检查memcache是​​否有负载,如果找不到则返回数据库。

Now, the session will still need to be saved to both (otherwise you might lose data when memcache is cleared), so the cost will probably remain similar, but it'll reduce latency since session saves are after the page is rendered, and thus not included in user-visible latency. 现在,会话仍然需要保存到两者(否则你可能会在清除memcache时丢失数据),因此成本可能会保持相似,但它会减少延迟,因为会话保存是在页面呈现之后,因此不包括在用户可见的延迟中。 It'll also reduce the number of reads needed to load the session. 它还会减少加载会话所需的读取次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM