Memcache with transactions?

Although I couldn’t find anything, I thought I would double check if memcache transactions are supported?

If not, then what I'm betting is the likely answer, then what is the correct method for working with memcache in a transactional environment? Didn't you have to read from the database every time you plan to update, even if the data is in the cache, just so you can set your locks? For example, a script that updates some data would look like this:

  • TO BEGIN; SELECT ... FOR UPDATE;
  • Calculate ...
  • UPDATE TABLE ...;
  • Refresh Cache
  • COMMIT;

I think you need to update the cache after running your update request, if you are at a dead end and you need a rollback. But you must also update the cache before committing, in case any other thread expects to read your data, and may accidentally update it with a cache of even newer data to you, as a result of which your obsolete data will be overwritten.

Is this the correct sequence of steps? Is there any way not to get into db for reading to update?

+4
source share
2 answers

Memcache has a CAS (Check And Set - or Compare And Swap) statement that can help you. The PHP manual has documentation for it, Memcached :: cas , but it should also be supported in other libraries and languages.

+4
source

Memcached does not support transactions in this sense, although its operations are atomic. You can use the database transaction mechanism and update cache manually (as indicated) or use a transactional wrapper such as this .

0
source

Source: https://habr.com/ru/post/1342199/


All Articles