PHP code example of lovecoding / content-cache

1. Go to this page and download the library: Download lovecoding/content-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lovecoding / content-cache example snippets




= new Slim\App();

$container = $app->getContainer();

$container['cacheService'] = function() {
    // path to folder contains cached
    $cacheProvider = new \LoveCoding\ContentCache\CacheProvider('storage/cache');
    
    return $cacheProvider;
};

$app->get('/cache/array', function ($request, $response, $args) use($container) {
    $cacheService = $container->get('cacheService');

    // $cacheService->cache return a json
    $contentArrayCache = $cacheService->cacheArray($request, function() {

        // This function will run when $content is null on server
        // TODO something

        // Must return an array
        return ...;
    });
    
    return $response->withJson(json_encode($contentArrayCache));
});

$app->get('/cache/plaintext', function ($request, $response, $args) use($container) {
    $cacheService = $container->get('cacheService');

    $contentCache = $cacheService->cache($request, function() {
        // This function will run when $content is null on server
        // TODO something

        // return and save something you want on server
        return ...;
    });

    return $response->getBody()->write($contentCache);
});

// Using salt for many content
$app->get('/cache/plaintext', function ($request, $response, $args) use($container) {
    $cacheService = $container->get('cacheService');

    $firstList = $cacheService->salt('salt_for_firstList')->cacheArray($request, function() {
        // This function will run when $content is null on server
        // TODO something

        // return and save something you want on server
        return ...;
    });

    $secondList = $cacheService->salt('salt_for_secondList')->cacheArray($request, function() {
        // This function will run when $content is null on server
        // TODO something

        // return and save something you want on server
        return ...;
    });

    return $response->withJson(json_encode([$firstList, $secondList]));
});

$app->run();