PHP Cache Duration

Overview

PHP Cache Duration

Banner

Introduction

A readable and fluent way to generate PHP cache time.

Built and written by Ajimoti Ibukun

Quick Samples

Instead of this:

$cacheDuration = 25 * 60 * 60; // twenty five hours
Redis::expire($userData, $cacheDuration);

You can do this

$cacheDuration = Duration::twentyFiveHours(); // returns 25 hours in seconds (25 * 60 * 60)
Redis::expire($userData, $cacheDuration);

// or
$cacheDuration = Duration::hours(25); // returns 25 hours in seconds (25 * 60 * 60)
Redis::expire($userData, $cacheDuration);

You can also do this:

$cacheDuration = Duration::at('first day of January 2023'); // returns the time difference between the present time and the first of january 2023 in seconds

Redis::expire($userData, $cacheDuration);

Requirements

  • PHP 8.0 or higher

Installation

You can install the package via composer:

composer require ajimoti/cache-duration --with-all-dependencies

Documentation

After installing the package via composer, import the Duration trait inside your class, then you are set.


require 'vendor/autoload.php';

use Ajimoti\CacheDuration\Duration;

var_dump(Duration::fourtyMinutes()); // returns 2400;
var_dump(Duration::tenHours()); // returns 36000;
var_dump(Duration::fiftyFourDays()); // returns 4665600;

Available methods

Method Expectations
seconds($value) Expects time in seconds
minutes($value) Expects time in minutes
hours($value) Expects time in hours
days($value) Expects time in days
at($value) Expects string, carbon instance, or DateTime instance

Dynamic calls

In addition to the methods provided above, the package uses PHP __callStatic() method to allow you make dynamic calls on the Duration trait.

For example, you want to get the number of seconds in 37 days, you can achieve this by calling a camel-case text of the number (thirtySeven in this case), plus the unit (Days in this case). That will leave us with something like this:

// The formula = camelCaseOfTheNumberInWords + Unit
Duration::thirtySevenDays(); // returns the number of seconds in 37 days

Note: The number in words MUST be in camel-case. Any other case will throw an InvalidArgumentException. Additionally, it must be followed by a title-case of the unit. The available units are Seconds, Minutes, Hours, and Days.

Usage

seconds($value)

Get time in seconds. It basically returns the same value passed into it.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::seconds(30); // returns 30

// or dynamically
$cacheDuration = Duration::thirtySeconds(); // returns 30

minutes($value)

Converts time in minutes into seconds.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::minutes(55); // returns 55 minutes in seconds (55 * 60)

// or dynamically
$cacheDuration = Duration::fiftyFiveMinutes(); // returns 55 minutes in seconds (55 * 60)

hours($value)

Converts time in hours into seconds.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::hours(7); // returns 7 hours in seconds (7 * 60 * 60)

// or dynamically
$cacheDuration = Duration::sevenHours(); // returns 7 hours in seconds (7 * 60 * 60)

days($value)

Converts time in days into seconds.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::days(22); // returns 22 days in seconds (22 * 24 * 60 * 60)

// or dynamically
$cacheDuration = Duration::twentyTwoDays(); // returns 22 days in seconds (22 * 24 * 60 * 60)

at($value)

This method allows you to convert a Carbon\Carbon instance, DateTime instance or string of date into seconds.

It returns the difference in seconds between the argument passed and the current timestamp.

The date passed into this method MUST be a date in the future. When a string is passed, the text MUST be compatible with Carbon::parse() method, else an exception will be thrown

Examples

use Date;
use Carbon\Carbon;
use Ajimoti\CacheDuration\Duration;

// Carbon instance
$cacheDuration = Duration::at(Carbon::now()->addMonths(3)); // returns time in seconds between the present timestamp and three months time

// Datetime instance
$cacheDuration = Duration::at(new DateTime('2039-09-30')); // returns time in seconds between the present timestamp and the date passed (2039-09-30).

