Caching

Product:
You need to modify the getFriendsList() and getFriendsIds() in integration.php file.

a. Show all online users with caching enabled

To use caching for all online users, no change is required; simply use the getFriendsList function from above (Update Who’s Online list fetching mechanism).

b. Show online friends with caching enabled

When using caching and wanting to show only online friends, you still need to modify the getFriendsList function to show all online users as follows.

“`

function getFriendsList($userid, $time){
  $sql = ("select users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message,  cometchat_status.isdevice from users left join cometchat_status on users.id = cometchat_status.userid where (('" . mysqli_real_escape_string($GLOBALS['dbh'],     $time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and    (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username    asc");
  return $sql;
}
function getFriendsList($userid, $time){
  $sql = ("select users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message,  cometchat_status.isdevice from left join cometchat_status on = cometchat_status.userid where (('" . mysqli_real_escape_string($GLOBALS['dbh'],     $time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and    (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username    asc");
  return $sql;
}
function getFriendsList($userid, $time){
  $sql = ("select users.id userid, users.username username, users.username link, users.id avatar, cometchat_status.lastactivity, cometchat_status.lastseen, cometchat_status.lastseensetting, cometchat_status.status, cometchat_status.message,  cometchat_status.isdevice from left join cometchat_status on = cometchat_status.userid where (('" . sql_real_escape_string($GLOBALS['dbh'],     $time) . "' - cometchat_status.lastactivity < '".((ONLINE_TIMEOUT)*2)."') OR cometchat_status.isdevice = 1) and    (cometchat_status.status IS NULL OR cometchat_status.status <> 'invisible' OR cometchat_status.status <> 'offline') order by username    asc");
  return $sql;
}

Then you need to modify getFriendsIds to return friends for the logged in user. The getFriendsIds() function should return SQL query which retrieves userids of friends.

function getFriendsIds($userid) {
  $sql = ("select toid friendid from friends where fromid ='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status = 1 union select   fromid friendid from friends where toid='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status=1");
  return $sql;
}
function getFriendsIds($userid) {
  $sql = ("select toid friendid from friends where fromid ='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status = 1 union select   fromid friendid from friends where toid='".mysqli_real_escape_string($GLOBALS['dbh'],$userid)."' and status=1");
  return $sql;
}
function getFriendsIds($userid) {
  $sql = ("select toid friendid from friends where fromid ='".sql_real_escape_string($GLOBALS['dbh'],$userid)."' and status = 1 union select   fromid friendid from friends where toid='".sql_real_escape_string($GLOBALS['dbh'],$userid)."' and status=1");
  return $sql;
}

The getFriendsList function helps to store the details of all the online users in a cache and getFriendsIds helps to store only IDs of all the friends in a cache. CometChat then uses both the caches to get the details of all the friends.

Caching is already enabled. No configuration is required.
  • Was this helpful?
  • Yes   No