Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
namespace WebStream\Log;
4
5
use WebStream\Cache\Driver\ICache;
6
7
/**
8
 * LoggerCache
9
 * @author Ryuichi Tanaka
10
 * @since 2016/05/23
11
 * @version 0.7
12
 */
13
class LoggerCache
14
{
15
    /**
16
     * @var ICache キャッシュドライバ
17
     */
18
    private ICache $driver;
19
20
    /**
21
     * @var string キャッシュキー
22
     */
23
    private $key;
24
25
    /**
26
     * @var int キャッシュインデックス
27
     */
28
    private int $index;
29
30
    /**
31
     * constructor
32 142
     * @param ICache $driver キャッシュドライバ
33
     */
34 142
    public function __construct(ICache $driver)
35 142
    {
36 142
        $this->driver = $driver;
37 142
        $this->key = md5(get_class($this));
38
        $this->index = 0;
39
    }
40
41
    /**
42 1
     * destructor
43
     */
44 1
    public function __destruct()
45 1
    {
46
        $this->clear();
47
    }
48
49
    /**
50
     * キャッシュに追加
51 96
     * @param string $content キャッシュデータ
52
     */
53 96
    public function add(string $content)
54 96
    {
55
        $this->driver->add($this->key . $this->index++, $content);
56
    }
57
58
    /**
59
     * キャッシュを返却する
60 1
     * @return array<string> キャッシュデータ
61
     */
62 1
    public function get()
63 1
    {
64 1
        $list = [];
65
        for ($i = 0; $i < $this->index; $i++) {
66
            $list[] = $this->driver->get($this->key . $i);
67 1
        }
68
69
        return $list;
70
    }
71
72
    /**
73
     * キャッシュデータ数を返却する
74 96
     * @return int キャッシュデータ数
75
     */
76 96
    public function length()
77
    {
78
        return $this->index;
79
    }
80
81
    /**
82
     * キャッシュクリア
83
     */
84
    public function clear()
85
    {
86
        $this->driver->clear();
87
    }
88
}
89