Sweep   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 4 1
A old() 0 4 1
A process() 0 16 4
A flush() 0 16 3
1
<?php
2
3
namespace PhpDbaCache;
4
5
/**
6
 * Cache Garbage Collector/Cleaner
7
 *
8
 * @category PhpDbaCache
9
 */
10
class Sweep
11
{
12
    /**
13
     * @var Cache
14
     */
15
    protected $cache;
16
17
    /**
18
     * @param Cache $cache
19
     */
20
    public function __construct(Cache $cache)
21
    {
22
        $this->cache = $cache;
23
    }
24
25
    /**
26
     * Remove all cache entries.
27
     *
28
     * @return void
29
     */
30
    public function all()
31
    {
32
        $this->process();
33
    }
34
35
    /**
36
     * Remove too old cache entries.
37
     *
38
     * @return void
39
     */
40
    public function old()
41
    {
42
        $this->process(false);
43
    }
44
45
    /**
46
     * Internal cleaning process.
47
     *
48
     * @param boolean $cleanAll
49
     *
50
     * @return void
51
     */
52
    protected function process($cleanAll = true)
53
    {
54
        $key = dba_firstkey($this->cache->getDba());
55
56
        while ($key !== false && $key !== null) {
57
            if (true === $cleanAll) {
58
                $this->cache->delete($key);
59
            } else {
60
                $this->cache->get($key);
61
            }
62
63
            $key = dba_nextkey($this->cache->getDba());
64
        }
65
66
        dba_optimize($this->cache->getDba());
67
    }
68
69
    /**
70
     * Flush the whole storage.
71
     *
72
     * @throws \RuntimeException If can not flush file.
73
     * @return bool
74
     */
75
    public function flush()
76
    {
77
        $file = $this->cache->getStorage();
78
79
        if (file_exists($file)) {
80
            // close the dba file before delete
81
            // and reopen on next use
82
            $this->cache->closeDba();
83
84
            if (!@unlink($file)) {
85
                throw new \RuntimeException("can not flush file '$file'");
86
            }
87
        }
88
89
        return true;
90
    }
91
}
92