1: <?php
2:
3:
4: /**
5: * A multi-level Cache provider that's made of multiple cache providers.
6: * Cache hit is checked in the order the cache providers are set
7: * Setting or deleting into the cache sets or deletes it from all the providers
8: */
9: class QMultiLevelCacheProvider extends QAbstractCacheProvider {
10: /** @var QAbstractCacheProvider[] */
11: protected $arrCacheProviders = array();
12:
13: /**
14: * Constructs a Multi-level Cache provider based on the configuration used for each provider
15: * @param array $objOptionsArray an associative array where each item is an array with two elements specifying
16: * an inner cache provider. The first element of that array is the class name of the cache provider, and the second
17: * one is the options for constructing the provider
18: */
19: public function __construct($objOptionsArray) {
20: foreach ($objOptionsArray as $arrProviderNameAndOptions) {
21: $strCacheProviderClassname = $arrProviderNameAndOptions[0];
22: $this->arrCacheProviders[] = new $strCacheProviderClassname($arrProviderNameAndOptions[1]);
23: }
24: }
25:
26: /**
27: * Get the object that has the given key from the cache
28: * @param string $strKey the key of the object in the cache
29: * @return object
30: */
31: public function Get($strKey) {
32: $objValue = false;
33: /** @var QAbstractCacheProvider[] */
34: $arrCacheProviders = array();
35: foreach ($this->arrCacheProviders as $objCacheProvider) {
36: $objValue = $objCacheProvider->Get($strKey);
37: if (false !== $objValue) {
38: break;
39: }
40: $arrCacheProviders[] = $objCacheProvider;
41: }
42: // Set or clear value in all lower caches
43: if (false !== $objValue) {
44: $arrCacheProviders = array_reverse($arrCacheProviders);
45: foreach ($arrCacheProviders as /** @var QAbstractCacheProvider */ $objCacheProvider) {
46: $objCacheProvider->Set($strKey, $objValue);
47: }
48: }
49: return $objValue;
50: }
51:
52: /**
53: * Set the object into the cache with the given key
54: * @param string $strKey the key to use for the object
55: * @param object $objValue the object to put in the cache
56: * @return void
57: */
58: public function Set($strKey, $objValue) {
59: foreach ($this->arrCacheProviders as $objCacheProvider) {
60: $objCacheProvider->Set($strKey, $objValue);
61: }
62: }
63:
64: /**
65: * Delete the object that has the given key from the cache
66: * @param string $strKey the key of the object in the cache
67: * @return void
68: */
69: public function Delete($strKey) {
70: foreach ($this->arrCacheProviders as $objCacheProvider) {
71: $objCacheProvider->Delete($strKey);
72: }
73: }
74:
75: /**
76: * Invalidate all the objects in the cache
77: * @return void
78: */
79: public function DeleteAll() {
80: foreach ($this->arrCacheProviders as $objCacheProvider) {
81: $objCacheProvider->DeleteAll();
82: }
83: }
84: }