定制 apricity/micro-cache 二次开发

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

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

apricity/micro-cache

Composer 安装命令:

composer require apricity/micro-cache

包简介

Simple in-memory caching mechanism for PHP applications.

README 文档

README

Latest Stable Version PHP Version Require License

MicroCache is a lightweight and simple in-memory caching solution that allows you to store key-value pairs in memory. It is particularly useful for caching data that is expensive to generate or retrieve from external sources.

Class provides a straightforward approach to caching data in memory, allowing for efficient storage and retrieval of cached items.

Installation

composer require apricity/micro-cache

Table of contents

  1. Usage
  2. Tests
  3. Contributing
  4. Changelog
  5. License

MicroCache::set

Store an item in the cache.

This method stores an item in the cache under the specified key. If the key already exists, its value will be overwritten with the new value. If the TTL (time-to-live) parameter is provided and is greater than zero, the item will expire and be removed from the cache after the specified number of seconds. If the TTL is zero, the item will be cached indefinitely.

public static function set(mixed $key, mixed $value, int $ttl = 0): string|int

Parameters:

  • mixed $key: The key under which to store the item. Can be of any type except Closure.
  • mixed $value: The value to store.
  • int $ttl: Time-to-live in seconds. If zero, the item is cached indefinitely.

Returns: string|int - The key under which the item is stored.

Throws: InvalidArgumentException - If the key is callable or if the key is empty and not zero or false.

Example

$key = MicroCache::set('my_key', 'my_value', 3600);

MicroCache::get

Retrieve an item value from the cache.

This method retrieves the value of an item stored in the cache under the specified key. If the item exists in the cache and has not expired, its value will be returned. If the item does not exist or has expired, null will be returned.

public static function get(mixed $key): mixed

Parameters:

  • mixed $key: The key under which to store the item. Can be of any type except Closure.

Returns: mixed|null - The cached item value or null if the key doesn't exist or has expired.

Throws: InvalidArgumentException - If the key is callable or if the key is empty and not zero or false.

Example

$value = MicroCache::get('my_key');
if (is_null($value)) {
    echo "Cache miss";
} else {
    echo "Cache value: " . $value;
}

MicroCache::delete

Delete an item from the cache.

This method removes the item stored in the cache under the specified key. If the item exists in the cache, it will be deleted. If the item does not exist, no action will be taken.

public static function delete(mixed $key): void

Parameters:

  • mixed $key: The key under which to store the item. Can be of any type except Closure.

Returns: void

Throws: InvalidArgumentException - If the key is callable or if the key is empty and not zero or false.

Example

MicroCache::delete('my_key');

MicroCache::clear

Clear all items from the cache.

This method clears all items stored in the cache, effectively resetting the cache to an empty state.

public static function clear(): void

Returns: void

Example

MicroCache::clear();

MicroCache::has

Check if an item exists in the cache and is not expired.

This method checks whether the specified key exists in the cache and if it is not expired. If the item exists and is not expired, it returns true; otherwise, it returns false.

public static function has(mixed $key): bool

Parameters:

  • mixed $key: The key under which to store the item. Can be of any type except Closure.

Returns: bool - True if the item exists and is not expired, false otherwise.

Throws: InvalidArgumentException - If the key is callable or if the key is empty and not zero or false.

Example

if (MicroCache::has('my_key')) {
    echo "Cache exists";
} else {
    echo "Cache does not exist or has expired";
}

Inheritance and creation of a unique cache

Shared cache

use Apricity\MicroCache;

class SharedCache extends MicroCache {}

SharedCache::set('shared_cache_key', 'shared_value');

SharedCache::get('shared_cache_key'); // shared_value
MicroCache::get('shared_cache_key'); // shared_value

MicroCache::set('cache_key', 'cached_value');

SharedCache::get('cache_key'); // cached_value
MicroCache::get('cache_key'); // cached_value

Unique cache

use Apricity\MicroCache;

class UniqueCache extends MicroCache {
    protected static array $microCache = []; // Non-shared unique cache.
}

UniqueCache::set('unique_cache_key', 'unique_value');

UniqueCache::get('unique_cache_key'); // unique_value
MicroCache::get('unique_cache_key'); // null

MicroCache::set('cache_key', 'cached_value');

UniqueCache::get('cache_key'); // null
MicroCache::get('cache_key'); // cached_value

Run tests

composer test

Contributing

We welcome contributions from the community! For guidelines on how to contribute, please refer to the CONTRIBUTING.md file.

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

The repository has been migrated from GitLab.

apricity/micro-cache 适用场景与选型建议

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

它主要适用于以下技术方向: 「performance」 「php」 「cache」 「caching」 「memory」 「in-memory」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-06-07