Completed
Push — master ( 28ceb5...e8f403 )
by Mathieu
02:46 queued 01:17
created

File::getDefaultExpiry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Suricate\Cache;
3
4
use Suricate;
5
6
/**
7
 * Memcache extension for Suricate
8
 *
9
 * @package Suricate
10
 * @author  Mathieu LESNIAK <[email protected]>
11
 *
12
 * @property string $path           Storage path (default: app/storage/app)
13
 * @property int    $defaultExpiry  Key default expiry in sec
14
 */
15
16
class File extends Suricate\Cache
17
{
18
    protected $parametersList = array(
19
                                    'path',
20
                                    'defaultExpiry',
21
                                );
22
    private $handler;
23
24
    public function __construct()
25
    {
26
        $this->handler          = false;
27
        $this->path             = app_path() . '/storage/app/';
28
        $this->defaultExpiry    = 3600;
29
    }
30
31
    public function getDefaultExpiry()
32
    {
33
        return $this->defaultExpiry;
34
    }
35
36
    public function setDefaultExpiry($expiry)
37
    {
38
        $this->defaultExpiry = $expiry;
39
40
        return $this;
41
    }
42
    
43
    /**
44
     * Put a value into memcache
45
     * @param string $variable Variable name
46
     * @param mixed $value    Value
47
     * @param int $expiry   Cache expiry
48
     */
49
    public function set($variable, $value, $expiry = null)
50
    {
51
        if ($expiry === null) {
52
            $expiry = $this->defaultExpiry;
53
        }
54
        $fp = fopen($this->path . $variable, 'w');
55
        fputs($fp, $value);
56
        fclose($fp);
57
        if ($expiry !== null) {
58
            $fp = fopen($this->path . $variable .'.expiry', 'w');
59
            fputs($fp, time() + $expiry);
60
            fclose($fp);
61
        }
62
    }
63
64
    public function get($variable)
65
    {
66
        if (is_readable($this->path . $variable)) {
67
            if (is_readable($this->path . $variable . '.expiry')) {
68
                $expiry = file_get_contents($this->path . $variable . '.expiry');
69
                $hasExpired = (time() - $expiry) > 0 ? 1 : -1;
70
            } else {
71
                $hasExpired = 0;
72
            }
73
            
74
            if ($hasExpired < 0) {
75
                return file_get_contents($this->path . $variable);
76
            } elseif ($hasExpired > 0) {
77
                unlink($this->path . $variable . '.expiry');
78
            }
79
        }
80
        return null;
81
    }
82
83
    public function delete($variable)
84
    {
85
        if (is_file($this->path . $variable)) {
86
            unlink($this->path . $variable);
87
        }
88
        if (is_file($this->path . $variable . '.expiry')) {
89
            unlink($this->path . $variable . '.expiry');
90
        }
91
    }
92
}
93