broker.php 1.16 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

9
10
11
namespace OC\Cache;

class Broker {
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	protected $fast_cache;
	protected $slow_cache;

	public function __construct($fast_cache, $slow_cache) {
		$this->fast_cache = $fast_cache;
		$this->slow_cache = $slow_cache;
	}

	public function get($key) {
		if ($r = $this->fast_cache->get($key)) {
			return $r;
		}
		return $this->slow_cache->get($key);
	}

	public function set($key, $value, $ttl=0) {
28
		if (!$this->fast_cache->set($key, $value, $ttl)) {
29
30
31
			if ($this->fast_cache->hasKey($key)) {
				$this->fast_cache->remove($key);
			}
32
			return $this->slow_cache->set($key, $value, $ttl);
33
		}
34
		return true;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	}

	public function hasKey($key) {
		if ($this->fast_cache->hasKey($key)) {
			return true;
		}
		return $this->slow_cache->hasKey($key);
	}

	public function remove($key) {
		if ($this->fast_cache->remove($key)) {
			return true;
		}
		return $this->slow_cache->remove($key);
	}

Thomas Mueller's avatar
Thomas Mueller committed
51
	public function clear($prefix='') {
52
53
		$this->fast_cache->clear($prefix);
		$this->slow_cache->clear($prefix);
54
55
	}
}