ApcCache   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 3 1
A exists() 0 3 1
A write() 0 3 1
A read() 0 6 2
A __construct() 0 7 3
1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file ApcCache.php
36
 *
37
 *  The APC cache class
38
 *
39
 *  @package    Platine\Template\Cache
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   https://www.platine-php.com
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Cache;
51
52
use Platine\Template\Configuration;
53
use Platine\Template\Exception\TemplateException;
54
55
/**
56
 * @class ApcCache
57
 * @package Platine\Template\Cache
58
 */
59
class ApcCache extends AbstractCache
60
{
61
    /**
62
     * Create new instance
63
     * @param Configuration|null $config
64
     */
65
    public function __construct(?Configuration $config = null)
66
    {
67
        parent::__construct($config);
68
69
        if ((!extension_loaded('apcu')) || !((bool) ini_get('apc.enabled'))) {
70
            throw new TemplateException('The cache for APCu driver is not available.'
71
                            . ' Check if APCu extension is loaded and enabled.');
72
        }
73
    }
74
75
    /**
76
    * {@inheritdoc}
77
    */
78
    public function read(string $key, bool $unserialize = true): mixed
79
    {
80
        $success = false;
81
        $data = apcu_fetch($this->prefix . $key, $success);
82
83
        return $success ? $data : false;
84
    }
85
86
    /**
87
    * {@inheritdoc}
88
    */
89
    public function exists(string $key): bool
90
    {
91
        return apcu_exists($this->prefix . $key) === true;
92
    }
93
94
    /**
95
    * {@inheritdoc}
96
    */
97
    public function write(string $key, mixed $value, bool $serialize = true): bool
98
    {
99
        return apcu_store($this->prefix . $key, $value, $this->expire);
100
    }
101
102
103
    /**
104
    * {@inheritdoc}
105
    */
106
    public function flush(bool $expired = false): bool
107
    {
108
        return apcu_clear_cache();
109
    }
110
}
111