Passed
Push — develop ( 8c3394...9d4761 )
by Mykola
08:36
created

RedisCache::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/* 	Sunrise CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Sunrise CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
namespace Sunrise\Engine\Library;
24
25
class RedisCache
26
{
27
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
28
    {
29
        $this->cache = new \Redis();
0 ignored issues
show
Bug Best Practice introduced by
The property cache does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
        $this->cache->pconnect('localhost', '6379');
31
    }
32
33
    public function get($key)
34
    {
35
        $data = $this->cache->get('snrs_' . $key);
36
        return json_decode($data, true);
37
    }
38
39
    public function set($key, $value)
40
    {
41
        $status = $this->cache->set(
42
            'snrs_' . $key,
43
            json_encode($value)
44
        );
45
        if ($status) {
46
            $this->cache->setTimeout(
47
                'snrs_' . $key,
48
                '3600'
49
            );
50
        }
51
        return $status;
52
    }
53
54
    public function delete($key)
55
    {
56
        $this->cache->delete('snrs_' . $key);
57
    }
58
}
59