ajimoti / cache-duration
该软件包提供了一种更易读的方式来获取缓存时间的秒数。
Requires
- php: ^8.0
- nesbot/carbon: ^2.55
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pestphp/pest: ^1.20
- spatie/ray: ^1.28
This package is auto-updated.
Last update: 2024-09-10 02:03:19 UTC
README
简介
生成PHP缓存时间的易读和流畅方式。
由Ajimoti Ibukun构建和编写
快速示例
而不是这个
$cacheDuration = 25 * 60 * 60; // twenty five hours Redis::expire($userData, $cacheDuration);
你可以这样做
$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);
你也可以这样做
$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);
要求
- PHP 8.0或更高版本
安装
您可以通过composer安装此软件包
composer require ajimoti/cache-duration --with-all-dependencies
文档
通过composer安装软件包后,将Duration
特质导入到您的类中,然后即可使用。
<?php 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;
可用方法
动态调用
除了上述方法外,该软件包还使用PHP
__callStatic()
方法来允许您对Duration
特质进行动态调用。
例如,您想得到37天的秒数,您可以通过调用数字的驼峰文本(在这种情况下为thirtySeven
),加上单位(在这种情况下为Days
)来实现这一点。这样我们就会得到类似的东西
// The formula = camelCaseOfTheNumberInWords + Unit Duration::thirtySevenDays(); // returns the number of seconds in 37 days
注意:数字的文字必须使用
camel-case
。任何其他情况都会抛出InvalidArgumentException
。此外,它必须跟在单位的标题化形式后面。可用的单位是Seconds
、Minutes
、Hours
和Days
。
用法
seconds($value)
获取时间(以秒为单位)。它基本上返回传递给它相同的值。
use Ajimoti\CacheDuration\Duration; $cacheDuration = Duration::seconds(30); // returns 30 // or dynamically $cacheDuration = Duration::thirtySeconds(); // returns 30
minutes($value)
将时间(以分钟为单位)转换为秒。
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)
将时间(以小时为单位)转换为秒。
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)
将时间(以天为单位)转换为秒。
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)
此方法允许您将Carbon\Carbon
实例、DateTime
实例或日期字符串转换为秒。
它返回传递的参数和当前timestamp
之间的秒数差。
传递到此方法中的日期必须是未来的日期。<当传递字符串时,文本必须与
Carbon::parse()
方法兼容,否则将抛出异常。
示例
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.
测试
composer test
更新日志
请参阅更新日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请参阅我们的安全策略了解如何报告安全漏洞。
鸣谢
许可协议
MIT许可(MIT)。请参阅许可文件以获取更多信息。