Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
LggrCacheFile
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 6
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 retrieve
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 purge
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 filterKey
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getFilename
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Lggr;
3
4/**
5 * @brief Caching class for file based cache.
6 */
7class LggrCacheFile extends AbstractLggrCache {
8
9    const MAXAGE = 300;
10
11    // 5 minutes
12    private $cachepath = null;
13
14    function __construct() {
15        $this->cachepath = __DIR__ . '/../cache/';
16    }
17
18    // constructor
19    public function store($key, $value) {
20        $fname = $this->getFilename($key);
21        $s = serialize($value);
22        file_put_contents($fname, $s);
23    }
24
25    // function
26    public function retrieve($key) {
27        $fname = $this->getFilename($key);
28        if (file_exists($fname) && is_readable($fname)) {
29            $ts = filemtime($fname);
30            if (time() - $ts > self::MAXAGE) {
31                unlink($fname);
32                return null;
33            } else {
34                $s = file_get_contents($fname);
35                return unserialize($s);
36            } // if
37        } else {
38            return null;
39        } // if
40    }
41
42    // function
43    public function purge($key) {
44        $fname = $this->getFilename($key);
45        unlink($fname);
46    }
47
48    // function
49    private function filterKey($key) {
50        $sTmp = str_replace(' ', '-', $key);
51        return preg_replace('/[^A-Za-z0-9\-]/', '', $sTmp);
52    }
53
54    // function
55    private function getFilename($key) {
56        return $this->cachepath . 'key_' . $this->filterKey($key) . '.data';
57    } // function
58}
59