ZendCache::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Onoi\Cache;
4
5
use Zend\Cache\Storage\StorageInterface;
6
7
/**
8
 * ZF2 Cache adapter
9
 *
10
 * @note http://framework.zend.com/manual/current/en/modules/zend.cache.storage.adapter.html
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.1
14
 *
15
 * @author mwjames
16
 */
17
class ZendCache implements Cache {
18
19
	/**
20
	 * @var StorageInterface
21
	 */
22
	private $cache = null;
23
24
	/**
25
	 * @var integer
26
	 */
27
	private $cacheInserts = 0;
28
29
	/**
30
	 * @var integer
31
	 */
32
	private $cacheDeletes = 0;
33
34
	/**
35
	 * @var integer
36
	 */
37
	private $cacheHits = 0;
38
39
	/**
40
	 * @var integer
41
	 */
42
	private $cacheMisses = 0;
43
44
	/**
45
	 * @since 1.1
46
	 *
47
	 * @param StorageInterface $cache
48
	 */
49 10
	public function __construct( StorageInterface $cache ) {
50 10
		$this->cache = $cache;
51 10
	}
52
53
	/**
54
	 * @since 1.1
55
	 *
56
	 * {@inheritDoc}
57
	 */
58 3
	public function fetch( $id ) {
59
60 3
		if ( $this->contains( $id ) ) {
61 2
			$this->cacheHits++;
62 2
			return $this->cache->getItem( $id );
63
		}
64
65 1
		$this->cacheMisses++;
66 1
		return false;
67
	}
68
69
	/**
70
	 * @since 1.1
71
	 *
72
	 * {@inheritDoc}
73
	 */
74 4
	public function contains( $id ) {
75 4
		return $this->cache->hasItem( $id );
76
	}
77
78
	/**
79
	 * @note ZF2 doesn't support per-item ttl storage therefore we use
80
	 * https://github.com/zendframework/zf2/pull/5386#issuecomment-43191005
81
	 *
82
	 * - ttl with 0 means an item never expires
83
	 *
84
	 * @since 1.1
85
	 *
86
	 * {@inheritDoc}
87
	 */
88 3
	public function save( $id, $data, $ttl = 0 ) {
89
90 3
		$options = $this->cache->getOptions();
91 3
		$oldTtl  = $options->getTtl( $ttl );
92 3
		$options->setTtl( $ttl );
93
94
		try {
95 3
			$this->cacheInserts++;
96 3
			$this->cache->setItem( $id, $data );
97 3
		} catch ( \Exception $e ) {
98
			// Don't re-throw any exception, the consumer
99
			// should not care about this
100 1
			$this->cacheInserts--;
101
		}
102
103 3
		$options->setTtl( $oldTtl );
104 3
	}
105
106
	/**
107
	 * @since 1.1
108
	 *
109
	 * {@inheritDoc}
110
	 */
111 2
	public function delete( $id ) {
112 2
		$this->cacheDeletes++;
113 2
		return $this->cache->removeItem( $id );
114
	}
115
116
	/**
117
	 * @since 1.1
118
	 *
119
	 * {@inheritDoc}
120
	 */
121 4
	public function getStats() {
122
		return array(
123 4
			'inserts' => $this->cacheInserts,
124 4
			'deletes' => $this->cacheDeletes,
125 4
			'hits'    => $this->cacheHits,
126 4
			'misses'  => $this->cacheMisses
127 4
		);
128
	}
129
130
	/**
131
	 * @since  1.2
132
	 *
133
	 * {@inheritDoc}
134
	 */
135 1
	public function getName() {
136 1
		return __CLASS__ . '::' . get_class( $this->cache );
137
	}
138
139
}
140