]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/TraitCompareSet.php
Adding force to update routine
[friendica.git] / src / Core / Cache / TraitCompareSet.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6
7 /**
8  * Trait TraitCompareSetDelete
9  *
10  * This Trait is to compensate non native "exclusive" sets/deletes in caches
11  *
12  * @package Friendica\Core\Cache
13  */
14 trait TraitCompareSet
15 {
16         abstract public function get($key);
17
18         abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
19
20         abstract public function delete($key);
21
22         abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
23
24         /**
25          * NonNative - Compares if the old value is set and sets the new value
26          *
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
31          *
32          * @return bool
33          */
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");
39                                 return true;
40                         } else {
41                                 $this->delete($key . "_lock");
42                                 return false;
43                         }
44                 } else {
45                         return false;
46                 }
47         }
48 }