Browse Source

implement DHT data cache

main
ghost 3 years ago
parent
commit
72949e0834
  1. BIN
      database/cloud-server.mwb
  2. 21
      src/application/controller/api/image.php
  3. 34
      src/application/controller/api/user/avatar.php
  4. 34
      src/application/controller/api/user/profile.php
  5. 2
      src/application/view/common/module/post.phtml
  6. 3
      src/config-default.php
  7. 1
      src/public/css/template/default/module/feed.css
  8. 1
      src/public/css/template/default/module/following.css
  9. 1
      src/public/css/template/default/module/post.css
  10. 4
      src/public/js/module/feed.js
  11. 12
      src/public/js/module/following.js
  12. 4
      src/public/js/module/post.js
  13. 2
      src/public/js/module/users.js

BIN
database/cloud-server.mwb

Binary file not shown.

21
src/application/controller/api/image.php

@ -1,21 +0,0 @@ @@ -1,21 +0,0 @@
<?php
if (isset($_GET['hash'])) {
$fileName = md5($_GET['hash']);
$filePath = PROJECT_DIR . '/cache/image/' . $fileName . '.jpeg';
if (!file_exists($filePath)) {
$icon = new Icon();
$image = $icon->generateImageResource($fileName, 42, 42, false);
file_put_contents($filePath, $image);
}
$image = file_get_contents($filePath);
header("Content-Type: image/jpeg");
echo $image;
}

34
src/application/controller/api/user/avatar.php

