Memory   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 3 1
A get() 0 9 4
A set() 0 3 2
A delete() 0 3 1
A exists() 0 3 3
A flush() 0 3 1
A inc() 0 3 2
A dec() 0 3 1
1
<?php
2
3
/**
4
 * Cache\Memory
5
 *
6
 * Core\Cache Memory Driver.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
namespace Cache;
14
15
class Memory implements Adapter {
16
17
  protected $memory = [];
18
19
    public static function valid(){
20
        return true;
21
    }
22
23
    public function get($key){
24
      if(isset($this->memory[$key])){
25
        if($this->memory[$key][1] && (time() > $this->memory[$key][1])) {
26
          unset($this->memory[$key]);
27
          return null;
28
        }
29
        return $this->memory[$key][0];
30
      }
31
    }
32
33
    public function set($key,$value,$expire=0){
34
      $this->memory[$key] = [$value,$expire?time()+$expire:0];
35
    }
36
37
    public function delete($key){
38
      unset($this->memory[$key]);
39
    }
40
41
    public function exists($key){
42
      return isset($this->memory[$key]) && (!$this->memory[$key][1] || (time() <= $this->memory[$key][1]));
43
    }
44
45
    public function flush(){
46
      $this->memory = [];
47
    }
48
49
    public function inc($key,$value=1){
50
      return isset($this->memory[$key]) ? $this->memory[$key][0] += $value : $this->memory[$key][0] = $value;
51
    }
52
53
    public function dec($key,$value=1){
54
        $this->inc($key,-abs($value));
55
    }
56
}
57