Skip to content

Commit

Permalink
reload device cache on device create
Browse files Browse the repository at this point in the history
  • Loading branch information
TrystanLea committed May 2, 2022
1 parent 3a088ce commit 5556e42
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions device_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ public function create($userid, $nodeid, $name, $description, $type, $devicekey
$type = '';
}

if ($this->redis) {
// Reload all devices from mysql here to ensure cache is not out of sync
$this->load_list_to_redis($userid);
}

if (!$this->exists_nodeid($userid, $nodeid)) {
$stmt = $this->mysqli->prepare("INSERT INTO device (userid,nodeid,name,description,type,devicekey) VALUES (?,?,?,?,?,?)");
$stmt->bind_param("isssss",$userid,$nodeid,$name,$description,$type,$devicekey);
Expand All @@ -366,8 +371,6 @@ public function create($userid, $nodeid, $name, $description, $type, $devicekey
if ($deviceid > 0) {
// Add the device to redis
if ($this->redis) {
// Reload all devices from mysql here to ensure cache is not out of sync
$this->load_list_to_redis($userid);
$this->redis->sAdd("user:device:$userid", $deviceid);
$this->redis->hMSet("device:".$deviceid, array(
'id'=>$deviceid,
Expand All @@ -383,7 +386,7 @@ public function create($userid, $nodeid, $name, $description, $type, $devicekey
}
return array('success'=>false, 'result'=>"SQL returned invalid insert feed id");
}
return array('success'=>false, 'message'=>'Device already exists');
return array('success'=>false, 'message'=>'Device already exists, cache reloaded, try reloading the page');
}

public function delete($id) {
Expand Down

0 comments on commit 5556e42

Please sign in to comment.