Level 3
can anyone tell me
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Implemented cache . How can i get to know correctly implemented cache
{
// Validate incoming request data
$request['worksheet_id'] = $worksheet_id;
$request->validate([
'worksheet_id' => 'required',
'from_date' => 'nullable|date_format:Y-m-d',
'to_date' => 'nullable|date_format:Y-m-d',
]);
// Fetch the worksheet
$worksheet = Worksheet::find($worksheet_id);
if (!$worksheet) {
return [
'success' => false,
'message' => "You don't have access to this worksheet"
];
}
// Determine the date range
$start_date = $request->from_date ? Carbon::parse($request->from_date) : now()->subDays(5);
$end_date = $request->to_date ? Carbon::parse($request->to_date)->endOfDay() : now();
// Cache keys
$tasksCacheKey = "tasks_from{$start_date->format('Ymd')}to{$end_date->format('Ymd')}";
$commentsCacheKey = "comments_from{$start_date->format('Ymd')}to{$end_date->format('Ymd')}";
// Use cache tags for better control
$cacheTags = ['worksheet_' . $worksheet_id];
// Cache tasks with tags
$tasks = Cache::tags($cacheTags)->remember($tasksCacheKey, now()->addDay(), function () use ($worksheet_id, $start_date, $end_date) {
return Task::withoutGlobalScope('user')
->where('worksheet_id', $worksheet_id)
->whereBetween('created_at', [$start_date, $end_date]) // Added date filter
->with('user')
->latest()
->get()
->groupBy(function ($item) {
return $item->created_at ? $item->created_at->format('Y-m-d') : null;
});
});
// Cache comments with tags
$comments = Cache::tags($cacheTags)->remember($commentsCacheKey, now()->addDay(), function () use ($worksheet_id, $start_date, $end_date) {
// Fetch task IDs for the worksheet
$taskIds = Task::where('worksheet_id', $worksheet_id)->pluck('_id');
// Fetch comments for tasks within the specified date range
return TaskComment::whereIn('task_id', $taskIds)
->whereBetween('created_at', [$start_date, $end_date])
->with(['user', 'task'])
->latest()
->get()
->groupBy(function ($item) {
return $item->created_at->format('Y-m-d');
});
});
// Fetch notifications and new comments count
$newComments = DatabaseNotification::whereRaw([
'data.worksheet._id' => $worksheet->_id,
'notifiable_id' => Auth::user()->_id
])->whereIn('type', [CommentAdded::class, UserMentioned::class])->unread()->get();
// Prepare the output
return [
'success' => true,
'message' => "Activity data fetched successfully",
'comments' => $comments,
'tasks' => $tasks,
'new_comments' => $newComments->count(),
'tasks_with_new_comments' => $newComments->groupBy('data.task._id')->count(),
'worksheet' => $worksheet,
'total_hours' => Task::withoutGlobalScope('user')
->where('worksheet_id', $worksheet_id)
->has('childs', '=', 0)
->sum('estimate'),
];
}```
Invalidate cache , implemented in model
```protected static function boot()
{
parent::boot();
// Listen to the created, updated, and deleted events
static::created(function ($comment) {
TaskComment::invalidateCache($comment);
});
static::updated(function ($comment) {
TaskComment::invalidateCache($comment);
});
static::deleted(function ($comment) {
TaskComment::invalidateCache($comment);
});
}
/**
* Invalidate cache related to the task and worksheet when a comment changes.
*
* @param TaskComment $comment
*/
protected static function invalidateCache($comment)
{
// Get the related task and worksheet ID
$taskId = $comment->task_id;
$worksheetId = $comment->task->worksheet_id;
// Invalidate the cache using tags
Cache::tags("worksheet_{$worksheetId}")->flush();
// Optionally, you can also target specific cache keys if you are not using tags
// Cache::forget("worksheet_{$worksheetId}comments_from{$startDate}to{$endDate}");
// Cache::forget("worksheet_{$worksheetId}tasks_from{$startDate}to{$endDate}");
}```
Please or to participate in this conversation.