[Cache] Interface Proposal and Implementations

58 views
Skip to first unread message

Jon Whelan

unread,
Oct 20, 2014, 11:05:19 PM10/20/14
to php...@googlegroups.com
Since it sounds like the group is moving away from the current proposed cache interface, I'd like to propose a new one that addresses concerns as of late.

Interface is here: https://github.com/jonwhelan/cache/blob/master/src/Maps/CacheMapInterface.php and there are some common implementations (tested) in the repo.

Some notables about the interface are:
  1. It uses more appropriate map semantics instead of collection semantics.
  2. Uses a "weak" item approach since all the calling library should concern itself with is getting values and setting values—no need for it to know any cache implementation details such as ttl, tags, etc.
  3. Uses a "find" method which returns an iterator which affords us a few advantages
    • Allows the use of the weak item without arbitrarily imposing restrictions of not allowing null (or false) to be a valid cache value since if the item does not exist, then the iterator returned from "find" will be empty and the client code can check for this.
    • Prevents a pontential race condition occurring when the client code calls "contains" followed by a "get" since find returns the value and allows the client to check if it's a hit or miss.
  4. The difference between "get" and "find" is that "get" is used when the client code assumes a precondition of a cache hit (which is why it throws and exception when there is a miss), whereas "find" is used when the client cannot assume a hit and checks hit or miss based on the validity of the iterator.
The repo also features an example of how you can add stampede protection using the CacheMapInterface and a LockInterface.

I'll also be adding an example of how you'd implement deferred saving using this interface and a unit of work interface

Thoughts?
Reply all
Reply to author
Forward
0 new messages