bit-mx/cache-entities 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

bit-mx/cache-entities

Composer 安装命令:

composer require bit-mx/cache-entities

包简介

Creates cacheable entities

README 文档

README

Manage your cache easily with Cache Entities.

Table of Contents

Introduction

Cache Entities is a package that allows you to manage your cache using a simple and clean API.

Installation

You can install the package via composer:

composer require bit-mx/cache-entities

Compatibility

This package is compatible with Laravel 10.x and above.

Due laravel 11 requires php 8.2, this package is compatible with php 8.2 and above.

Getting Started

Create a Cache Entity class

To create a Cache Entity, you need to extend the CacheEntity class and implement the resolveKey, resolveTtl, and resolveValue methods.

namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    public function __construct(
        protected int $id,
    )
    {
    }

    protected function resolveKey(): string
    {
        return sprintf('current_user:%s', $this->id);
    }

    protected function resolveTtl(): CarbonInterval
    {
        return CarbonInterval::days(12);
    }

    protected function resolveValue(): mixed
    {
        return User::find($this->id);
    }
}

You can use the artisan command to create a new Cache Entity:

php artisan make:cache-entity CurrentUserCache

This command will create a new Cache Entity in the app/cacheEntities directory.

Driver

You can set the driver name overriding the resolveCacheStore method.

namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    
    protected function resolveCacheStore(): string
    {
        return 'redis';
    }
}

Get the cached value

To get the cached value, you can use the get method.

use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

Helper methods

You can use the following helper methods to work with the cache:

exists

The exists method returns true if the cache key exists, and false otherwise.

use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

if ($cacheEntity->exists()) {
    // The cache key exists
} else {
    // The cache key does not exist
}

doesNotExist

The doesNotExist method returns true if the cache key does not exist, and false otherwise.

use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

if ($cacheEntity->doesNotExist()) {
    // The cache key does not exist
} else {
    // The cache key exists
}

forget

The forget method removes the cache key.

use App\CacheEntities\CurrentUserCache;

$cacheEntity = new CurrentUserCache(1);

$user = $cacheEntity->get();

$cacheEntity->forget();

Memoization

By default, Cache Entities do not use memoization. However, you can enable it by using the HasMemoization trait in your Cache Entity class.

When memoization is enabled, the cache entity will use Laravel's memo() method to store the cached value in memory during the request lifecycle. This prevents redundant cache lookups for the same key within a single request, improving performance.

Enabling Memoization

To enable memoization, simply add the HasMemoization trait to your Cache Entity class:

namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use BitMx\CacheEntities\Traits\HasMemoization;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    use HasMemoization;

    public function __construct(
        protected int $id,
    )
    {
    }

    protected function resolveKey(): string
    {
        return sprintf('current_user:%s', $this->id);
    }

    protected function resolveTtl(): CarbonInterval
    {
        return CarbonInterval::days(12);
    }

    protected function resolveValue(): mixed
    {
        return User::find($this->id);
    }
}

With memoization enabled:

  • The first call to get() will fetch the value from cache or execute resolveValue()
  • Subsequent calls to get() within the same request will return the memoized value from memory
  • This avoids redundant cache lookups, improving performance when accessing the same cache entity multiple times

Without Memoization

If you don't use the HasMemoization trait, each call to get() will perform a cache lookup, even within the same request.

bit-mx/cache-entities 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-02