Completed
Push — master ( ea3a48...b57255 )
by Lars
02:48
created

AdapterArray::removeAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * AdapterArray: simple array-cache adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterArray implements iAdapter
11
{
12
13
  /**
14
   * @var array
15
   */
16
  private static $values = array();
17
18
  /**
19
   * @var array
20
   */
21
  private static $expired = array();
22
23
  /**
24
   * get cached-item by key
25
   *
26
   * @param String $key
27
   *
28
   * @return mixed
29
   */
30 3
  public function get($key)
31
  {
32 3
    return $this->exists($key) ? self::$values[$key] : null;
33
  }
34
35
  /**
36
   * remove expired
37
   *
38
   * @param string $key
39
   *
40
   * @return boolean
41
   */
42 4
  private function removeExpired($key)
43
  {
44
    if (
45 4
        !isset(self::$expired[$key])
46 4
        ||
47 1
        !isset(self::$values[$key])
48 4
    ) {
49 3
      return false;
50
    }
51
52 1
    list($time, $ttl) = self::$expired[$key];
53
54 1
    if (time() > ($time + $ttl)) {
55
      unset(self::$values[$key]);
56
    }
57
58 1
    if (!isset(self::$values[$key])) {
59
      unset(self::$expired[$key]);
60
    }
61
62 1
    return true;
63
  }
64
65
  /**
66
   * check if cached-item exists
67
   *
68
   * @param string $key
69
   *
70
   * @return bool
71
   */
72 4
  public function exists($key)
73
  {
74 4
    $this->removeExpired($key);
75
76 4
    return isset(self::$values[$key]);
77
  }
78
79
  /**
80
   * set cache-item by key => value
81
   *
82
   * @param string $key
83
   * @param mixed  $value
84
   *
85
   * @return mixed|void
86
   */
87 2
  public function set($key, $value)
88
  {
89 2
    self::$values[$key] = $value;
90
91 2
    return true;
92
  }
93
94
  /**
95
   * set cache-item by key => value + ttl
96
   *
97
   * @param string $key
98
   * @param mixed  $value
99
   * @param int    $ttl
100
   *
101
   * @return mixed|void
102
   */
103 1
  public function setExpired($key, $value, $ttl)
104
  {
105 1
    self::$values[$key] = $value;
106 1
    self::$expired[$key] = array(time(), $ttl);
107
108 1
    return true;
109
  }
110
111
  /**
112
   * remove cache-item by key
113
   *
114
   * @param string $key
115
   *
116
   * @return bool
117
   */
118
  public function remove($key)
119
  {
120
    $this->removeExpired($key);
121
122
    if (isset(self::$values[$key])) {
123
      unset(self::$values[$key]);
124
125
      return true;
126
    }
127
128
    return false;
129
  }
130
131
  /**
132
   * remove cache
133
   *
134
   * @return true
135
   */
136
  public function removeAll()
137
  {
138
    self::$values = array();
139
    self::$expired = array();
140
141
    return true;
142
  }
143
144
  /**
145
   * check if cache is installed
146
   *
147
   * @return true
148
   */
149
  public function installed()
150
  {
151
    return true;
152
  }
153
154
}
155