230 lines
7.4 KiB
PHP
230 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\Comment;
|
|
use App\Models\Downloads;
|
|
use App\Models\Episode;
|
|
use App\Models\Hentai;
|
|
use App\Models\PopularDaily;
|
|
use App\Models\PopularMonthly;
|
|
use App\Models\PopularWeekly;
|
|
use App\Models\User;
|
|
use Conner\Tagging\Model\Tag;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CacheHelper
|
|
{
|
|
public static function getRecentlyReleased(bool $guest)
|
|
{
|
|
$guestString = $guest ? 'guest' : 'authed';
|
|
|
|
return Cache::remember('recently_released_'.$guestString, now()->addMinutes(60), function () use ($guest) {
|
|
return Episode::with('gallery')
|
|
->when($guest, fn ($query) => $query->withoutTags(['loli', 'shota']))
|
|
->orderBy('release_date', 'desc')
|
|
->limit(16)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getRecentlyUploaded(bool $guest)
|
|
{
|
|
$guestString = $guest ? 'guest' : 'authed';
|
|
|
|
return Cache::remember('recently_uploaded'.$guestString, now()->addMinutes(5), function () use ($guest) {
|
|
return Episode::with('gallery')
|
|
->when($guest, fn ($query) => $query->withoutTags(['loli', 'shota']))
|
|
->orderBy('created_at', 'desc')
|
|
->limit(16)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getTotalViewCount()
|
|
{
|
|
return Cache::remember('total_view_count', now()->addMinutes(60), function () {
|
|
return Episode::sum('view_count');
|
|
});
|
|
}
|
|
|
|
public static function getTotalEpisodeCount()
|
|
{
|
|
return Cache::remember('total_episode_count', now()->addMinutes(60), function () {
|
|
return Episode::count();
|
|
});
|
|
}
|
|
|
|
public static function getTotalHentaiCount()
|
|
{
|
|
return Cache::remember('total_hentai_count', now()->addMinutes(60), function () {
|
|
return Hentai::count();
|
|
});
|
|
}
|
|
|
|
public static function getTotalUserCount()
|
|
{
|
|
return Cache::remember('total_user_count', now()->addMinutes(60), function () {
|
|
return User::count();
|
|
});
|
|
}
|
|
|
|
public static function getTotalCommentCount()
|
|
{
|
|
return Cache::remember('total_comment_count', now()->addMinutes(60), function () {
|
|
return Comment::count();
|
|
});
|
|
}
|
|
|
|
public static function getTotalLikeCount()
|
|
{
|
|
return Cache::remember('total_like_count', now()->addMinutes(60), function () {
|
|
return DB::table('markable_likes')->count();
|
|
});
|
|
}
|
|
|
|
public static function getTotalDownloadCount()
|
|
{
|
|
return Cache::remember('total_download_count', now()->addMinutes(60), function () {
|
|
return Downloads::sum('count');
|
|
});
|
|
}
|
|
|
|
public static function get4kEpisodeCount()
|
|
{
|
|
return Cache::remember('episodes_4k_count', now()->addMinutes(60), function () {
|
|
return Episode::where('interpolated', true)->count();
|
|
});
|
|
}
|
|
|
|
public static function getUHD48FpsEpisodeCount()
|
|
{
|
|
return Cache::remember('episodes_uhd48_count', now()->addMinutes(60), function () {
|
|
return Episode::where('interpolated_uhd', true)->count();
|
|
});
|
|
}
|
|
|
|
public static function getTodayViewCount()
|
|
{
|
|
return Cache::remember('today_view_count', now()->addMinutes(30), function () {
|
|
return PopularDaily::whereDate('created_at', today())->count();
|
|
});
|
|
}
|
|
|
|
public static function getWeeklyViewCount()
|
|
{
|
|
return Cache::remember('weekly_view_count', now()->addMinutes(60), function () {
|
|
return PopularWeekly::whereDate('created_at', '>=', today()->subDays(7))->count();
|
|
});
|
|
}
|
|
|
|
public static function getPreviousWeeklyViewCount()
|
|
{
|
|
return Cache::remember('prev_weekly_view_count', now()->addMinutes(60), function () {
|
|
return PopularWeekly::whereDate('created_at', '>=', today()->subDays(14))
|
|
->whereDate('created_at', '<', today()->subDays(7))
|
|
->count();
|
|
});
|
|
}
|
|
|
|
public static function getAverageViewsPerEpisode()
|
|
{
|
|
return Cache::remember('avg_views_per_episode', now()->addMinutes(60), function () {
|
|
$totalEpisodes = Episode::count();
|
|
if ($totalEpisodes === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return round(Episode::sum('view_count') / $totalEpisodes);
|
|
});
|
|
}
|
|
|
|
public static function getNewEpisodesThisWeek()
|
|
{
|
|
return Cache::remember('new_episodes_this_week', now()->addMinutes(60), function () {
|
|
return Episode::whereDate('created_at', '>=', today()->subDays(7))->count();
|
|
});
|
|
}
|
|
|
|
public static function getTopTags()
|
|
{
|
|
return Cache::remember('top_tags_stats', now()->addMinutes(120), function () {
|
|
return Tag::where('count', '>', 0)
|
|
->orderBy('count', 'desc')
|
|
->limit(10)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getPopularAllTime(bool $guest)
|
|
{
|
|
$guestString = $guest ? 'guest' : 'authed';
|
|
|
|
return Cache::remember('top_hentai_alltime'.$guestString, now()->addMinutes(360), function () use ($guest) {
|
|
return Episode::with('gallery')
|
|
->when($guest, fn ($query) => $query->withoutTags(['loli', 'shota']))
|
|
->orderBy('view_count', 'desc')
|
|
->limit(16)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getPopularMonthly()
|
|
{
|
|
return Cache::remember('top_hentai_monthly', now()->addMinutes(360), function () {
|
|
return PopularMonthly::groupBy('episode_id')
|
|
->select('episode_id', DB::raw('count(*) as total'))
|
|
->with('episode.gallery')
|
|
->orderBy('total', 'desc')
|
|
->limit(16)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getPopularWeekly()
|
|
{
|
|
return Cache::remember('top_hentai_weekly', now()->addMinutes(360), function () {
|
|
return PopularWeekly::groupBy('episode_id')
|
|
->select('episode_id', DB::raw('count(*) as total'))
|
|
->with('episode.gallery')
|
|
->with('episode.studio')
|
|
->orderBy('total', 'desc')
|
|
->limit(16)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getPopularDaily()
|
|
{
|
|
return Cache::remember('top_hentai_daily', now()->addMinutes(30), function () {
|
|
return PopularDaily::groupBy('episode_id')
|
|
->select('episode_id', DB::raw('count(*) as total'))
|
|
->with('episode.gallery')
|
|
->orderBy('total', 'desc')
|
|
->limit(16)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
public static function getMostLikes()
|
|
{
|
|
return Cache::remember('top_likes', now()->addMinutes(30), function () {
|
|
return DB::table('markable_likes')->groupBy('markable_id')->select('markable_id', DB::raw('count(*) as total'))->orderBy('total', 'desc')->limit(16)->get();
|
|
});
|
|
}
|
|
|
|
public static function getAllTags()
|
|
{
|
|
return Cache::remember('all_tags', now()->addMinutes(10080), function () {
|
|
return Tag::where('count', '>', 0)->orderBy('slug', 'ASC')->get();
|
|
});
|
|
}
|
|
|
|
public static function getLatestComments()
|
|
{
|
|
return Cache::remember('latest_comments', now()->addMinutes(60), function () {
|
|
return Comment::with('user')->latest()->take(10)->get();
|
|
});
|
|
}
|
|
} |