Completed
Push — develop ( 26829c...31abed )
by Da Phuture
05:23
created

AbstractStorage::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Kemist\Cache\Storage;
4
5
/**
6
 * AbstractStorage class
7
 * 
8
 * @package Kemist\Cache
9
 * 
10
 * @version 1.0.6
11
 */
12
abstract class AbstractStorage {
13
14
  /**
15
   * Cached field names
16
   * 	 	
17
   * @var array
18
   */
19
  protected $fields = array();
20
21
  /**
22
   * Number of hits
23
   * @var int
24
   */
25
  protected $hits = 0;
26
27
  /**
28
   * Number of misses
29
   * @var int
30
   */
31
  protected $misses = 0;
32
33
  /**
34
   * Cache provider Object
35
   * @var object
36
   */
37
  protected $provider;
38
39
  /**
40
   * Info method
41
   * @var string 
42
   */
43
  protected $infoMethod = 'info';
44
45
  /**
46
   * Key prefix to avoid collisions
47
   * @var string 
48
   */
49
  protected $prefix = '';
50
51
  /**
52
   * Retrieves the content of $name cache
53
   * 	 
54
   * @param string $name cache name
55
   *
56
   * @return mixed
57
   */
58
  public function get($name) {
59
    $ret = $this->provider->get($this->prefix . $name);
60
    if ($ret !== false) {
61
      $this->hit();
62
      $this->storeName($name);
63
    } else {
64
      $this->miss();
65
    }
66
67
    return $ret;
68
  }
69
70
  /**
71
   * Cache miss occured
72
   */
73
  public function miss() {
74
    $this->misses++;
75
  }
76
77
  /**
78
   * Cache hit occured
79
   */
80
  public function hit() {
81
    $this->hits++;
82
  }
83
84
  /**
85
   * Deletes the specified cache or each one if '' given
86
   * 	 
87
   * @param string $name cache name
88
   *
89
   * @return bool
90
   */
91
  public function delete($name = '') {
92
    if ($name == '') {
93
      return $this->provider->flush();
94
    } else {
95
      return $this->provider->delete($this->prefix . $name);
96
    }
97
  }
98
99
  /**
100
   * Retrieves information of Cache state
101
   * 
102
   * @param bool $getFields
103
   *  
104
   * @return array
105
   */
106
  public function info($getFields = false) {
107
    $info = array();
108
    $className = explode('\\', get_class($this));
109
    $info['CACHE_TYPE'] = end($className);
110
    $info['CACHE_HITS'] = $this->hits;
111
    $info['CACHE_MISSES'] = $this->misses;
112
113
    $info = array_merge($info, call_user_func(array($this->provider, $this->infoMethod)));
114
115
    if ($getFields) {
116
      foreach ($this->fields as $field) {
117
        $info['field_content'][$field] = $this->get($field);
118
      }
119
    }
120
121
    return $info;
122
  }
123
124
  /**
125
   * Retrieves cache hits
126
   * 
127
   * @return int
128
   */
129
  public function getHits() {
130
    return $this->hits;
131
  }
132
133
  /**
134
   * Retrieves cache misses
135
   * 
136
   * @return int
137
   */
138
  public function getMisses() {
139
    return $this->misses;
140
  }
141
142
  /**
143
   * Stores cache name
144
   * 
145
   * @param string $name
146
   */
147
  protected function storeName($name) {
148
    if (!in_array($name, $this->fields)) {
149
      $this->fields[] = $name;
150
    }
151
  }
152
153
}
154