stefangabos/zebra_cache

A file-based lightweight PHP caching library that uses file locking to ensure proper functionality under heavy load

1.3.2 2023-01-03 21:59 UTC

This package is auto-updated.

Last update: 2024-03-30 00:31:11 UTC


README

zebra-curl-logo

Zebra Cache  Tweet

A file-based lightweight PHP caching library that uses file locking to ensure proper functionality under heavy load.

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

Features

  • uses file locking to ensure proper functionality under heavy load - meaning that in a concurent environment, writing of cache file will acquire an exclusive lock on the file and reads will wait for the writing to finish before fetching the cached data
  • automatic serialization and compression of cache data to save space and improve performance
  • automatic expiration of cache items based on a specified time-to-live (TTL) value
  • support for multiple instances, allowing you to use different cache configurations for different parts of your application

📔 Documentation

Check out the awesome documentation!

🎂 Support the development of this project

Your support is greatly appreciated and it keeps me motivated continue working on open source projects. If you enjoy this project please star it by clicking on the star button at the top of the page. If you're feeling generous, you can also buy me a coffee through PayPal or become a sponsor. Thank you for your support! 🎉

Star it on GitHub Donate 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d53706f6e736f722d6661666266633f6c6f676f3d47697448756225323053706f6e736f7273

Requirements

PHP 5.3.0+

Installation

You can install via Composer

# get the latest stable release
composer require stefangabos/zebra_cache

# get the latest commit
composer require stefangabos/zebra_cache:dev-master

Or you can install it manually by downloading the latest version, unpacking it, and then including it in your project

require_once 'path/to/Zebra_Cache.php';

How to use

// instantiate the library
$cache = new Zebra_Cache('path/to/store/cache-files/');

// if a cached, non-expired value for the sought key does not exist
if (!($some_data = $cache->get('my-key'))) {

    // do whatever you need to retrieve data
    $some_data = 'get this data';

    // cache the values for one hour (3600 seconds)
    $cache->set('my-key', $some_data, 3600);

}

// $some_data now holds the values, either fresh or from cache

Getting information about cached data

if ($cached_info = $cache->has('my-key')) {

    // will output something in the  likes of
    //  Array (
    //    'path'     => '',  //  path to the cache file
    //    'timeout'  => '',  //  the number of seconds the cache was supposed to be valid
    //    'ttl'      => '',  //  number of seconds remaining until the cache expires
    //  )

    print_r('<pre>');
    print_r($cached_info);

}

Deleting cached data

$cache->delete('my-key');