@ -10,7 +10,15 @@ if (isset($_SESSION['userName'])) { @@ -10,7 +10,15 @@ if (isset($_SESSION['userName'])) {
$userName = isset($_GET['userName']) ? Filter::userName($_GET['userName']) : $_SESSION['userName'];
if ($avatarVersions = $_twister->getDHT($userName, 'avatar', 's')) {
if ($avatar = $_memcache->get('api.user.avatar.' . $userName)) {
$response = [
'success' => true,
'message' => _('Avatar successfully received from Cache'),
'avatar' => $avatar
];
} else if ($avatarVersions = $_twister->getDHT($userName, 'avatar', 's')) {
// Check avatar exists
if ($userId = $_modelUser->getUserId($userName)) {
@ -36,10 +44,12 @@ if (isset($_SESSION['userName'])) { @@ -36,10 +44,12 @@ if (isset($_SESSION['userName'])) {
$response = [
'success' => true,
'message' => _('Avatar successfully received'),
'message' => _('Avatar successfully received from DHT'),
'avatar' => $avatarInfo['data']
];
$_memcache->set('api.user.avatar.' . $userName, $avatarInfo['data'], MEMCACHE_COMPRESS, MEMCACHE_DHT_AVATAR_TIMEOUT);
} else {
$response = [
@ -49,12 +59,26 @@ if (isset($_SESSION['userName'])) { @@ -49,12 +59,26 @@ if (isset($_SESSION['userName'])) {
];
}
// Generate identity icon
} else {
$fileName = md5($userName);
$filePath = PROJECT_DIR . '/cache/image/' . $fileName . '.jpeg';
if (!file_exists($filePath)) {
$icon = new Icon();
$image = $icon->generateImageResource($fileName, 42, 42, false);
file_put_contents($filePath, $image);
}
$image = file_get_contents($filePath);
$response = [
'success' => false,
'message' => _('Could not receive avatar details'),
'avatar' => false
'success' => true,
'message' => _('Avatar successfully received from Identity'),
'avatar' => 'data:image/jpeg;base64,' . base64_encode($image)
];
}

34
src/application/controller/api/user/profile.php

@ -10,7 +10,15 @@ if (isset($_SESSION['userName'])) { @@ -10,7 +10,15 @@ if (isset($_SESSION['userName'])) {
$userName = isset($_GET['userName']) ? Filter::userName($_GET['userName']) : $_SESSION['userName'];
if ($userProfileVersions = $_twister->getDHT($userName, 'profile', 's')) {
if ($profile = $_memcache->get('api.user.profile.' . $userName)) {
$response = [
'success' => true,
'message' => _('Profile successfully received from Cache'),
'profile' => $profile
];
} else if ($userProfileVersions = $_twister->getDHT($userName, 'profile', 's')) {
// Check user exists
if ($userId = $_modelUser->getUserId($userName)) {
@ -43,20 +51,24 @@ if (isset($_SESSION['userName'])) { @@ -43,20 +51,24 @@ if (isset($_SESSION['userName'])) {
// Get latest version available
if ($profileInfo = $_modelProfile->get($userId)) {
$profile = [
'userName' => $userName,
'fullName' => $profileInfo['fullName'],
'location' => $profileInfo['location'],
'url' => $profileInfo['url'],
'bitMessage' => $profileInfo['bitMessage'],
'tox' => $profileInfo['tox'],
'bio' => nl2br($profileInfo['bio']),
];
$response = [
'success' => true,
'message' => _('Profile successfully received'),
'profile' => [
'userName' => $userName,
'fullName' => $profileInfo['fullName'],
'location' => $profileInfo['location'],
'url' => $profileInfo['url'],
'bitMessage' => $profileInfo['bitMessage'],
'tox' => $profileInfo['tox'],
'bio' => nl2br($profileInfo['bio']),
]
'message' => _('Profile successfully received from DHT'),
'profile' => $profile
];
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT);
} else {
$response = [

2
src/application/view/common/module/post.phtml

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
<div class="modulePost" id="modulePost">
<form action="/" method="POST" name="post">
<div class="avatar">
<img src="<?php echo PROJECT_HOST ?>/api/image?hash=<?php echo $_SESSION['userName'] ?>" alt="" />
<img src="" alt="" />
</div>
<div class="message">
<textarea name="post" placeholder="<?php echo _('Enter your post...') ?>"></textarea>

3
src/config-default.php

@ -25,6 +25,9 @@ define('TWISTER_PASSWORD', ''); @@ -25,6 +25,9 @@ define('TWISTER_PASSWORD', '');
// MEMCACHE
define('MEMCACHE_HOST', 'localhost');
define('MEMCACHE_PORT', 11211);
define('MEMCACHE_COMPRESS', false);
define('MEMCACHE_DHT_AVATAR_TIMEOUT', 3600);
define('MEMCACHE_DHT_PROFILE_TIMEOUT', 3600);
// COMMON
define('APPLICATION_ALLOW_REGISTRATION', true);

1
src/public/css/template/default/module/feed.css

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
border: 2px #fff solid;
width: 46px;
height: 46px;
display: none
}
.moduleFeed .item .message {

1
src/public/css/template/default/module/following.css

@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
border: 2px #fff solid;
width: 16px;
height: 16px;
display: none
}
.moduleFollowing .item .action {

1
src/public/css/template/default/module/post.css

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
border: 2px #fff solid;
width: 46px;
height: 46px;
display: none
}
.modulePost .message {

4
src/public/js/module/feed.js

@ -38,7 +38,7 @@ var ModuleFeed = { @@ -38,7 +38,7 @@ var ModuleFeed = {
'href': 'people/' + userName
}).append(
$('<img/>', {
'src': '/api/image?hash=' + userName,
'src': '',
'alt': '',
})
)
@ -83,7 +83,7 @@ var ModuleFeed = { @@ -83,7 +83,7 @@ var ModuleFeed = {
if (response.success) {
if (response.avatar) {
$(feed).find('div[data-username="' + userName + '"] .avatar img').attr('src', response.avatar);
$(feed).find('div[data-username="' + userName + '"] .avatar img').attr('src', response.avatar).show();
}
} else {

12
src/public/js/module/following.js

@ -15,7 +15,7 @@ var ModuleFollowing = { @@ -15,7 +15,7 @@ var ModuleFollowing = {
'href': 'people/' + userName
}).append(
$('<img/>', {
'src': '/api/image?hash=' + userName,
'src': '',
'alt': '',
})
)
@ -94,7 +94,7 @@ var ModuleFollowing = { @@ -94,7 +94,7 @@ var ModuleFollowing = {
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
console.log(textStatus, errorThrown);
}
});
},
@ -110,7 +110,7 @@ var ModuleFollowing = { @@ -110,7 +110,7 @@ var ModuleFollowing = {
if (response.success) {
if (response.avatar) {
$(list).find('div[data-username="' + userName + '"] .avatar img').attr('src', response.avatar);
$(list).find('div[data-username="' + userName + '"] .avatar img').attr('src', response.avatar).show();
}
} else {
@ -120,7 +120,7 @@ var ModuleFollowing = { @@ -120,7 +120,7 @@ var ModuleFollowing = {
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
console.log(textStatus, errorThrown);
}
});
},
@ -149,7 +149,7 @@ var ModuleFollowing = { @@ -149,7 +149,7 @@ var ModuleFollowing = {
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
console.log(textStatus, errorThrown);
}
});
},
@ -176,7 +176,7 @@ var ModuleFollowing = { @@ -176,7 +176,7 @@ var ModuleFollowing = {
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
console.log(textStatus, errorThrown);
}
});
},

4
src/public/js/module/post.js

@ -11,7 +11,7 @@ var ModulePost = { @@ -11,7 +11,7 @@ var ModulePost = {
if (response.success) {
if (response.avatar) {
$(element).find('img').attr('src', response.avatar);
$(element).find('img').attr('src', response.avatar).show();
}
} else {
@ -27,7 +27,7 @@ var ModulePost = { @@ -27,7 +27,7 @@ var ModulePost = {
},
add: function() {
var input = $('#modulePost > .message > textarea');
var input = $('#modulePost > .message > textarea');
$.ajax({
url: 'api/post/add',

2
src/public/js/module/users.js

@ -14,7 +14,7 @@ var ModuleUsers = { @@ -14,7 +14,7 @@ var ModuleUsers = {
'href': 'people/' + userName
}).append(
$('<img/>', {
'src': '/api/image?hash=' + userName,
'src': '',
'alt': '',
})
)

Loading…
Cancel
Save