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

contributte / redis / 7002249489

24 Nov 2023 07:42PM UTC coverage: 59.322%. Remained the same
7002249489

push

github

f3l1x
Remove unused key removal

0 of 1 new or added line in 1 file covered. (0.0%)

140 of 236 relevant lines covered (59.32%)

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
        private const NS_NETTE = 'Contributte.Journal';
16

17
        private const PRIORITY = 'priority';
18
        private const TAGS = 'tags';
19
        private const 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) {
×
45
                        $this->client->sadd($this->formatKey($tag, self::KEYS), [$key]);
×
46
                        $this->client->sadd($this->formatKey($key, self::TAGS), [$tag]);
×
47
                }
48

49
                if (isset($dependencies[Cache::PRIORITY])) {
×
50
                        $this->client->zadd($this->formatKey(self::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
        private 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) {
×
68
                                $this->client->srem($this->formatKey($tag, self::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::TAGS));
×
73
                        $this->client->zrem($this->formatKey(self::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])) {
×
88
                        $all = $this->client->keys(self::NS_NETTE . ':*');
×
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
        {
120
                return $this->client->zrangebyscore($this->formatKey(self::PRIORITY), 0, $priority) ?: [];
×
121
        }
122

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

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

141
        private function formatKey(string $key, ?string $suffix = null): string
142
        {
143
                return self::NS_NETTE . ':' . $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

© 2025 Coveralls, Inc