3 namespace Friendica\Core\Cache;
6 * Trait TraitCompareSetDelete
8 * This Trait is to compensate non native "exclusive" sets/deletes in caches
10 * @package Friendica\Core\Cache
14 abstract public function get($key);
16 abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
18 abstract public function delete($key);
20 abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
23 * NonNative - Compares if the old value is set and sets the new value
25 * @param string $key The cache key
26 * @param mixed $oldValue The old value we know from the cache
27 * @param mixed $newValue The new value we want to set
28 * @param int $ttl The cache lifespan, must be one of the Cache constants
32 public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES) {
33 if ($this->add($key . "_lock", true)) {
34 if ($this->get($key) === $oldValue) {
35 $this->set($key, $newValue, $ttl);
36 $this->delete($key . "_lock");
39 $this->delete($key . "_lock");