current position:Home>php Cache_ Lite file cache

php Cache_ Lite file cache

2022-01-27 02:10:35 Carrot rabbit

php Official documents
githu Download address

Local cache

<?php
    header("content-type:text/html;charset=utf8");
    // introduce Cache_Lite Cache class 
    require_once('Caches/Cache/Lite.php');
    $options = array(
        'cacheDir' => '/like/',    // Set the directory to store , We must add  /.( This directory should be created by yourself )
        'lifeTime' => 32           // Cache cycle 
    );
    $Cache_Lite = new Cache_Lite($options);
    if ($data = $Cache_Lite->get('block1')) {
    
        echo($data);
        echo "woshi block1 cache";

    } else {
    
        $data = 'Data of the block 1';
        $Cache_Lite->save($data);
        echo $data." No cache ";

    }
    echo "<br>";

        // test cache Whether there is   and ( If it is )  Go back to it .
    if ($data = $Cache_Lite->get('block3')) {
    
        echo($data);
        echo "woshi block2 cache";

    } else {
    
        $data = 'Data of the block 2';
        // Save data to a cache  file .
        $Cache_Lite->save($data);
        echo $data." No cache ";
    }

    // You can delete the cache in the following ways :
    //$Cache_Lite->remove('block1');
    //$Cache_Lite->remove('block3');

?>


Page caching

<?php
    header("content-type:text/html;charset=utf8");
    // Cache file .
    require_once('Caches/Cache/Lite/Output.php');
    $options = array(
        'cacheDir' => '/like/',     // Set the directory to store , We must add  /.( This directory should be created by yourself )
        'lifeTime' => 3,             // Cache time .
);
    // Instantiate objects .
    $cache = new Cache_Lite_Output($options);
    // Test whether a cache It works   and ( If effective ) Return it to the browser .  otherwise , Thrill   Live output buffer .
    if (!($cache->start('isd_of_the_page'))) {
    
        /** *  Not found cache ! *  Until I met end() Method   All output will be buffered into the cache . *  It will automatically fetch data from the cache next time . */
        // Stop by start() Method to start the output buffer and save the output to a cache file 
        $cache->end();
    }
?>

Function cache

<?php
    header("content-type:text/html;charset=utf8");
    // Introduce method cache class .
    require_once('Caches/Cache/Lite/Function.php');
    $options = array(
        // The cache directory .
        'cacheDir' => '/like/',
        // Cache cycle .
        'lifeTime' =>15

    );
    // Method cache class .
    $cache = new Cache_Lite_Function($options);
    /** *  If there is a cache, it will directly output the cache . *  No, the output in the cache will be output to the browser and buffered to the cache . */
    $cache->call('functions', 14, 45);
    // Define a caching method .
    function functions($arg1, $arg2)
    {
    
        // Output content .
        echo "This is the output of the function_to_bench($arg1, $arg2) !";

    }

    /** *  Clear the cache of functions . Not recommended for use , High level of error reporting . *  And you also need to pass the old value , Pass error direct operation . *  Removal can be used . */
    //$cache->drop('functions', 14 ,45);

?>


Clean the cache :

<?php
    header("content-type:text/html;charset=utf8");
    // introduce Cache_Lite Cache class 
    require_once('Caches/Cache/Lite.php');
    $options = array(
        'cacheDir' => '/like/',
        'lifeTime' => 32
    );
    $Cache_Lite = new Cache_Lite($options);
    /** *  Clean the cache . return  boor  value  */
    $Cache_Lite->clean();
?>

copyright notice
author[Carrot rabbit],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270210332307.html

Random recommended