PHP 中发生事件时同步不同的用户会话
我正在编写一个社交网络应用程序,我想同步不同的用户会话。
我的上下文如下:
userA 已登录,并且在会话中拥有一系列联系人。 userB 已登录并在会话中拥有一系列联系人。
userA 将 userB 添加为联系人。 因此,联系人表(DB)看到一条新记录 并且 userA 更新了他的联系人数组(很容易,因为事件发生在 userA 导航端)
我的需求如下:
我现在想自动同步 userB 的会话,以便刷新他的联系人数组 无需执行 sql 查询来检查联系人表 (db)。我希望这样做是为了避免进行太多的 sql 查询来检查这一点。
您有什么建议吗?
我正在使用 CodeIgniter (PHP),并且会话是通过数据库管理的。
多谢
I’m writing a social networking application and I want to synchronize different user sessions.
My context is following:
userA is logged and has an array of his contacts in session.
userB is logged and has an array of his contacts in session.
userA adds userB as a contact.
So, Contact table (DB) sees a new record
And userA has an update of his array of contacts (easy because the event happens on userA navigation side)
My need is following:
I want now to synchronize automatically userB’s session so that his array of contacts is refreshed
without doing a sql query to check the contacts table (db). I want that in order to avoid making too many sql queries to check that.
Do you have any suggestions?
I’m using CodeIgniter (PHP) and the session is managed with database.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不执行 sql 查询来检查联系人表,这是不可能的。即使您将会话保存在 sql 表中,您仍然需要查询数据
This is not possible without doing a sql query to check the contacts table. Even if you hold your sessions in sql table you still have to query for the data
您想要更新数据副本(userAs 会话)而不引用主数据集。这是行不通的。
由于它的无共享架构,这将很难在 PHP 中实现。
在任何接近实时的情况下实现这一点的唯一明智的方法是为每个当前用户会话维护一个信号量 - 并在应重新加载会话数据时设置该信号量。此时您必须从数据库中读取数据。
其中大部分工作可以在会话处理程序中完成 - 这可能是处理它的最明智的地方 - 但您需要注意信号量因此是易失性的并对其进行适当的编码(例如,在读/写信号量时使用乐观锁定) 。
You want to update a copy of the data (userAs session) without refering to the master set of data. This is just not going to work.
With it's shared nothing architecture, this is going to be hard to implement in PHP.
The only sensible way to achieve this in anything approaching realtime is to maintain a semaphore for each current user session - and set that semaphore when the session data should be re-loaded. And at that point you must read the data from the database.
Much of this could be done within the session handler - which is probably the most sensible place to handle it - but you need to be aware that the semaphore is therefore volatile and code it appropriately (e.g. using optimistic locking around reading/writing the semaphore).