// String
$cacheDuration = Duration::at('first day of January 2023'); // returns time in seconds between the present timestamp and the first of January 2023.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
Releases(1.1.1)
Owner
null
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

Donate/Support: Documentation: https://www.scrapbook.cash - API reference: https://docs.scrapbook.cash Table of contents Installation & usage Adapters

Matthias Mullie 295 Nov 28, 2022
:zap: Simple Cache Abstraction Layer for PHP

⚡ Simple Cache Class This is a simple Cache Abstraction Layer for PHP >= 7.0 that provides a simple interaction with your cache-server. You can define

Lars Moelleken 27 Dec 8, 2022
LRU Cache implementation in PHP

PHP LRU Cache implementation Intro WTF is a LRU Cache? LRU stands for Least Recently Used. It's a type of cache that usually has a fixed capacity and

Rogério Vicente 61 Jun 23, 2022
Elephant - a highly performant PHP Cache Driver for Kirby 3

?? Kirby3 PHP Cache-Driver Elephant - a highly performant PHP Cache Driver for Kirby 3 Commerical Usage Support open source! This plugin is free but i

Bruno Meilick 11 Apr 6, 2022
PHP local cache

__ ____ _________ ______/ /_ ___ / __ \/ ___/ __ `/ ___/ __ \/ _ \ / /_/ / /__/ /_/ / /__/ / / / __/ / ._

Jayden Lie 48 Sep 9, 2022
A fast, lock-free, shared memory user data cache for PHP

Yac is a shared and lockless memory user data cache for PHP.

Xinchen Hui 815 Dec 18, 2022
Distributed PSR-16 cache implementation for PHP 6 that uses browser cookies to store data

cookiecache Cache beyond the edge with cookies! This library provides a distributed PSR-16 compatible cache implementation for PHP 6 that uses browser

Colin O'Dell 8 Apr 19, 2022
Simple artisan command to debug your redis cache. Requires PHP 8.1 & Laravel 9

?? php artisan cache:debug Simple artisan command to debug your redis cache ?? Installation You can install the package via composer: composer require

Juan Pablo Barreto 19 Sep 18, 2022
PHP cache implementation supporting memcached

php cache implementation (PSR-6 and PSR-16) Support for memcached and APCu is included. Memcached $memcached = new Memcached(); $memcached->addServer(

Michael Bretterklieber 1 Aug 11, 2022
The place to keep your cache.

Stash - A PHP Caching Library Stash makes it easy to speed up your code by caching the results of expensive functions or code. Certain actions, like d

Tedious Developments 944 Jan 4, 2023
Cache slam defense using a semaphore to prevent dogpile effect.

metaphore PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampending herd or Slashdot effect). Problem: t

Przemek Sobstel 102 Sep 28, 2022
Doctrine Cache component

Doctrine Cache Cache component extracted from the Doctrine Common project. Documentation This library is deprecated and will no longer receive bug fix

Doctrine 7.6k Jan 3, 2023
Simple cache abstraction layer implementing PSR-16

sabre/cache This repository is a simple abstraction layer for key-value caches. It implements PSR-16. If you need a super-simple way to support PSR-16

sabre.io 48 Sep 9, 2022
PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

null 1 Oct 15, 2021
More Than Just a Cache: Redis Data Structures

More Than Just a Cache: Redis Data Structures Redis is a popular key-value store, commonly used as a cache or message broker service. However, it can

Andy Snell 2 Oct 16, 2021
Simple cache

Simple cache

Róbert Kelčák 3 Dec 17, 2022
An improved helper for working with cache

Laravel Cache Installation To get the latest version of Laravel Cache, simply require the project using Composer: $ composer require dragon-code/larav

The Dragon Code 64 Sep 23, 2022
Zend Framework cache backend for MongoDB

Zend_Cache_Backend_Mongo Author: Anton Stöckl About Zend_Cache_Backend_Mongo is a Zend Framework Cache Backend for MongoDB. It supports tags and autoc

Anton Stöckl 12 Feb 19, 2020