1 | <?php |
||
11 | class QueryCacheService implements QueryCacheServiceContract |
||
12 | { |
||
13 | /** |
||
14 | * The model the cache should run on. |
||
15 | * The model should use the IsCacheable trait for the whole process to work. |
||
16 | * |
||
17 | * @var Model |
||
18 | */ |
||
19 | protected $model; |
||
20 | |||
21 | /** |
||
22 | * Flag whether or not to cache queries forever. |
||
23 | * |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $cacheAllQueries = true; |
||
27 | |||
28 | /** |
||
29 | * Flag whether or not to cache only duplicate queries for the current request. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $cacheDuplicateQueries = true; |
||
34 | |||
35 | /** |
||
36 | * The query cache types available. |
||
37 | * |
||
38 | * @const |
||
39 | */ |
||
40 | const TYPE_CACHE_ALL_QUERIES_FOREVER = 1; |
||
41 | const TYPE_CACHE_ONLY_DUPLICATE_QUERIES_ONCE = 2; |
||
42 | |||
43 | /** |
||
44 | * Get the cache store to be used when caching queries forever. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getAllQueryCacheStore(): string |
||
52 | |||
53 | /** |
||
54 | * Get the cache store to be used when caching only duplicate queries. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getDuplicateQueryCacheStore(): string |
||
62 | |||
63 | /** |
||
64 | * Get the cache prefix to be appended to the specific cache tag for the model instance. |
||
65 | * Used when caching queries forever. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getAllQueryCachePrefix(): string |
||
73 | |||
74 | /** |
||
75 | * Get the cache prefix to be appended to the specific cache tag for the model instance. |
||
76 | * Used when caching only duplicate queries. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getDuplicateQueryCachePrefix(): string |
||
84 | |||
85 | /** |
||
86 | * Verify if forever query caching should run. |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function shouldCacheAllQueries(): bool |
||
94 | |||
95 | /** |
||
96 | * Verify if caching of duplicate queries should run. |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function shouldCacheDuplicateQueries(): bool |
||
104 | |||
105 | /** |
||
106 | * Get the "cache all queries forever" caching type. |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | public function cacheAllQueriesForeverType(): int |
||
114 | |||
115 | /** |
||
116 | * Get the "cache only duplicate queries once" caching type. |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | public function cacheOnlyDuplicateQueriesOnceType(): int |
||
124 | |||
125 | /** |
||
126 | * Enable caching of database queries for the current request. |
||
127 | * This is generally useful when working with rolled back database migrations. |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | public function enableQueryCache(): void |
||
135 | |||
136 | /** |
||
137 | * Disable caching of database queries for the current request. |
||
138 | * This is generally useful when working with rolled back database migrations. |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | public function disableQueryCache(): void |
||
146 | |||
147 | /** |
||
148 | * Verify if either forever query caching or duplicate query caching are enabled. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function canCacheQueries(): bool |
||
156 | |||
157 | /** |
||
158 | * Flush all the query cache for the specified store. |
||
159 | * Please note that this does not happen only for one caching type, but for all. |
||
160 | * |
||
161 | * @throws Exception |
||
162 | */ |
||
163 | public function flushQueryCache(): void |
||
173 | |||
174 | /** |
||
175 | * Flush the query cache from the store only for the tag corresponding to the model instance. |
||
176 | * If something fails, flush all existing cache for the specified store. |
||
177 | * This way, it's guaranteed that nothing will be left out of sync at the database level. |
||
178 | * |
||
179 | * @param Model $model |
||
180 | * @return void |
||
181 | * @throws Exception |
||
182 | */ |
||
183 | public function clearQueryCache(Model $model): void |
||
206 | } |
||
207 |