/** * Handle the event. * * @param CspotItemUpdated $event * @return void */ public function handle(CspotItemUpdated $event) { // get item $key = 'offline-' . $event->item->plan_id; //$key .= '-'.(1*$event->item->seq_no); // delete cached data of this item - NEW: // delete cached data of the whole PLAN as it's now outdated $cache = PlanCache::where('key', 'like', $key . '-%')->delete(); }
function deleteCachedItemsFromPastPlans($plan_id) { $caches = PlanCache::get(); // check each cache item if it is outdated foreach ($caches as $cache) { if ($cache->plan->date < Carbon::now() && $cache->plan->id != $plan_id) { $cache->delete(); } } }
/** * receive pre-rendered slides from client and buffer them for other users to download */ public function postCache(Request $request, $plan_id) { // validation if (!$request->has('key')) { return response()->json(['status' => 400, 'data' => "missing key or value!"], 400); } // allow for an empty value.... if (!$request->has('value')) { $request->value = ' '; } // make sure it's a valid plan id $plan = Plan::find($plan_id); // save key and value to local cache (replacing existing values) if ($plan->count()) { // keep only future plans in cache deleteCachedItemsFromPastPlans($plan_id); // is there already a value for that key? $cache = PlanCache::where('key', $request->key)->first(); if ($cache) { // yes, so just update the existing value $cache->update(['value' => $request->value]); // return ok response return response()->json(['status' => 200, 'data' => "Updated!"], 200); } // extract item id from request if ($request->has('item_id')) { $item_id = $request->item_id; } else { $item_id = 9999999; } // no, so create a new key/value pair in the PlanCaches table $cache = new PlanCache(['key' => $request->key, 'value' => $request->value, 'item_id' => $item_id]); $plan->planCaches()->save($cache); // return ok response return response()->json(['status' => 200, 'data' => "Inserted!"], 200); } return response()->json(['status' => 404, 'data' => "plan with id {$plan_id} not found"], 404); }