Fetch posts with labels - render in html - caching

<div class="jobs">


<?php


/* =========================

   CONFIG

========================= */


$label = 'Bank%20Jobs';

$keyword = 'apprentice';


$feedUrl = 'https://govtjobsportal.in/feeds/posts/summary/?max-results=150&alt=json&category=' . $label;


$cacheDir  = __DIR__ . '/cache';

$cacheFile = $cacheDir . '/bank-jobs.json';


$cacheTime = 3600; // 1 hour


$lockFile = $cacheDir . '/cache.lock';




/* =========================

   INIT CACHE FOLDER

========================= */


if (!is_dir($cacheDir)) {

    mkdir($cacheDir, 0755, true);

}




/* =========================

   CHECK CACHE VALIDITY

========================= */


$cacheExists = file_exists($cacheFile);

$isExpired = !$cacheExists || (time() - filemtime($cacheFile) > $cacheTime);




/* =========================

   LOCK SYSTEM (IMPORTANT)

   PREVENTS CACHE STAMPEDE

========================= */


$lock = fopen($lockFile, 'c');


if ($isExpired) {


    if (flock($lock, LOCK_EX | LOCK_NB)) {


        // Only ONE request will refresh cache


        $context = stream_context_create([

            'http' => [

                'timeout' => 10

            ]

        ]);


        $json = @file_get_contents($feedUrl, false, $context);


        if ($json) {

            file_put_contents($cacheFile, $json);

        }


        flock($lock, LOCK_UN);

    }

}




/* =========================

   LOAD CACHE (FALLBACK SAFE)

========================= */


if (file_exists($cacheFile)) {

    $json = file_get_contents($cacheFile);

} else {

    echo "<p>Data temporarily unavailable.</p>";

    exit;

}


$data = json_decode($json, true);


if (empty($data['feed']['entry'])) {

    echo "<p>No jobs found.</p>";

    exit;

}




/* =========================

   DISPLAY RESULTS

========================= */


foreach ($data['feed']['entry'] as $entry) {


    $title = $entry['title']['$t'] ?? '';


    if (stripos($title, $keyword) === false) {

        continue;

    }


    $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');


    $summary = strip_tags($entry['summary']['$t'] ?? '');

    $summary = mb_substr($summary, 0, 150);

    $summary = htmlspecialchars($summary, ENT_QUOTES, 'UTF-8');


    $url = '';


    foreach ($entry['link'] as $link) {

        if (($link['rel'] ?? '') === 'alternate') {

            $url = $link['href'];

            break;

        }

    }


    $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');


    $date = date('j F Y', strtotime($entry['published']['$t'] ?? ''));


    ?>


    <div class="bs-jobs">


        <h2 class="bs-job-title"><?= $title ?></h2>


        <table class="bs-job-table">

            <tbody>

                <tr>

                    <td>Education Qualification</td>

                    <td>10th Pass</td>

                </tr>

                <tr>

                    <td>Location Eligibility</td>

                    <td>Sasaram</td>

                </tr>

                <tr>

                    <td>Post Date</td>

                    <td><?= $date ?></td>

                </tr>

                <tr>

                    <td>Employment Notification</td>

                    <td>

                        <a href="<?= $url ?>" target="_blank" rel="nofollow">

                            Job Details

                        </a>

                    </td>

                </tr>

            </tbody>

        </table>


    </div>


    <?php

}


fclose($lock);


?>


</div>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.