Selaa lähdekoodia

Fix serializing datasets and caching behaviour

staging
Christopher Ross Hind 3 vuotta sitten
vanhempi
commit
e0fcab751f
7 muutettua tiedostoa jossa 83 lisäystä ja 10 poistoa
  1. +7
    -1
      app/Console/Commands/Stats.php
  2. +4
    -3
      app/Http/Controllers/IndexController.php
  3. +18
    -0
      app/Knockout/Dataset.php
  4. +1
    -1
      app/Knockout/Event.php
  5. +48
    -0
      app/Knockout/Stat.php
  6. +1
    -1
      app/Knockout/Subforum.php
  7. +4
    -4
      resources/views/page/index.blade.php

+ 7
- 1
app/Console/Commands/Stats.php Näytä tiedosto

@@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Knockout\Stat;
use Illuminate\Console\Command;

class Stats extends Command {
@@ -11,7 +12,12 @@ class Stats extends Command {

public function handle()
{
echo 'wolo';
try {
Stat::updateAll();
echo '[ OK ] Stats cached' . PHP_EOL;
} catch (Exception $e) {
echo '[FAIL] Stats cached' . PHP_EOL;
}
}

}

+ 4
- 3
app/Http/Controllers/IndexController.php Näytä tiedosto

@@ -2,12 +2,12 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Knockout\Subforum;
use App\Knockout\Stat;

class IndexController extends Controller {

public function get(Request $request)
public function get()
{
$subforums = Subforum::all();
$records = $subforums->getRecords();
@@ -18,7 +18,8 @@ class IndexController extends Controller {

return view('page/index', [
'leftSubforums' => $leftSubforums,
'rightSubforums' => $rightSubforums
'rightSubforums' => $rightSubforums,
'stat' => Stat::all()->getRecord()
]);
}



+ 18
- 0
app/Knockout/Dataset.php Näytä tiedosto

@@ -13,6 +13,24 @@ class Dataset
private $currentPage = null;
private $recordsPerPage = null;

public function __serialize(): array
{
return [
'records' => $this->records,
'totalRecords' => $this->totalRecords,
'currentPage' => $this->currentPage,
'recordsPerPage' => $this->recordsPerPage
];
}

public function __unserialize(array $data): void
{
$this->records = $data['records'] ?? [];
$this->totalRecords = $data['totalRecords'] ?? null;
$this->currentPage = $data['currentPage'] ?? null;
$this->recordsPerPage = $data['recordsPerPage'] ?? null;
}

public function __construct($records)
{
if (!is_array($records)) $records = [$records];


+ 1
- 1
app/Knockout/Event.php Näytä tiedosto

@@ -52,7 +52,7 @@ class Event {
public static function all(): Dataset
{
return Cache::get('events', function() {
return self::requestAll();
return self::updateAll();
});
}



+ 48
- 0
app/Knockout/Stat.php Näytä tiedosto

@@ -0,0 +1,48 @@
<?php

namespace App\Knockout;

use Illuminate\Support\Facades\Cache;

class Stat {

public $users;
public $posts;
public $threads;
public $ratings;

public static function unwrap($stat)
{
$s = new self();

$s->users = $stat->userCount;
$s->posts = $stat->postCount;
$s->threads = $stat->threadCount;
$s->ratings = $stat->ratingsCount;

return $s;
}

private static function requestAll(): Dataset
{
$data = (new AbstractData)->httpGet('/stats');
$json = json_decode($data);
$record = self::unwrap($json);
return (new Dataset($record));
}

public static function updateAll(): Dataset
{
$data = self::requestAll();
Cache::put('stats', $data, 7200);
return $data;
}

public static function all(): Dataset
{
return Cache::get('stats', function() {
return self::updateAll();
});
}

}

+ 1
- 1
app/Knockout/Subforum.php Näytä tiedosto

@@ -54,7 +54,7 @@ class Subforum {
public static function all(): Dataset
{
return Cache::get('subforums', function() {
return self::requestAll();
return self::updateAll();
});
}



+ 4
- 4
resources/views/page/index.blade.php Näytä tiedosto

@@ -21,10 +21,10 @@
</div>
<div class="clearfix"></div>
<div class="row stats">
<span>Users: 11682</span>
<span>Threads: 19684</span>
<span>Posts: 655765</span>
<span>Ratings: 9013296</span>
<span>Users: {{ $stat->users }}</span>
<span>Threads: {{ $stat->threads }}</span>
<span>Posts: {{ $stat->posts }}</span>
<span>Ratings: {{ $stat->ratings }}</span>
</div>
@endsection



Loading…
Peruuta
Tallenna