承接 kielabokkie/uber-cache 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kielabokkie/uber-cache

Composer 安装命令:

composer require kielabokkie/uber-cache

包简介

Retrieve & store cache with a fallback for a specific duration

README 文档

README

Author Packagist Version Software License

UberCache for Laravel works very similar to the Retrieve & Store cache functionality of Laravel. The difference is that once Laravel's cache expires, and the retrieval of new data failed, you are left with no data at all. This is where UberCache comes in, as it allows you to reuse your old cache in case retrieving of new data failed.

Sponsor Me

If you find this package useful, or it somehow sparks joy, please consider sponsoring me.

Requirements

  • PHP >= 7.4
  • Laravel 6.x and higher

Installation

Install the package via composer:

composer require kielabokkie/uber-cache

TLDR

// data is fetched from the external API and cached for 1 minute
$todo = UberCache::remember('key', now()->addMinute(), now()->addHour(), function () {
    return Http::get('https://jsonplaceholder.typicode.com/todos/1')->json();
});

dump($todo);

/**
{
    id: 1,
    title: "delectus aut autem",
    completed: false
}
*/

// -- 5 minutes later --

// cache is expired so should fetch from API but API is down
$todo = UberCache::remember('key', now()->addMinute(), now()->addHour(), function () {
    throw new \Exception('API error');
});

// the todo still returns the previously expired cache
dump($todo);

/**
{
    id: 1,
    title: "delectus aut autem",
    completed: false
}
*/

// -- 2 hours later --

// cache is expired and max cache time also expired but API is still down
$todo = UberCache::remember('key', now()->addMinute(), now()->addHour(), function () {
    throw new \Exception('API error');
});

// an UberCacheException is thrown

Usage

As mentioned before, UberCache provides just one function that is very similar to Laravel's remember cache function. Let's start by looking at an example of this first:

$value = Cache::remember('todos', now()->addMinute(), function () {
    return Http::get('https://jsonplaceholder.typicode.com/todos/1')->json();
});

As you can see it takes 3 parameters: the cache key, the lifetime of the value in your cache and a callback function that's responsible for retrieving the data.

Now let's look at UberCache's remember function, it takes one extra parameter before the callback called maxTtl. This is used to set the maximum time your cache is allowed to be used.

$value = UberCache::remember('todos', now()->addMinute(), now()->addHour(), function () {
    return Http::get('https://jsonplaceholder.typicode.com/todos/1')->json();
});

It's clear that it's almost identical to Laravel's remember function but thanks to the maxTtl parameter our cache is a little smarter. The example above will cache the todo that is fetched for 1 minute. If this function is called again after more than 1 minute the cache is expired, so the API call is executed again. If the API call fails for whatever reason, maybe it is down temporary, UberCache will restore the old cached value and continue as without breaking.

If the API calls keep on failing and no new data can be fetched within the time set as the maxTtl then an exception will be thrown. This is to ensure you are not working with old cache data forever without being aware of it.

kielabokkie/uber-cache 适用场景与选型建议

kielabokkie/uber-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kielabokkie/uber-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kielabokkie/uber-cache 我们能提供哪些服务?
定制开发 / 二次开发

基于 kielabokkie/uber-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-05