Return Promise for set and remove#23
Conversation
src/ArrayCache.php
Outdated
| public function set($key, $value) | ||
| { | ||
| $this->data[$key] = $value; | ||
| return new Promise\FulfilledPromise(true); |
There was a problem hiding this comment.
I'd prefer Promise\resolve(true) here.
src/ArrayCache.php
Outdated
| public function remove($key) | ||
| { | ||
| unset($this->data[$key]); | ||
| return new Promise\FulfilledPromise(true); |
There was a problem hiding this comment.
I'd prefer Promise\resolve(true) here.
src/CacheInterface.php
Outdated
| * @param string $key | ||
| * @param mixed $value | ||
| * @return void | ||
| * @return PromiseInterface<bool|Exception> |
There was a problem hiding this comment.
As discussed (ot sure where atm), only fulfillment values should be included.
PromiseInterface<bool>
src/CacheInterface.php
Outdated
| * | ||
| * @param string $key | ||
| * @return void | ||
| * @return PromiseInterface<bool|Exception> |
There was a problem hiding this comment.
PromiseInterface<bool> (see above)
|
@jsor addressed your points 👍 |
src/CacheInterface.php
Outdated
| /** | ||
| * All methods only reject with an exception when an error occurs on the underlying storage | ||
| * layer, for example the connection with Redis dropped and cannot be recovered, or the | ||
| * directory on the filesystem used to store cache has the wrong permissions of is full. |
|
@jsor fixed 👍 |
clue
left a comment
There was a problem hiding this comment.
No objections, but one question:
What is an "error" and how is this related to #22? It's my understanding that we aim for a Promise-based alternative interface similar to PSR-16, which uses the following return type for set():
@return bool True on success and false on failure.
@throws \Psr\SimpleCache\InvalidArgumentException MUST be thrown if the $key string is not a legal value.
|
Good point, there was also a discussion here: https://github.com/reactphp/cache/pull/16/files#r104752102 |
|
Regarding
But since i've not attended the call, i'm probably missing something. |
|
The point is, that according to PSR-16, Refs: |
|
We've discussed this only briefly in our last hangouts. My point is: I can see some valid arguments for both directions as rightfully pointed out by @jsor. I personally don't have a use case that requires any specific behavior, I only require consistent behavior. For now, I would vote to keep this in line with PSR-16 and simply use a promise with a boolean resolution value and basically never reject. I would rather get this feature out and we can still discuss this for a future version |
src/CacheInterface.php
Outdated
| * Retrieve an item from the cache, resolves with its value on | ||
| * success or null when no item can be found. | ||
| * success or null when no item can be found. It will reject with an exception | ||
| * on error. (Note that a cache miss isn't an error.) |
There was a problem hiding this comment.
Rejecting would be the async equivalent of an Exception and I don't see this in PSR-16?
src/CacheInterface.php
Outdated
| * All methods only reject with an exception when an error occurs on the underlying storage | ||
| * layer, for example the connection with Redis dropped and cannot be recovered, or the | ||
| * directory on the filesystem used to store cache has the wrong permissions or is full. | ||
| */ |
There was a problem hiding this comment.
Not sure if this is a leftover, but this seems to contradict the below definitions?
src/CacheInterface.php
Outdated
| * @param string $key | ||
| * @param mixed $value | ||
| * @return void | ||
| * @return PromiseInterface<bool> |
There was a problem hiding this comment.
I don't see any notation for this in any of the major projects, so I would suggest using the more verbose form @return PromiseInterface Returns a promise which resolves to true on success of false on error and possibly update the description above? (See also below)
|
@clue updated the cache interface 👍 |
README.md
Outdated
|
|
||
| If the key `foo` does not exist, the promise will be fulfilled with `null` as value. | ||
| If the key `foo` does not exist, the promise will be fulfilled with `null` as value. On | ||
| any error it will reject with an exception. |
|
@clue whoops updated |
clue
left a comment
There was a problem hiding this comment.
LGTM, please squash this to a reasonable number of commits before merging ![]()
|
When @jsor also doesn't find any issues and approves I'll squash everything 👍 |
README.md
Outdated
| already exists, it is overridden. No guarantees are made as to when the cache | ||
| value is set. If the cache implementation has to go over the network to store | ||
| already exists, it is overridden. To provide guarantees as to when the cache | ||
| value is set a promise is returned. Which will fulfill with `true` on success or |
There was a problem hiding this comment.
This should either be one sentence ("...a promise is returned which will fulfill...") or better separated ("...a promise is returned. The promise will fulfill...").
|
@jsor done |
a814de4 to
584e3c2
Compare
|
Squashed the commits, the whole history without the squash is available here: https://github.com/WyriHaximus-labs/cache/tree/return_promises_history |
This PR builds on #16 by resolving merge conflicts and adding more documentation in the cache interface plus adding documentation to the readme.
Supersedes / closes #16