Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(Site): try to cache re-hit
Browse files Browse the repository at this point in the history
1. New Components Site , call by app()->site.
2. Use app()->site->getUser() instead of new User() to get class User
   with PHP runtime cache, to avoid Redis ReHit.
3. Use app()->site->getCurUser($grant) to get current User status,
   if this return False , It means this user is Anonymous.
  • Loading branch information
Rhilip committed Jul 28, 2019
1 parent 164de39 commit 2cd1a49
Show file tree
Hide file tree
Showing 42 changed files with 742 additions and 716 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
- **gitignore:** Add ignore of `/backup` folder

### Feat
- **Category:** Add Categories Support when upload torrent
- **Category:** Add Categories Manage Pane
- **Category:** Add Categories Support when upload torrent
- **Redis:** Add mutiDelete() function for Redis
- **Tracker:** Add `retry in` field when failed
- **UserInfo:** Add Cache Lock of user access_{time,ip} update
- **ban:** Add table `ban_usernames` and `ban_emails`
- **csrf:** Add Csrf Support
- **email:** Use Site::sendEmail to simple email sender
- **system:** can get more system info via class SystemInfoHelper

Expand Down
143 changes: 143 additions & 0 deletions apps/components/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 7/28/2019
* Time: 5:04 PM
*/

namespace apps\components;

use apps\libraries\Constant;
use Rid\Base\Component;

use apps\models\User;
use Rid\Utils\ClassValueCacheUtils;

class Site extends Component
{
use ClassValueCacheUtils;

protected $cur_user;

protected $users = [];
protected $map_username_to_id = [];

public function onRequestBefore()
{
parent::onRequestBefore();
$this->cur_user = null;
$this->users = [];
$this->map_username_to_id = [];
}

/**
* @param $uid
* @return User|bool return False means this user is not exist
*/
public function getUser($uid)
{
if (array_key_exists($uid, $this->users)) {
$user = $this->users[$uid];
} else {
$user = new User($uid); // TODO Handing if this user id does not exist
$this->users[$uid] = $user;
}
return $user;
}

/**
* @param $username
* @return User|bool
*/
public function getUserByUserName($username)
{
if (array_key_exists($username, $this->map_username_to_id)) {
$uid = $this->map_username_to_id[$username];
} else {
$uid = app()->redis->hGet(Constant::mapUsernameToId, $username);
if (false === $uid) {
$uid = app()->pdo->createCommand('SELECT id FROM `users` WHERE LOWER(`username`) = LOWER(:uname) LIMIT 1;')->bindParams([
'uname' => $username
])->queryScalar() ?: 0; // 0 means this username is not exist ???
app()->redis->hSet(Constant::mapUsernameToId, $username, $uid);
$this->map_username_to_id[$username] = $uid;
}
}

return $this->getUser($uid);
}

/**
* @param string $grant
* @return User|bool return False means this user is anonymous
*/
public function getCurUser($grant = 'cookies')
{
if (is_null($this->cur_user)) {
$this->cur_user = $this->loadCurUser($grant);
}
return $this->cur_user;
}

/**
* @param string $grant
* @return User|boolean
*/
protected function loadCurUser($grant = 'cookies')
{
$user_id = false;
if ($grant == 'cookies') $user_id = $this->loadCurUserFromCookies();
elseif ($grant == 'passkey') $user_id = $this->loadCurUserFromPasskey();
// elseif ($grant == 'oath2') $user_id = $this->loadCurUserFromOAth2();

if ($user_id !== false) {
$user_id = intval($user_id);
return $this->getUser($user_id);
}

return false;
}

protected function loadCurUserFromCookies()
{
$user_session_id = app()->request->cookie(Constant::cookie_name);
$user_id = app()->redis->zScore(Constant::mapUserSessionToId, $user_session_id);
if (false === $user_id) {
// First check cache
if (false === app()->redis->zScore(Constant::invalidUserSessionZset, $user_session_id)) {
// check session from database to avoid lost
$user_id = app()->pdo->createCommand('SELECT `uid` FROM `user_session_log` WHERE `sid` = :sid LIMIT 1;')->bindParams([
'sid' => $user_session_id
])->queryScalar();
if (false === $user_id) { // This session is not exist
app()->redis->zAdd(Constant::invalidUserSessionZset, time() + 86400, $user_session_id);
} else { // Remember it
app()->redis->zAdd(Constant::mapUserSessionToId, $user_id, $user_session_id);
}
}
}

return $user_id;
}

protected function loadCurUserFromPasskey()
{
$passkey = app()->request->get('passkey');
$user_id = app()->redis->zScore(Constant::mapUserPasskeyToId, $passkey);
if (false === $user_id) {
if (app()->redis->zScore(Constant::invalidUserPasskeyZset, $passkey) !== false) {
$user_id = app()->pdo->createCommand('SELECT `id` FROM `users` WHERE `passkey` = :passkey LIMIT 1;')->bindParams([
'passkey' => $passkey
])->queryScalar();
if (false === $user_id) {
app()->redis->zAdd(Constant::invalidUserPasskeyZset, time() + 600, $passkey);
} else {
app()->redis->zAdd(Constant::mapUserPasskeyToId, $user_id, $passkey);
}
}
}

return $user_id;
}
}
123 changes: 0 additions & 123 deletions apps/components/User/User.php

This file was deleted.

77 changes: 0 additions & 77 deletions apps/components/User/UserInterface.php

This file was deleted.

Loading

0 comments on commit 2cd1a49

Please sign in to comment.