在社交网络环境中,检查全球好友的最简单方法是什么?

发布于 2024-12-08 21:51:57 字数 693 浏览 0 评论 0原文

作为参考,这是我最近问的一个与此问题相关的问题:如何模型友谊关系

在这个问题上,我们找到了一种方法,仅显示朋友发布的新闻提要项目。然而,我需要的是如何以更动态的方式检查友谊的建议,以便它可以在多个站点功能中使用。

例如,我刚刚安装了一个评论系统,允许用户对新闻帖子发表评论。然而,这并不限于朋友,而且应该如此(并且稍后应由帖子的作者将其设为可选)。

仅由朋友发布新闻项目略有不同,因为我直接从数据库获取数据,并使用 SELECT 子查询仅获取当前用户的朋友发布的帖子。但是,在评论的示例中,我只想在该人是朋友的情况下显示评论帖子表单。我实际上并没有从数据库中提取任何内容来进行选择、排序和显示。

知道类似的问题会在整个网站中多次出现,检查友谊的最简单方法是什么? 我能否以某种方式将所有朋友的用户 ID 从数据库中提取到某种会话数组中,然后执行 if(in_array($friends)) 每当我需要确定相关人员是否是当前登录用户的朋友? 从我的角度来看,这听起来似乎可以正常工作,但我首先需要您的输入。

我上面链接的问题解释了我的友谊表是如何工作的,以防你帮助我解决这个问题。

For reference, here is a question on SO that I asked recently that is relevant to this question: How to model Friendship relationships

On that question, we figured out a way to display news feed items only if posted by friends. However, what I need is advice on how to check for friendship in a more dynamic way so it can be used throughout several site functions.

For example, I've just installed a comment system, allowing users to post comments on news posts. However, this isn't restricted to friends, and it should be (and later should be made optional by the author of the post).

Posting news items only by friends was slightly different because I was getting data directly from the database, and using SELECT subqueries to only get posts by friends of the current user. However, in the example of the commenting, I only want to display the comment post form if the person is a friend. I'm not actually pulling anything from the database to pick, sort, and display.

Knowing issues like this will arise many times throughout the site, what would be the simplest way to check for friendship? Could I somehow pull all the friends' user IDs from the database into a session array of some kind and then do an if(in_array($friends)) whenever I needed to determine if the person in question is a friend of the currently logged in user? Off the top of my head, this sounds like it would work fine, but I'd like your input first.

The question I linked to above explains how my friendship table works, in case that help you help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-12-15 21:51:57

实际上,将友元数组存储在会话中是一个非常糟糕的主意。如果在创建会话变量后添加新朋友,会发生什么情况?它不会在会话中更新。

由于您将在同一页面上多次检查朋友列表,因此为什么不直接查询并将其存储在本地数组中,以便您可以在同一页面上继续使用。

当页面执行完毕时,该数组将被丢弃。

所以基本上,您只会查询该列表一次。

推荐的实现是遵循上述注释中“pst”的建议,每次需要先查找关系时只需查询,因为这很容易实现。后来,当查询速度开始成为问题时,您可以更改该方法的内部,以便在第一次调用该方法时将好友列表缓存在本地数组中,以加快速度。 (将内存使用量换成处理器使用量)。

Actually, it is a very bad idea to stor the friend array in the session. What happens if some you add a new friend after the session variable is created? It does not get updated in the session.

Since you will be checking the list of friends a lot of times on the same page, why not just query it out and store it in a local array that you can keep using on the same page.

When the page finish executing, the array will be discarded.

So basically, you will only query the list out once.

A recommended implementation would be to follow the advise of "pst" in the above comment, and just query for every time you need to find the relationship first, as that is simple to implement. Later, when the speed of the query starts to become a issue, you can just change the internal of that method to cache the friend list in a local array when it is first called to speed things up. (exchange memory usage for processor usage).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文