1. Go to this page and download the library: Download hejunjie/tools 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/ */
hejunjie / tools example snippets
use Hejunjie\Tools\Str;
// 示例:获取随机姓氏
$result = Str::getRandomSurname();
// 自定义数据源 - 数据库层
class UserDataSource implements \Hejunjie\Tools\Cache\Interfaces\DataSourceInterface
{
protected DataSourceInterface $wrapped;
// 构造函数,如果是最后一层则不需要构造函数
// public function __construct(
// DataSourceInterface $wrapped
// ) {
// $this->wrapped = $wrapped;
// }
public function get(string $key): ?string
{
// 根据 key 在数据库中获取对应内容
// 返回内容字符串 `string`
// 如果下一层返回数据,则在当前层存储。如果是最后一层则不需要下列代码
// $content = $this->wrapped->get($key);
// if ($content !== null) {
// $this->set($key, $content);
// }
// return $content;
}
public function set(string $key, string $value): bool
{
// 根据 key 在数据库中存储 value
// 返回存储结果 `bool`
}
public function del(string $key, string $value): void
{
// 根据 key 在进行删除操作
// 不需要进行返回
}
}
use Hejunjie\Tools\Cache\Decorators;
// 构建缓存链:内存 → Redis → 文件 → 自定义数据源
$cache = new Decorators\MemoryCache(
new Decorators\RedisCache(
new Decorators\FileCache(
new UserDataSource(
... // 可以继续套娃
),
'[文件]缓存文件夹路径',
'[文件]缓存时长(秒)'
),
'[redis]配置'
'[redis]前缀'
'[redis]是否持久化链接'
),
'[内存]缓存时长(秒)',
'[内存]缓存数量(防止内存溢出)'
);
// 获取数据
$data = $cache->get('key')
// 存储数据
$data = $cache->set('key','value')
$log = new \Hejunjie\Tools\Log\Logger([
new \Hejunjie\Tools\Log\Handlers\ConsoleHandler(), // 打印到控制台
new \Hejunjie\Tools\Log\Handlers\FileHandler('日志存储文件夹路径'), // 存储到文件
new \Hejunjie\Tools\Log\Handlers\RemoteApiHandler('请求url') // 发送到某个地址
]);
$log->info('标题','内容',['上下文']); // INFO 级
$log->warning('标题','内容',['上下文']); // WARNING 级
$log->error('标题','内容',['上下文']); // ERROR 级
$log->log('自定义级别','标题','内容',['上下文']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.