定制 ascetik/cacheable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ascetik/cacheable

Composer 安装命令:

composer require ascetik/cacheable

包简介

Convert Closures and objects into strings

README 文档

README

Convert Closures and objects to strings in order to provide callable caching

Release notes

v1.0.0 :

  • Breaking change : CacheableCall now extends CallableType from ascetik/callapsule package. Methods have been updated to match with required behavior.
  • CacheableCall decorates a CallableType.

Purpose

Each time a client switches the page or submits some data, the server has to build whole system, with services, routes, middlewares... I wondered if it was possible to store some instances or functions in cache for faster system build. The main problem was the serialization of a Closure, which may be used by a router or a service container.

A short time was necessary to find the opis/closure package. This was the base to find out how to resolve my problem. This package may be still incomplete. I have to test it in a production context to get some feedback with some logs. Self-made logger package is still not done.

Vocabulary

Cacheable is the main interface. It just extends Serializable interface and nothing else for now. CacheableCall abstracts the idea of a wrapper able to serialize any callable. CacheableInstance extends an instance to be fully serializable. CacheableProperty holds the name and the value of each CacheableInstance wrapped instance.

Descriptions & Exposed Methods

Notice : The use of serialize and unserialize methods is not recommended and will certainly lead to serialization inconsistencies. Use native serialize/unserialize functions instead.

CacheableFactory (not Cacheable itself)

  • wrapCall(callable): CacheableCall : Returns a CacheableCall instance
  • wrapInstance(object): CacheableInstance : Returns a CacheableInstance instance
  • wrapProperty(string, mixed): CacheableProperty : Returns a CacheableProperty

CacheableCall

  • action(): callable : Returns registered callable as is
  • apply(iterable): mixed : Calls registered callable

CacheableCall is invokable to keep it simple to use on runtime

CacheableInstance

  • getClass(): string : Returns registered class name
  • getProperties(): Ds\Set : Returns a container of CacheableProperties built from registered instance properties
  • getInstance(): object : Returns registered instance

CacheableProperty

  • getName(): string : Returns property name
  • getValue(): mixed : Returns property value

Usage

Callable Wrap

The CacheableFactory provides methods to build Cacheable instances for callable types. There are three sorts of wrapper to serialize a callable :

// Closure use case
$func = function(string $name): string {
    return 'Closure : hello '.$name;
}

// class method use case
class Greeter
{
    public function greet($name): string
    {
        return 'Method : hello ' . $name;
    }
}

// invokable class use case
class InvokableGreeter
{
    public function __invoke(string $name): string
    {
        return 'Invkable : hello ' . $name;
    }
}

$closureWrapper = CacheableFactory::wrapCall($func); // returns a CacheableClosure instance
$methodWrapper = CacheableFactory::wrapCall(new Greeter()); // returns a CacheableMethod instance
$invokableWrapper = CacheableFactory::wrapCall(new InvokableGreeter()); // returns a CacheableInvokable instance

As they all are Cacheable, so Serializable, serialization is easy :

$serial = serialize($closureWrapper);

And unserialization is easy too :

$serial = goAndFindInCache('something-satisfying-my-needs'); // file, external service, SQL, noSQL, hyper-speed keyboard typing world champion...
$wrapper = unserialize($serial);
echo $wrapper->apply(['John']); // prints 'Closure : hello John'
// A CacheableCall is invokable
echo $wrapper(['John']); // same result

Instances Wrap

$instance = new MyInstance();
$wrapper = CacheableFactory::wrapInstance($instance);
$serial = serialize($wrapper);

// get wrapper back
$extractedWrapper = unserialize($serial);
$extractedInstance = $extractedWrapper->getInstance();
$extractedInstance->doWhatIsExpected();
// Or use the extracted wrapper directly
$extractedWrapper->doWhatIsExpected();
$property = $extractedWrapper->someProperty();

When using wrapper magic methods, Exceptions are thrown if :

  • called method does not exist
  • property does not exist
  • property is public

Finalities

This package provides serialization and unserialization. The usage of the output is up to you.

ascetik/cacheable 适用场景与选型建议

ascetik/cacheable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 ascetik/cacheable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-07