CombinatoryCache   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.5%

Importance

Changes 0
Metric Value
dl 0
loc 75
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1
ccs 39
cts 40
cp 0.975
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A assertCachesListIsValid() 0 4 1
A assertNotEmpty() 0 5 2
A assertAreCaches() 0 7 4
A get() 0 18 4
A setInFirstCache() 0 3 1
A has() 0 9 3
A set() 0 5 2
1
<?php
2
3
namespace SimpleCache\Cache;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Allows combining multiple caches.
9
 *
10
 * The caches first in the list are hit first. Therefore cheap caches
11
 * (ie in memory caches) should be placed before more expensive ones
12
 * (ie memcached).
13
 *
14
 * When a cache entry is requested and is not found in the first cache,
15
 * a new entry will be written to this cache. In case the requested entry
16
 * was found in a later cache, its value will be written to the first one.
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class CombinatoryCache implements Cache {
22
23
	/**
24
	 * @var Cache[]
25
	 */
26
	protected $caches;
27
28
	protected $hasMoreThenOneCache;
29
30 12
	public function __construct( array $caches ) {
31 12
		$this->assertCachesListIsValid( $caches );
32
33 8
		$this->caches = $caches;
34 8
		$this->hasMoreThenOneCache = count( $this->caches ) > 1;
35 8
	}
36
37 12
	protected function assertCachesListIsValid( array $caches ) {
38 12
		$this->assertNotEmpty( $caches );
39 11
		$this->assertAreCaches( $caches );
40 8
	}
41
42 12
	protected function assertNotEmpty( $caches ) {
43 12
		if ( empty( $caches ) ) {
44 1
			throw new InvalidArgumentException( 'The caches list needs to contain at least one cache' );
45
		}
46 11
	}
47
48 11
	protected function assertAreCaches( $caches ) {
49 11
		foreach ( $caches as $cache ) {
50 11
			if ( !is_object( $cache ) || !( $cache instanceof Cache ) ) {
51 11
				throw new InvalidArgumentException( 'The cache list can only contain instances of Cache' );
52
			}
53
		}
54 8
	}
55
56 4
	public function get( $key ) {
57 4
		reset( $this->caches );
58 4
		$firstCacheIndex = key( $this->caches );
59
60 4
		foreach ( $this->caches as $currentIndex => $cache ) {
61 4
			$value = $cache->get( $key );
62
63 4
			if ( $value !== null ) {
64 3
				if ( $currentIndex !== $firstCacheIndex ) {
65 2
					$this->setInFirstCache( $firstCacheIndex, $key, $value );
66
				}
67
68 4
				return $value;
69
			}
70
		}
71
72 1
		return null;
73
	}
74
75 2
	protected function setInFirstCache( $firstCacheIndex, $key, $value ) {
76 2
		$this->caches[$firstCacheIndex]->set( $key, $value );
77 2
	}
78
79 2
	public function has( $key ) {
80 2
		foreach ( $this->caches as $cache ) {
81 2
			if ( $cache->has( $key ) ) {
82 2
				return true;
83
			}
84
		}
85
86
		return false;
87
	}
88
89 2
	public function set( $key, $value ) {
90 2
		foreach ( $this->caches as $cache ) {
91 2
			$cache->set( $key, $value );
92
		}
93 2
	}
94
95
}
96