vaibhavpandeyvpz/godam
Composer 安装命令:
composer require vaibhavpandeyvpz/godam
包简介
A modern, PSR-6 and PSR-16 compliant caching library for PHP 8.2+ with support for multiple storage backends.
README 文档
README
A modern, PSR-6 and PSR-16 compliant caching library for PHP 8.2+ with support for multiple storage backends.
Godam (
गोदाम) means "Warehouse" in Hindi
Features
- ✅ PSR-6 (Cache Item Pool Interface) compliant
- ✅ PSR-16 (Simple Cache Interface) compliant
- ✅ Multiple storage backends: Memory, FileSystem, Redis, Memcache, Predis
- ✅ TTL (Time To Live) support with flexible expiration
- ✅ Type-safe with PHP 8.2+ features
- ✅ 100% test coverage
- ✅ Zero dependencies (except PSR interfaces)
Installation
Install via Composer:
composer require vaibhavpandeyvpz/godam
Optional Dependencies
For Redis support, you can use either:
ext-redis(PECL extension)predis/predis(pure PHP client)
For Memcache support:
ext-memcache(PECL extension)
Storage Backends
Godam supports multiple storage backends through the StoreInterface:
MemoryStore
In-memory cache that stores data in PHP arrays. Perfect for testing or single-request caching.
use Godam\Store\MemoryStore; $store = new MemoryStore();
FileSystemStore
File-based cache that stores data on disk. Ideal for applications without external cache servers.
use Godam\Store\FileSystemStore; $store = new FileSystemStore('/path/to/cache/directory');
RedisStore
Redis cache using the ext-redis extension.
use Godam\Store\RedisStore; $redis = new Redis(); $redis->connect('localhost', 6379); $store = new RedisStore($redis);
PredisStore
Redis cache using the predis/predis library (pure PHP, no extension required).
use Godam\Store\PredisStore; use Predis\Client; $redis = new Client('tcp://127.0.0.1:6379'); $store = new PredisStore($redis);
MemcacheStore
Memcache cache using the ext-memcache extension.
use Godam\Store\MemcacheStore; $memcache = new Memcache(); $memcache->connect('localhost', 11211); $store = new MemcacheStore($memcache);
Usage
PSR-16 Simple Cache Interface
The Cache class provides a simple, straightforward caching interface:
use Godam\Cache; use Godam\Store\MemoryStore; $cache = new Cache(new MemoryStore()); // Store a value with TTL (time to live in seconds) $cache->set('user:123', ['name' => 'John', 'email' => 'john@example.com'], 3600); // Retrieve a value $user = $cache->get('user:123'); // Get with default value if key doesn't exist $user = $cache->get('user:456', ['name' => 'Guest']); // Check if a key exists $exists = $cache->has('user:123'); // Delete a single key $cache->delete('user:123'); // Delete multiple keys $cache->deleteMultiple(['user:123', 'user:456']); // Store multiple values at once $cache->setMultiple([ 'key1' => 'value1', 'key2' => 'value2', ], 3600); // Get multiple values at once $values = $cache->getMultiple(['key1', 'key2'], []); // Clear all cache $cache->clear();
PSR-6 Cache Item Pool Interface
The CacheItemPool class provides a more advanced caching interface with cache items:
use Godam\CacheItemPool; use Godam\Store\FileSystemStore; $pool = new CacheItemPool(new FileSystemStore(__DIR__ . '/cache')); // Get a cache item $item = $pool->getItem('user:123'); if ($item->isHit()) { // Cache hit - item exists and is not expired $user = $item->get(); } else { // Cache miss - set the value $item->set(['name' => 'John', 'email' => 'john@example.com']); // Set expiration time (in seconds) $item->expiresAfter(3600); // Or set expiration to a specific date/time // $item->expiresAt(new \DateTime('+1 hour')); // Save the item $pool->save($item); } // Get multiple items at once $items = $pool->getItems(['user:123', 'user:456']); // Save a deferred item (will be saved on commit) $item = $pool->getItem('user:789'); $item->set(['name' => 'Jane']); $item->expiresAfter(1800); $pool->saveDeferred($item); // Commit all deferred items $pool->commit(); // Delete an item $pool->deleteItem('user:123'); // Delete multiple items $pool->deleteItems(['user:123', 'user:456']); // Clear all items $pool->clear();
TTL (Time To Live) Examples
use Godam\Cache; use Godam\Store\MemoryStore; $cache = new Cache(new MemoryStore()); // Cache for 1 hour (3600 seconds) $cache->set('key1', 'value1', 3600); // Cache forever (no expiration) $cache->set('key2', 'value2', null); // Cache for 30 minutes $cache->set('key3', 'value3', 1800); // Using DateInterval $cache->set('key4', 'value4', new \DateInterval('PT1H')); // 1 hour
Advanced Usage
Custom Storage Backend
You can create your own storage backend by implementing the StoreInterface:
use Godam\StoreInterface; class CustomStore implements StoreInterface { public function get(string $key): mixed { /* ... */ } public function set(string $key, mixed $value, ?int $ttl = null): bool { /* ... */ } public function delete(string $key): bool { /* ... */ } public function clear(): bool { /* ... */ } public function has(string $key): bool { /* ... */ } }
Error Handling
use Godam\Cache; use Godam\InvalidArgumentException; try { $cache->set('', 'value'); // Empty key throws InvalidArgumentException } catch (InvalidArgumentException $e) { // Handle invalid key }
Requirements
- PHP >= 8.2
- PSR Cache interfaces (automatically installed via Composer)
Testing
Run the test suite:
composer test
Or with PHPUnit directly:
vendor/bin/phpunit
License
This project is open-sourced software licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Links
vaibhavpandeyvpz/godam 适用场景与选型建议
vaibhavpandeyvpz/godam 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 972 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「psr-6」 「psr-16」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaibhavpandeyvpz/godam 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaibhavpandeyvpz/godam 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaibhavpandeyvpz/godam 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Simple PSR-16 cache implementations for WordPress transient the OOP way
Stash Cache pool wrapper
Stash Cache middleware for Relay
PSR-6 Cache for Craft CMS
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 972
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-25