PHP code example of scottchiefbaker / cache_sqlite

1. Go to this page and download the library: Download scottchiefbaker/cache_sqlite 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/ */

    

scottchiefbaker / cache_sqlite example snippets




$opts = ["db_file" => "/var/tmp/mycache.sqlite"];
$cache = new \Scottchiefbaker\Cache\Sqlite($opts);


// Remove all expired entries and defragment the database
$vacuum = true;
$ok     = $cache->remove_expired_entries($vacuum);

function get_slow_data($id) {
	global $cache;

	$ckey = "item:$id";
	$data = $cache->get($ckey);

	if ($data) { return $data; }

	// Not found in cache go get the data the slow way
	$data = my_slow_data_function($id);

	// Store the data in the cache for two hours
	$ok = $cache->set($ckey, $data, time() + 7200);

	return $data;
}