Passed
Push — master ( 47e23c...a61d86 )
by Greg
05:13
created

UseCache::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\Middleware;
21
22
use Fisharebest\Webtrees\Cache;
23
use Fisharebest\Webtrees\Webtrees;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\MiddlewareInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
use Symfony\Component\Cache\Adapter\ArrayAdapter;
29
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
30
31
use function app;
32
33
/**
34
 * Middleware to setup an in-memory cache.
35
 */
36
class UseCache implements MiddlewareInterface
37
{
38
    // How frequently to perform garbage collection.
39
    private const GC_PROBABILITY = 100;
40
41
    // Filesystem cache parameters.
42
    private const FILES_LIFETIME = 8640000;
43
    private const FILES_DIR      = Webtrees::DATA_DIR . 'cache/';
44
45
    /** @var ArrayAdapter */
46
    private $array_adapter;
47
48
    /** @var FilesystemAdapter */
49
    private $files_adapter;
50
51
    /**
52
     * UseCache constructor.
53
     */
54
    public function __construct()
55
    {
56
        $this->array_adapter = new ArrayAdapter();
57
        $this->files_adapter = new FilesystemAdapter('', self::FILES_LIFETIME, self::FILES_DIR);
58
    }
59
60
    /**
61
     * @param ServerRequestInterface  $request
62
     * @param RequestHandlerInterface $handler
63
     *
64
     * @return ResponseInterface
65
     */
66
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
67
    {
68
        // Use an array cache for database calls, etc.
69
        app()->instance('cache.array', new Cache($this->array_adapter));
70
71
        // Use a filesystem cache for image thumbnails, etc.
72
        app()->instance('cache.files', new Cache($this->files_adapter));
73
74
        return $handler->handle($request);
75
    }
76
77
    /**
78
     * Perform garbage collection.
79
     */
80
    public function __destruct()
81
    {
82
        if (random_int(1, self::GC_PROBABILITY) === 1) {
83
            $this->files_adapter->prune();
84
        }
85
    }
86
}
87