且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过PHP和MySQL检查用户的声誉

更新时间:2023-01-17 13:06:27

时间(不是通过使用触发器,而是通过在注释之前查询数据库并在PHP中检查用户的信誉)。

Personally, I've been using the Database Side approach all the time (not by using a trigger but by querying the database before commenting and checking user's reputation in PHP). It will not slow down the performance too much.

但是,如果你真的需要性能好,你可以在内存中缓存用户信誉(不要使用$ _SESSION,它很容易导致不一致问题)。如何使用Redis / Memcached?当用户登录时,将他/她的信誉放在缓存中。当用户的信誉在数据库中更新时,更新(无效)缓存。当你需要弄清楚用户是否具有向下投票的权限,你只需要咨询你的缓存,没有db查询涉及。

However, if you really need the performance to be good, you may cache user reputation in memory (don't use $_SESSION, it will easily lead to inconsistency problems). What about using Redis/Memcached? When the user logs in, put his/her reputation in the cache. When the user's reputation is updated in the database, update(invalidate) the cache too. When you need to figure out whether the user has the permission to down vote, you just need to consult your cache, no db query involved.

缓存的键应该类似于 user_rep_5 (其中5是user_id),而不是 user_rep_xxxx (其中xxxx是会话ID)。我的意思是,缓存应该做全局,以确保只有一个单一的真相来源,而不是将其与会话相关联。

P.S. They key of the cache should look like user_rep_5 (where 5 is the user_id), not user_rep_xxxx (where xxxx is the session id). I mean, the cache should be done globally to make sure there is only one single source of truth, instead of associating it with the session.