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

aplus-framework / cache / 15646503070

14 Jun 2025 12:37AM UTC coverage: 95.167% (+0.07%) from 95.094%
15646503070

push

github

natanfelles
Add Cache::getSerializer method

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

1 existing line in 1 file now uncovered.

512 of 538 relevant lines covered (95.17%)

57.81 hits per line

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

98.15
/src/ApcuCache.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Cache Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Cache;
11

12
use RuntimeException;
13

14
/**
15
 * Class ApcuCache.
16
 *
17
 * @package cache
18
 */
19
class ApcuCache extends Cache
20
{
21
    protected bool $useCustomSerializer = true;
22

23
    protected function initialize() : void
24
    {
25
        if (!\apcu_enabled()) {
108✔
UNCOV
26
            throw new RuntimeException('APCu extension is not enabled');
×
27
        }
28
        $this->setCustomSerializer();
108✔
29
    }
30

31
    protected function setCustomSerializer() : void
32
    {
33
        $this->useCustomSerializer = $this->configs['use_custom_serializer'] ?? true;
108✔
34
    }
35

36
    public function isUsingCustomSerializer() : bool
37
    {
38
        return $this->useCustomSerializer;
66✔
39
    }
40

41
    public function get(string $key) : mixed
42
    {
43
        if (isset($this->debugCollector)) {
66✔
44
            $start = \microtime(true);
12✔
45
            return $this->addDebugGet(
12✔
46
                $key,
12✔
47
                $start,
12✔
48
                $this->getValue($key)
12✔
49
            );
12✔
50
        }
51
        return $this->getValue($key);
54✔
52
    }
53

54
    protected function getValue(string $key) : mixed
55
    {
56
        $key = \apcu_fetch($this->renderKey($key), $success);
66✔
57
        if ($success && $this->isUsingCustomSerializer()) {
66✔
58
            $key = $this->unserialize($key);
45✔
59
        }
60
        return $success
66✔
61
            ? $key
54✔
62
            : null;
66✔
63
    }
64

65
    public function set(string $key, mixed $value, ?int $ttl = null) : bool
66
    {
67
        if ($this->isUsingCustomSerializer()) {
60✔
68
            $value = $this->serialize($value);
50✔
69
        }
70
        if (isset($this->debugCollector)) {
60✔
71
            $start = \microtime(true);
6✔
72
            return $this->addDebugSet(
6✔
73
                $key,
6✔
74
                $ttl,
6✔
75
                $start,
6✔
76
                $value,
6✔
77
                \apcu_store(
6✔
78
                    $this->renderKey($key),
6✔
79
                    $value,
6✔
80
                    $this->makeTtl($ttl)
6✔
81
                )
6✔
82
            );
6✔
83
        }
84
        return \apcu_store(
54✔
85
            $this->renderKey($key),
54✔
86
            $value,
54✔
87
            $this->makeTtl($ttl)
54✔
88
        );
54✔
89
    }
90

91
    public function delete(string $key) : bool
92
    {
93
        if (isset($this->debugCollector)) {
18✔
94
            $start = \microtime(true);
6✔
95
            return $this->addDebugDelete(
6✔
96
                $key,
6✔
97
                $start,
6✔
98
                \apcu_delete($this->renderKey($key))
6✔
99
            );
6✔
100
        }
101
        return \apcu_delete($this->renderKey($key));
12✔
102
    }
103

104
    public function flush() : bool
105
    {
106
        if (isset($this->debugCollector)) {
108✔
107
            $start = \microtime(true);
24✔
108
            return $this->addDebugFlush(
24✔
109
                $start,
24✔
110
                \apcu_clear_cache()
24✔
111
            );
24✔
112
        }
113
        return \apcu_clear_cache();
84✔
114
    }
115
}
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