Skip to content

[V5˖] Detaching Attaching item to from pool

Georges.L edited this page Jun 10, 2019 · 1 revision

Since the V5, the ItemPool API (ExtendedCacheItemPoolInterface) supports some new methods:

  • detachAllItems()
  • detachItem($item)
  • isAttached($item)
  • attachItem($item)

Those methods allow you to "detach" an item from the pool to allow you to "re-pull" the item from the cache backend. This is useful when you need to retrieve a "fresh cache item" from the backend if you think that your current cache item is outdated/expired in the "current script" context.

When the pull returns you a "cache item" it actually returns you a "snapshoot" of the cache item as it has been returned by the cachen backend at the T time. What's that means exactly ?

If you have concurrent perpetual scripts running along each others that keep the cache item in the pool (statically in memory) you will may face to concurrence job issue. This issue can be addressed by calling detachItem($item) or detachAllItems() just before calling getItem($cacheKey), forcing by this way the pool to retrieve a fresh cache item, avoiding you job concurrency issues.

If for any reason you need to re-attach a previously-detached cache item from pool, can do it by calling attachItem($item) under some restrictions:

  • You cannot re-attach an expired item
  • You cannot re-attach an item if there's already one with the same cache key in pool
  • You cannot re-attach a cool pool from a foreign backend (E.g Redis to Predis)

Finally please note that isAttached($item) not only check the cache key but also the object hash in order to check you are checking to re-attach the exact same object.