phpfastcache

repository·master·Indexed 25 days ago

https://github.com/trucopilot/phpfastcache

A flexible PHP caching library providing a unified abstraction layer for various backend drivers, including local files, Redis, MongoDB, and other NoSQL or cloud options. It features a PSR-16 adapter for simple caching, an event mechanism via EventManager, and advanced item control through the ExtendedCacheItemInterface. As of V9, the configuration system uses objects, and several high-performance drivers (such as Arangodb, Firestore, and Solr) are available as separate composer extensions.

Tokens
12.2K
Snippets
17
Records
78
Agent score
81%

What's inside phpfastcache

  1. Reference: Tagging Strategies

    master

    When performing operations on tags (e.g., getItemsByTags), you can specify a strategy:

    • TAG_STRATEGY_ONE: Get items that match at least ONE of the specified tags. (Default)
    • TAG_STRATEGY_ALL: Get items that match ALL of the specified tags.
    • TAG_STRATEGY_ONLY: Get items that match ONLY the specified tags (no additional tags allowed).
  2. Understand Scoped vs Unscoped EventManagers (V9.2+)

    master

    As of V9.2, there is a distinction between Scoped and Unscoped EventManagers:

    1. Scoped EventManager: Retrieved via ExtendedCacheItemPoolTrait::->getEventManager(). It only fires events related to that specific pool instance.
    2. Unscoped (Global) EventManager: Accessed via EventManager::getInstance(). It fires events from any pool, regardless of which one emitted them.

    Execution Order

    When an event is triggered, the execution order is:

    1. Scoped named event (getEventManager()->onXxxxx(...))
    2. Scoped onEveryEvent event (getEventManager()->onEveryEvent(...))
    3. Unscoped named event (EventManager::getInstance()->onXxxxx(...))
    4. Unscoped onEveryEvent event (EventManager::getInstance()->onEveryEvents(...))
  3. Use ItemPool events to intercept cache operations

    master

    Phpfastcache provides an event mechanism to intercept and manipulate cache operations at the ItemPool level. You can bind callbacks to specific lifecycle stages of the cache pool.

    Warning: Circular Methods Many events have 'Risky Circular Methods'. If you call a method listed under 'Risky Circular Methods' inside your callback, you may trigger an infinite loop (e.g., calling getItem() inside an onCacheGetItem callback).

  4. Handle reference parameters in V9 events

    master

    In V9, certain callback parameters that are not objects are passed by reference using the \Phpfastcache\Event\EventReferenceParameter class.

    This class allows you to either read or re-write the original value.

    • Reading: The class is invokable; calling it as a function returns the parameter value.
    • Writing: Use setParameterValue() to update the value. Note that if the event dispatcher does not allow type changes, attempting to change the type will throw a PhpfastcacheInvalidArgumentException.

    This mechanism allows event listeners to modify non-object arguments passed to the event.

  5. Use development drivers for testing

    master

    Phpfastcache provides several development drivers to simulate different caching behaviors during testing:

    • Devnull: A driver that returns null for read/hit actions (driverRead(), driverIsHit()) but returns true for write/delete actions (driverDelete(), DriverClear()).
    • Devrandom: A driver that returns random data with configurable chance factors and data lengths.
  6. Optimize Opcode efficiency for PHP core functions, constants, and classes

    master

    To improve Opcode efficiency, PHPFASTCACHE requires specific prefixing and import patterns for PHP core elements:

    • Core Functions: Prefix all core functions with a backslash \.
    • Core Constants: Prefix all core constants with a backslash \.
    • Core Classes: Do not use use statements to import non-namespaced classes; instead, use the absolute path with a backslash \ during instantiation.
  7. Use Tagging Strategies in Item Pools

    master

    In version 3.0.0, specific byTagsAll() methods were removed and replaced by a more flexible strategy-based approach. Instead of calling dedicated methods for every tag operation, use the standard byTags methods and pass one of the following constants from TaggableCacheItemPoolInterface:

    • TAG_STRATEGY_ONE: Matches if at least one tag matches.
    • TAG_STRATEGY_ALL: Matches if all tags match.
    • TAG_STRATEGY_ONLY: Matches if only the provided tags match.