sanovskiy/simple-cache 问题修复 & 功能扩展

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

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

sanovskiy/simple-cache

Composer 安装命令:

composer require sanovskiy/simple-cache

包简介

A simple and transparent caching library for PHP with multiple driver support

README 文档

README

A simple and transparent caching library for PHP with multiple driver support.

Features

  • Lightweight and easy-to-use API
  • Multiple drivers: File, Memcached, Redis, Database, Mock
  • Built-in support for the "remember" pattern
  • Mock driver for testing

Installation

composer require sanovskiy/simple-cache

Requirements

  • PHP 8.0 or higher
  • For File driver: No additional requirements
  • For Memcached driver: ext-memcached (version >= 3.0 recommended)
  • For Redis driver: ext-redis (version >= 5.0 recommended)
  • For Database driver: ext-pdo with appropriate database driver (e.g., ext-pdo-mysql)
  • For Mock driver: No additional requirements

Configuration

Files

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\FileCacheDriver;

$cache = new Cache([
    'driver' => FileCacheDriver::class,
    'config' => [
        'directory' => '/var/cache/',
        'file_extension' => '.cache'
    ]
]);

Memcached

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\MemcachedCacheDriver;
$cache = new Cache([
    'driver' => MemcachedCacheDriver::class,
    'config' => [
        'servers' => [['127.0.0.1', 11211]],
        'prefix' => 'app_'
    ]
]);

Redis

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\RedisCacheDriver;
$cache = new Cache([
    'driver' => RedisCacheDriver::class,
    'config' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'prefix' => 'app_',
        'database' => 0
    ]
]);

SQL database

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\DatabaseCacheDriver;
$cache = new Cache([
    'driver' => DatabaseCacheDriver::class,
    'config' => [
        'dsn' => 'mysql:host=localhost;dbname=cache_db',
        'username' => 'user',
        'password' => 'pass',
        'table' => 'cache'
    ]
]);

Note: The DatabaseCacheDriver automatically creates cache table with columns key, value, expires_at, created_at, and updated_at (with an index on expires_at) if it doesn't exist. Ensure the database user has CREATE TABLE and INDEX permissions.

Mock cache for testing

$cache = new Cache([
    'driver' => \Sanovskiy\SimpleCache\Drivers\MockCacheDriver::class,
    'config' => [
        'emulate_failures' => false, // If true will emulate failure at every operation
        'log_operations' => true
    ]
]);

Usage

Basic usage

$cache->set('key', 'value', 3600);
$value = $cache->get('key', 'default');
$has = $cache->has('key');
$cache->delete('key');
$cache->clear();

Using the "remember" pattern:

$data = $cache->remember('expensive_data', function() {
    return expensiveOperation();
}, 1800);

Drivers

Simple Cache supports the following drivers:

  • File: Stores cache in files on the filesystem.
    • Config: directory (path to cache directory), file_extension (e.g., .cache)
  • Memcached: Uses a Memcached server for distributed caching.
    • Config: servers (array of [host, port]), prefix (key prefix)
  • Redis: Uses a Redis server for high-performance caching.
    • Config: host, port, prefix, database, password, timeout
  • Database: Stores cache in an SQL database (e.g., MySQL, PostgreSQL).
    • Config: dsn, username, password, table
  • Mock: In-memory driver for testing, does not persist data.
    • Config: emulate_failures (bool), log_operations (bool)

Testing

To run tests locally, ensure Memcached, Redis, and MySQL are running and PHP extensions (ext-memcached, ext-redis, ext-pdo_mysql) are installed. Then run:

composer install
./vendor/bin/phpunit tests/

To run the tests using Docker Compose:

docker-compose up --build

To generate a code coverage report:

docker-compose run php sh -c "composer install && ./vendor/bin/phpunit tests/ --coverage-html coverage"

Why Simple Cache?

Simple Cache is designed to be a lightweight, transparent, and easy-to-use caching library for PHP. Unlike other caching libraries that come with complex configurations or heavy dependencies, Simple Cache focuses on:

  • Simplicity: Intuitive API with minimal setup.
  • Flexibility: Supports multiple drivers (File, Memcached, Redis, Database, Mock) out of the box.
  • Testability: Includes a Mock driver for easy unit testing.
  • Lightweight: Minimal dependencies for better performance in small to medium projects.

Roadmap

  • Add support for PSR-6 (Caching Interface) in future versions
  • Add support for additional drivers (e.g., APCu)

Contributing

Found a bug or have an idea? Open an issue or submit a pull request on GitHub.

License

This library is licensed under the MIT License

sanovskiy/simple-cache 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-21