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