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-pdowith 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)
- Config:
- Memcached: Uses a Memcached server for distributed caching.
- Config:
servers(array of [host, port]),prefix(key prefix)
- Config:
- Redis: Uses a Redis server for high-performance caching.
- Config:
host,port,prefix,database,password,timeout
- Config:
- Database: Stores cache in an SQL database (e.g., MySQL, PostgreSQL).
- Config:
dsn,username,password,table
- Config:
- Mock: In-memory driver for testing, does not persist data.
- Config:
emulate_failures(bool),log_operations(bool)
- Config:
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sanovskiy/simple-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
repository php library
Microservice RPC through message queues.
Store your language lines in the database, yaml or other sources
The CodeIgniter Redis package
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-21