Completed
Push — feature/0.7.0 ( 2d465b...a2171b )
by Ryuichi
03:17
created

LoggerCache   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 6
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 4 1
A add() 0 4 1
A get() 0 9 2
A length() 0 4 1
1
<?php
2
namespace WebStream\Log;
3
4
/**
5
 * LoggerCache
6
 * @author Ryuichi Tanaka
7
 * @since 2016/05/23
8
 * @version 0.7
9
 */
10
class LoggerCache
11
{
12
    /**
13
     * @var string キャッシュ接頭辞
14
     */
15
    private $prefix;
16
17
    /**
18
     * @var int キャッシュインデックス
19
     */
20
    private $index;
21
22
    /**
23
     * constructor
24
     */
25
    public function __construct()
26
    {
27
        $this->prefix = md5(get_class($this));
28
        $this->index = 0;
29
    }
30
31
    /**
32
     * destructor
33
     */
34
    public function __destruct()
35
    {
36
        apcu_clear_cache();
37
    }
38
39
    /**
40
     * キャッシュに追加
41
     * @param string $content キャッシュデータ
42
     */
43
    public function add(string $content)
44
    {
45
        apcu_add($this->prefix . $this->index++, $content);
46
    }
47
48
    /**
49
     * キャッシュを返却する
50
     * @return array<string> キャッシュデータ
51
     */
52
    public function get()
53
    {
54
        $list = [];
55
        for ($i = 0; $i < $this->index; $i++) {
56
            $list[] = apcu_fetch($this->prefix . $i);
57
        }
58
59
        return $list;
60
    }
61
62
    /**
63
     * キャッシュデータ数を返却する
64
     * @return int キャッシュデータ数
65
     */
66
    public function length()
67
    {
68
        return $this->index;
69
    }
70
}
71