1: <?php
2:
3:
4: /**
5: * Cache provider that uses a local in memory array.
6: * The lifespan of this cache is the request, unless 'KeepInSession' option is used, in which case the lifespan
7: * is the session.
8: */
9: class QCacheProviderLocalMemory extends QAbstractCacheProvider {
10: /** @var array */
11: protected $arrLocalCache;
12:
13: /**
14: * @param array $objOptionsArray configuration options for this cache provider. Currently supported options are
15: * 'KeepInSession': if set to true the cache will be kept in session
16: */
17: public function __construct($objOptionsArray) {
18: if (array_key_exists('KeepInSession', $objOptionsArray) && $objOptionsArray['KeepInSession'] === true) {
19: if (!isset($_SESSION['__LOCAL_MEMORY_CACHE__'])) {
20: $_SESSION['__LOCAL_MEMORY_CACHE__'] = array();
21: }
22: $this->arrLocalCache = &$_SESSION['__LOCAL_MEMORY_CACHE__'];
23: } else {
24: $this->arrLocalCache = array();
25: }
26: }
27:
28: /**
29: * Get the object that has the given key from the cache
30: * @param string $strKey the key of the object in the cache
31: * @return object
32: */
33: public function Get($strKey) {
34: if (array_key_exists($strKey, $this->arrLocalCache)) {
35: // Note the clone statement - it is important to return a copy,
36: // not a pointer to the stored object
37: // to prevent it's modification by user code.
38: $objToReturn = $this->arrLocalCache[$strKey];
39: if ($objToReturn && is_object($objToReturn)) {
40: $objToReturn = clone $objToReturn;
41: }
42: return $objToReturn;
43: }
44: return false;
45: }
46:
47: /**
48: * Set the object into the cache with the given key
49: * @param string $strKey the key to use for the object
50: * @param object $objValue the object to put in the cache
51: * @return void
52: */
53: public function Set($strKey, $objValue) {
54: // Note the clone statement - it is important to store a copy,
55: // not a pointer to the user object
56: // to prevent it's modification by user code.
57: $objToSet = $objValue;
58: if ($objToSet && is_object($objToSet)) {
59: $objToSet = clone $objToSet;
60: }
61: $this->arrLocalCache[$strKey] = $objToSet;
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: if (array_key_exists($strKey, $this->arrLocalCache)) {
71: unset($this->arrLocalCache[$strKey]);
72: }
73: }
74:
75: /**
76: * Invalidate all the objects in the cache
77: * @return void
78: */
79: public function DeleteAll() {
80: $this->arrLocalCache = array();
81: }
82: }