Best way to handle concurrency issues

后端 未结 11 1183
迷失自我
迷失自我 2021-02-07 23:27

i have a LAPP (linux, apache, postgresql and php) environment, but the question is pretty the same both on Postgres or Mysql.

I have an cms app i developed, that handle

11条回答
  •  暖寄归人
    2021-02-07 23:43

    I agree that I probably wouldn't hit the database for this. I suppose I would use APC cache (or some other in-memory cache) to maintain this information. What you are describing is clearly optimistic locking at the detailed record level. The higher the level in the database structure the less you need to deal with. It sounds like you want to check with multiple tables within a structure.

    I would maintain a cache (in APC) of the IDs and the timestamps of the last updated time keyed by the table name. So for example I might have an array of table names where each entry is keyed by ID and the actual value is the last updated timestamp. There are probably many ways to set this up with arrays or other structures but you get the idea. I would probably add a timeout to the cache so that entries in the cache are removed after a certain period of time - i.e., I wouldn't want the cache to grow and assume that 1 day old entries aren't useful anymore).

    With this architecture you would need to do the following (in addition to setting up APC):

    • on any update to any (applicable) table, update the APC cache entry with the new timestamp.

    • within ajax just go as far "back" as php (to obtain the APC cache to check the entry) rather than all of the way "back" to the database.

提交回复
热议问题