55use Illuminate \Bus \Queueable ;
66use Illuminate \Console \Scheduling \CallbackEvent ;
77use Illuminate \Contracts \Queue \ShouldQueue ;
8+ use Illuminate \Foundation \Bus \PendingDispatch ;
89use Illuminate \Support \Arr ;
910use Illuminate \Support \Facades \Blade ;
1011use Illuminate \Support \Facades \Cache ;
@@ -44,18 +45,27 @@ trait CachesValue
4445 */
4546 protected $ expression = null ;
4647
47- /** @var array<string, mixed> */
48+ /**
49+ * The parameters this cache should be stored with.
50+ *
51+ * @var array<string, mixed>
52+ */
4853 protected $ parameters = [];
4954
50- private bool $ isUpdating = false ;
55+ /**
56+ * Indicates whether this cache is currently updating or not.
57+ *
58+ * @var bool
59+ */
60+ private $ isUpdating = false ;
5161
5262 /**
5363 * Update the cached value, this method expects an event if
5464 * the cacher is not static.
5565 *
56- * @internal You shouldn't call this yourself.
66+ * @internal You shouldn't call this yourself, use the `CachesValue::update` method instead .
5767 */
58- final public function handle ($ event = null ): mixed
68+ final public function handle ($ event = null ): void
5969 {
6070 $ this ->isUpdating = true ;
6171
@@ -68,16 +78,14 @@ final public function handle($event = null): mixed
6878 : $ this ->run ($ event );
6979
7080 if (is_null ($ value )) {
71- return null ;
81+ return ;
7282 }
7383
7484 Cache::driver ($ driver )->forever ($ cacheKey , $ value );
7585
7686 PermanentCacheUpdated::dispatch ($ this );
7787
7888 $ this ->isUpdating = false ;
79-
80- return $ value ;
8189 }
8290
8391 public function getParameters ()
@@ -113,20 +121,17 @@ public function shouldBeUpdating(): bool
113121 /**
114122 * Manually force a static cache to update.
115123 */
116- final public static function update ($ parameters = []): mixed
124+ final public static function update ($ parameters = []): ? PendingDispatch
117125 {
118126 $ instance = app ()->make (static ::class, $ parameters );
119127
120- if (
121- app ()->runningInConsole () &&
122- is_subclass_of (static ::class, ShouldQueue::class)
123- ) {
124- dispatch ($ instance );
128+ if (! is_subclass_of (static ::class, ShouldQueue::class)) {
129+ $ instance ->handle ();
125130
126131 return null ;
127132 }
128133
129- return $ instance-> handle ( );
134+ return dispatch ( $ instance );
130135 }
131136
132137 /**
@@ -147,16 +152,27 @@ final public static function get($default = null, bool $update = false): mixed
147152
148153 $ cache = Cache::driver ($ driver );
149154
150- if (
151- $ update ||
152- ! $ cache ->has ($ cacheKey )
153- ) {
154- return static ::update ($ parameters ?? []);
155+ if ($ update && ! $ cache ->has ($ cacheKey )) {
156+ static ::update ($ parameters ?? [])->onConnection ('sync ' );
155157 }
156158
157159 return $ cache ->get ($ cacheKey , $ default );
158160 }
159161
162+ /**
163+ * Force an update of the cache and return the updated value.
164+ *
165+ * @return V|mixed
166+ */
167+ final public static function updateAndGet ($ parameters = []): mixed
168+ {
169+ [$ driver , $ cacheKey ] = self ::store ($ parameters );
170+
171+ static ::update ($ parameters )->onConnection ('sync ' );
172+
173+ return Cache::driver ($ driver )->get ($ cacheKey );
174+ }
175+
160176 /**
161177 * Get the cached value this cacher provides.
162178 *
@@ -237,7 +253,7 @@ private static function parseCacheString($class, ?string $store, ?array $paramet
237253 }
238254
239255 $ cacheDriver ??= config ('cache.default ' );
240- $ cacheKey ??= preg_replace ('/[^A-Za-z0-9]+/ ' , '_ ' , strtolower (snake_case ($ class )));
256+ $ cacheKey ??= preg_replace ('/[^A-Za-z0-9]+/ ' , '_ ' , strtolower (\Str:: snake ($ class )));
241257
242258 if ($ parameters ) {
243259 $ cacheKey .= ': ' .http_build_query ($ parameters );
0 commit comments