English 中文(简体)
数据缓存会造成干扰吗?
原标题:Can data caching cause interference?

我正在为我的PHP站点编写一个新的引擎,该引擎具有数据缓存功能。现在我想知道这些脚本之间是否存在干扰。考虑以下示例:

比方说,许多用户访问了与摄影相关的网站,其中两个用户几乎在同一时间运行两个脚本。

脚本1从数据库中删除一个名为FooBar的用户及其所有照片。

脚本2显示一张编号为12345的照片(由FooBar制作)。

  1. (Script 1) check if user FooBar exists. Store its ID inside a variable $id.
  2. (Script 2) check if a photo nr 12345 exists. Store owner ID inside a variable $owner.
  3. (Script 1) query("DELETE FROM users WHERE id= $id ");
  4. (Script 2) query("SELECT name FROM users WHERE id= $owner "); //error here
  5. (Script 1) query("DELETE FROM photos WHERE owner= $id ");

正如我们所看到的,脚本2检测到照片存在,但其所有者已经被删除。

问题是:PHP脚本是否按顺序处理(因此所呈现的场景是不可能的)?如果没有,在一个脚本中发送多个查询时,我该怎么做才能防止此类错误?

非常感谢。


谢谢你的回答。所呈现的场景只是一个例子,我知道这可以用更好的方式来完成。因此,当创建一个大型网站时,我必须不断意识到数据在运行时可能会发生变化。

但通过这种方式,我根本无法信任php中变量中存储的任何信息。你是如何处理这类问题的?是否在一个查询中更新或获取所有数据?

问题回答

所呈现的场景是可能的。我想说的唯一一件事是为这种可能性编写一个错误代码:如您所示,查询4没有返回有效的所有者。现在你可以做几件事,这些只是例子:

  • return the ownername in a combined query so you can at least show the name if you found a picture s owner (in point 2.). If later on you want to show more, you ll just have to return that the owner does not excist (anymore). That s what happened (it got deleted in between loads), so even if it s a bit weird for the user, he ll have to deal with it :)
  • Just show the error on point 4, a bit like the second part of the previous example. You ve gotten a result very shortly, but then it was gone. So you return "could not load user" or something like that.

事实上,种族状况是可能的。

我要说的是,在删除脚本中删除用户之前的照片让我觉得很奇怪。如果在用户删除之后但在照片删除之前出现了问题,那么数据库仍然有他的孤立照片。通常,事务(使用支持它们的存储引擎,如InnoDB)可以缓解这个问题。

至于竞争条件本身,我相信有一些技术使用行锁定来防止其他进程同时访问它们。

[如果我有机会做一些研究,我会填写更多细节。到目前为止,我在mysql row locking表明一些引擎实际上隐式地锁定了任何受事务影响的行。但我承认在这方面没有直接经验。]





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签