We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Increment a value in memcache

Hi. How to increment a value in memcache ? There is no method increment or decrement which should do appropriate action as atomic operation

Due to these docs https://docs.phalcon.io/en/latest/api/Phalcon_Cache_Backend_Memcache.html

I dont use to work with memcache, but you can get the content, increment it, save it again with the new value.

You can also do a function/method that would do it for you. For instance by extending the Phalcon memcache adapter and add a method increment("key"). Then instead of using the base class, you use your one memcache class.

The first problem - get the content - increment - save - in 100 threads you will get race condition and real fuckup in counter :)

As for extend memcache class - it's not a problem - but in this case I have to make my own connection to memcache and I will have an overhead (one connection from Phalcon, and one from my class). The best way is to implement native increment/decrement memcache methods in Phalcon. I have made i ticket for it.

Or I can create my own memcacheClass with needed methods. But it is not "true" programming :)

I think we could close this question due to ticket #1083

If you implement the increment / decrement method for memcache built in into your one extend, it wont need 2 connexions and it's still "true" programming.

Why I dont need it ? I need native increment method of memcache. I can get it from memcache object created from a class from pecl. This increment object need its own connection to work and I don't see any way to give Phalcon memcache connection to pecl object.

so first I'm making Class MyMemcache extends \Phalcon\Cache\Memcache, and implements MyMemcache->increment where making a new connection to memcache from pecl class and calling native libmemcached method