• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / redis / 7092115097

04 Dec 2023 08:21PM UTC coverage: 59.244% (-0.08%) from 59.322%
7092115097

push

github

f3l1x
Make naming constants public so they can be reused in tests

1 of 12 new or added lines in 2 files covered. (8.33%)

31 existing lines in 1 file now uncovered.

141 of 238 relevant lines covered (59.24%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.5
/src/Caching/RedisJournal.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Redis\Caching;
4

5
use Nette\Caching\Cache;
6
use Nette\Caching\Storages\IJournal as Journal;
7
use Predis\ClientInterface;
8

9
/**
10
 * @see based on original https://github.com/Kdyby/Redis
11
 */
12
final class RedisJournal implements Journal
13
{
14

15
        public const NS_PREFIX = 'Contributte.Journal';
16

17
        public const KEY_PRIORITY = 'priority';
18
        public const SUFFIX_TAGS = 'tags';
19
        public const SUFFIX_KEYS = 'keys';
20

21
        /** @var ClientInterface $client */
22
        private $client;
23

24
        public function __construct(ClientInterface $client)
1✔
25
        {
26
                $this->client = $client;
1✔
27
        }
1✔
28

29
        /**
30
         * Writes entry information into the journal.
31
         *
32
         * @param string $key
33
         * @param mixed[] $dependencies
34
         * @return void
35
         */
36
        public function write($key, array $dependencies): void
37
        {
38
                $this->cleanEntry($key);
×
39

40
                $this->client->multi();
×
41

42
                // add entry to each tag & tag to entry
43
                $tags = empty($dependencies[Cache::TAGS]) ? [] : (array) $dependencies[Cache::TAGS];
×
44
                foreach (array_unique($tags) as $tag) {
×
NEW
45
                        $this->client->sadd($this->formatKey($tag, self::SUFFIX_KEYS), [$key]);
×
NEW
46
                        $this->client->sadd($this->formatKey($key, self::SUFFIX_TAGS), [$tag]);
×
47
                }
48

49
                if (isset($dependencies[Cache::PRIORITY])) {
×
NEW
50
                        $this->client->zadd($this->formatKey(self::KEY_PRIORITY), [$key => $dependencies[Cache::PRIORITY]]);
×
51
                }
52

53
                $this->client->exec();
×
54
        }
55

56
        /**
57
         * Deletes all keys from associated tags and all priorities
58
         *
59
         * @param mixed[]|string $keys
60
         */
61
        public function cleanEntry($keys): void
62
        {
63
                foreach (is_array($keys) ? $keys : [$keys] as $key) {
×
64
                        $entries = $this->entryTags($key);
×
65

66
                        $this->client->multi();
×
67
                        foreach ($entries as $tag) {
×
NEW
68
                                $this->client->srem($this->formatKey($tag, self::SUFFIX_KEYS), $key);
×
69
                        }
70

71
                        // drop tags of entry and priority, in case there are some
NEW
72
                        $this->client->del($this->formatKey($key, self::SUFFIX_TAGS));
×
NEW
73
                        $this->client->zrem($this->formatKey(self::KEY_PRIORITY), $key);
×
74

75
                        $this->client->exec();
×
76
                }
77
        }
78

79
        /**
80
         * Cleans entries from journal.
81
         *
82
         * @param mixed[] $conditions
83
         * @return mixed[] of removed items or NULL when performing a full cleanup
84
         */
85
        public function clean(array $conditions): ?array
86
        {
87
                if (!empty($conditions[Cache::ALL])) {
×
NEW
88
                        $all = $this->client->keys(self::NS_PREFIX . ':*');
×
89

90
                        $this->client->multi();
×
91
                        call_user_func_array([$this->client, 'del'], $all);
×
92
                        $this->client->exec();
×
93
                        return null;
×
94
                }
95

96
                $entries = [];
×
97
                if (!empty($conditions[Cache::TAGS])) {
×
98
                        foreach ((array) $conditions[Cache::TAGS] as $tag) {
×
99
                                $this->cleanEntry($found = $this->tagEntries($tag));
×
100
                                $entries[] = $found;
×
101
                        }
102

103
                        $entries = array_merge(...$entries);
×
104
                }
105

106
                if (isset($conditions[Cache::PRIORITY])) {
×
107
                        $this->cleanEntry($found = $this->priorityEntries($conditions[Cache::PRIORITY]));
×
108
                        $entries = array_merge($entries, $found);
×
109
                }
110

111
                return array_unique($entries);
×
112
        }
113

114
        /**
115
         * @param int $priority
116
         * @return mixed[]
117
         */
118
        private function priorityEntries(int $priority): array
119
        {
NEW
120
                return $this->client->zrangebyscore($this->formatKey(self::KEY_PRIORITY), 0, $priority) ?: [];
×
121
        }
122

123
        /**
124
         * @param string $key
125
         * @return mixed[]
126
         */
127
        private function entryTags(string $key): array
128
        {
NEW
129
                return $this->client->smembers($this->formatKey($key, self::SUFFIX_TAGS)) ?: [];
×
130
        }
131

132
        /**
133
         * @param string $tag
134
         * @return mixed[]
135
         */
136
        private function tagEntries(string $tag): array
137
        {
NEW
138
                return $this->client->smembers($this->formatKey($tag, self::SUFFIX_KEYS)) ?: [];
×
139
        }
140

141
        private function formatKey(string $key, ?string $suffix = null): string
142
        {
NEW
143
                return self::NS_PREFIX . ':' . $key . ($suffix ? ':' . $suffix : null);
×
144
        }
145

146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc