|
| 1 | +# Phpfastcache |
| 2 | + |
| 3 | +> A high-performance PHP caching library that provides a unified abstraction layer over many cache backends. PSR-6 and PSR-16 compliant. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Phpfastcache is an open-source PHP caching library designed for building reactive applications. It offers a single, consistent API across dozens of cache backends — from file-based caching to Redis clusters, Memcached, Couchbase, MongoDB, and more. The library focuses on performance, security, and portability. |
| 8 | + |
| 9 | +- **Current stable version**: 9.2.4 |
| 10 | +- **License**: MIT |
| 11 | +- **Requires**: PHP >= 8.0 |
| 12 | +- **Standards**: PSR-6 (Cache Interface), PSR-16 (Simple Cache), PSR-12 (Coding Style) |
| 13 | +- **Package**: `composer require phpfastcache/phpfastcache` |
| 14 | + |
| 15 | +## Supported Cache Drivers |
| 16 | + |
| 17 | +### Core Drivers (included) |
| 18 | +- Apcu, Files, Leveldb, Memcache(d), Sqlite, Wincache (deprecated), Zend Disk Cache, Zend Memory Cache |
| 19 | +- CouchBasev3 (deprecated, will be removed in v10) |
| 20 | +- Predis, Redis, RedisCluster, Ssdb |
| 21 | +- Devnull, Devrandom, Memory (development drivers) |
| 22 | + |
| 23 | +### Extension Drivers (install separately via Composer) |
| 24 | +- Arangodb (`phpfastcache/arangodb-extension`) |
| 25 | +- Cassandra |
| 26 | +- Couchbasev4 (`phpfastcache/couchbasev4-extension`) |
| 27 | +- Couchdb (`phpfastcache/couchdb-extension`) |
| 28 | +- Dynamodb (`phpfastcache/dynamodb-extension`) |
| 29 | +- Firestore (`phpfastcache/firestore-extension`) |
| 30 | +- Mongodb (`phpfastcache/mongodb-extension`) |
| 31 | +- Ravendb (`phpfastcache/ravendb-extension`) |
| 32 | +- Solr (`phpfastcache/solr-extension`) |
| 33 | + |
| 34 | +### Cluster/Aggregated Drivers |
| 35 | +- FullReplicationCluster, SemiReplicationCluster, MasterSlaveReplicationCluster, RandomReplicationCluster |
| 36 | + |
| 37 | +## Quick Start |
| 38 | + |
| 39 | +```php |
| 40 | +use Phpfastcache\CacheManager; |
| 41 | +use Phpfastcache\Drivers\Redis\Config as RedisConfig; |
| 42 | + |
| 43 | +// Setup Redis (or any other driver) |
| 44 | +$cacheInstance = CacheManager::getInstance('redis', new RedisConfig([ |
| 45 | + 'host' => '127.0.0.1', |
| 46 | + 'port' => 6379, |
| 47 | +])); |
| 48 | + |
| 49 | +// Get a cache item |
| 50 | +$item = $cacheInstance->getItem('my_key'); |
| 51 | + |
| 52 | +if (!$item->isHit()) { |
| 53 | + $item->set('my_value') |
| 54 | + ->expiresAfter(300); // 5 minutes |
| 55 | + $cacheInstance->save($item); |
| 56 | +} |
| 57 | + |
| 58 | +echo $item->get(); // "my_value" |
| 59 | +``` |
| 60 | + |
| 61 | +## Key APIs |
| 62 | + |
| 63 | +### Cache Item (ExtendedCacheItemInterface) |
| 64 | +- `get()` / `set($value)` — Get/set cached value |
| 65 | +- `isHit()` — Check if cache entry exists and is valid |
| 66 | +- `expiresAfter($ttl)` / `expiresAt($expiration)` — Set TTL |
| 67 | +- `getTtl()` — Get remaining TTL |
| 68 | +- `addTag($tagName)` / `addTags(array)` / `removeTag($tagName)` — Tag management |
| 69 | +- `getTags()` / `hasTag($tagName)` / `hasTags(array, $strategy)` — Tag queries |
| 70 | +- `append($data)` / `prepend($data)` — Modify string/array data |
| 71 | +- `increment($step)` / `decrement($step)` — Atomic counters |
| 72 | +- `isExpired()` / `isEmpty()` / `isNull()` — State checks |
| 73 | +- `getCreationDate()` / `getModificationDate()` / `getExpirationDate()` — Date metadata |
| 74 | + |
| 75 | +### Cache Pool (ExtendedCacheItemPoolInterface) |
| 76 | +- `getItem($key)` / `getItems(array $keys)` — Retrieve items |
| 77 | +- `save($item)` / `saveDeferred($item)` / `commit()` — Persist items |
| 78 | +- `deleteItem($key)` / `deleteItems(array $keys)` — Delete items |
| 79 | +- `hasItem($key)` — Check existence |
| 80 | +- `clear()` — Flush all items |
| 81 | +- `getItemsByTag($tagName)` / `deleteItemsByTag($tagName)` — Tag-based operations |
| 82 | +- `getAllItems()` — Retrieve all cached items (driver support varies) |
| 83 | +- `getStats()` — Driver statistics |
| 84 | + |
| 85 | +## Configuration |
| 86 | + |
| 87 | +Configuration is done via driver-specific `Config` objects extending `\Phpfastcache\Config\ConfigurationOption`. |
| 88 | + |
| 89 | +Common options: |
| 90 | +- `path` — Cache directory for file-based drivers |
| 91 | +- `host` / `port` — Connection details for network drivers |
| 92 | +- `defaultTtl` — Default time-to-live in seconds |
| 93 | +- `itemDetailedDate` — Enable creation/modification date tracking |
| 94 | +- `preventCacheSlams` — Prevent cache stampede (deprecated in v9.2) |
| 95 | + |
| 96 | +## Events |
| 97 | + |
| 98 | +Phpfastcache supports an event-driven architecture via `EventManager`: |
| 99 | +- Pool-scoped events via `$pool->getEventManager()` |
| 100 | +- Global events via `EventManager::getInstance()` |
| 101 | +- Supports: `onCacheGetItem`, `onCacheSaveItem`, `onCacheDeleteItem`, `onCacheClearItem`, `onCacheWriteFileOnDisk`, and more |
| 102 | + |
| 103 | +## Documentation |
| 104 | + |
| 105 | +- [GitHub Repository](https://github.com/TruCopilot/phpfastcache) |
| 106 | +- [Wiki](https://github.com/TruCopilot/phpfastcache/wiki) |
| 107 | +- [Examples](https://github.com/TruCopilot/phpfastcache/tree/master/docs/examples) |
| 108 | +- [Migration Guide (V8 to V9)](https://github.com/TruCopilot/phpfastcache/blob/master/docs/migration/MigratingFromV8ToV9.md) |
| 109 | +- [Configuration Options](https://github.com/TruCopilot/phpfastcache/blob/master/docs/OPTIONS.md) |
| 110 | +- [Events Documentation](https://github.com/TruCopilot/phpfastcache/blob/master/docs/EVENTS.md) |
| 111 | +- [Driver Details](https://github.com/TruCopilot/phpfastcache/blob/master/docs/DRIVERS.md) |
| 112 | +- [Changelog](https://github.com/TruCopilot/phpfastcache/blob/master/CHANGELOG.md) |
0 commit comments