1 | <?php |
||
19 | class EntityCache extends Component |
||
20 | { |
||
21 | /** |
||
22 | * Indication that entity cache is enabled. |
||
23 | * |
||
24 | * @var bool |
||
25 | */ |
||
26 | private $enabled = true; |
||
27 | |||
28 | /** |
||
29 | * Maximum entity cache size. Null is unlimited. |
||
30 | * |
||
31 | * @var int|null |
||
32 | */ |
||
33 | private $cacheSize = null; |
||
34 | |||
35 | /** |
||
36 | * In cases when ORM cache is enabled every constructed instance will be stored here, cache used |
||
37 | * mainly to ensure the same instance of object, even if was accessed from different spots. |
||
38 | * Cache usage increases memory consumption and does not decreases amount of queries being made. |
||
39 | * |
||
40 | * @var RecordEntity[] |
||
41 | */ |
||
42 | private $cache = []; |
||
43 | |||
44 | /** |
||
45 | * Check if entity cache enabled. |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function isEnabled() |
||
53 | |||
54 | /** |
||
55 | * Enable or disable entity cache. Disabling cache will not flush it's values. |
||
56 | * |
||
57 | * @deprecated see configure |
||
58 | * @param bool $enabled |
||
59 | * @param int|null $maxSize Null = unlimited. |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function configureCache($enabled, $maxSize = null) |
||
66 | |||
67 | /** |
||
68 | * Enable or disable entity cache. Disabling cache will not flush it's values. |
||
69 | * |
||
70 | * @param bool $enabled |
||
71 | * @param int|null $maxSize Null = unlimited. |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function configure($enabled, $maxSize = null) |
||
85 | |||
86 | /** |
||
87 | * Add Record to entity cache (only if cache enabled). Primary key is required for caching. |
||
88 | * |
||
89 | * @param IdentifiedInterface $entity |
||
90 | * @param bool $ignoreLimit Cache overflow will be ignored. |
||
91 | * @return IdentifiedInterface |
||
92 | * @throws CacheException |
||
93 | */ |
||
94 | public function remember(IdentifiedInterface $entity, $ignoreLimit = true) |
||
106 | |||
107 | /** |
||
108 | * Remove Record record from entity cache. Primary key is required for caching. |
||
109 | * |
||
110 | * @param IdentifiedInterface $entity |
||
111 | */ |
||
112 | public function forget(IdentifiedInterface $entity) |
||
120 | |||
121 | /** |
||
122 | * Check if desired entity was already cached. |
||
123 | * |
||
124 | * @param string $class |
||
125 | * @param mixed $primaryKey |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function has($class, $primaryKey) |
||
132 | |||
133 | /** |
||
134 | * Fetch entity from cache. |
||
135 | * |
||
136 | * @param string $class |
||
137 | * @param mixed $primaryKey |
||
138 | * @return null|IdentifiedInterface |
||
139 | */ |
||
140 | public function get($class, $primaryKey) |
||
148 | |||
149 | /** |
||
150 | * Flush content of entity cache. |
||
151 | */ |
||
152 | public function flushCache() |
||
156 | |||
157 | /** |
||
158 | * Destructing. |
||
159 | */ |
||
160 | public function __destruct() |
||
164 | } |