ftembe/cachehandler

该项目是一个轻量级的缓存解决方案,通过高效存储和检索数据来提高性能。它提供了一种基于文件的存储方法,具有可自定义的过期时间,自动删除过期的数据,并确保缓存文件夹可访问和可写。集成此缓存机制可以提升应用程序的速度并减少处理时间。

v1 2023-07-04 14:09 UTC

This package is auto-updated.

Last update: 2024-09-04 17:05:27 UTC


README

该项目是一个轻量级的缓存解决方案,通过高效存储和检索数据来提高性能。它提供了一种基于文件的存储方法,具有可自定义的过期时间,自动删除过期的数据,并确保缓存文件夹可访问和可写。集成此缓存机制可以提升应用程序的速度并减少处理时间。

安装

composer require ftembe/cachehandler

或者

git clone https://github.com/FTembe/Cachehandler.git

如何使用

<?php

use Ftembe\Cachehandler\Cachehandler;

require 'vendor/autoload.php';

function fetchDataFromApi($endPoint =  'https://jsonplaceholder.typicode.com/photos/')
{

   $curl = curl_init();

   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($curl, CURLOPT_URL, $endPoint);
   curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);

   $response = curl_exec($curl);

   if ($e = curl_error($curl))
       echo $e;

   curl_close($curl);

   return $response;
}

/*
You can create a folder to receive temporary data and pass it as a parameter when instantiating the CacheHandler class
eg. $cacheHandler = new Cachehandler('cacheFolder');
*/

$cacheHandler = new Cachehandler();

if ($cacheHandler->getDataLength('photos') != 5000){
   $store = $cacheHandler->store('photos', json_decode(fetchDataFromApi()), '1 hour');
}

$getDataLength = $cacheHandler->getDataLength('photos');
$getDataType = $cacheHandler->getDataType('photos');

$get = $cacheHandler->get('photos');


var_dump($get);
var_dump($getDataLength);
var_dump($getDataType);