Comments (8)
What would you expect in this situation @jsor? That behavior makes perfect sense to me, and I'm using it extensively for flow control in applications/packages.
from cache.
The correct implementation in ArrayCache would be:
public function get($key)
{
if (!isset($this->data[$key])) {
return Promise\resolve(null);
}
return Promise\resolve($this->data[$key]);
}
Rejection means that an error occured, eg. in the underlying storage system.
In case of the WyriHaximus/reactphp-cache-redis implementation, rejection should only happen when the redis client reports an error, eg. https://github.com/clue/php-redis-react/blob/master/src/StreamingClient.php#L90 because this is then a bug in your application.
The implementation should be something like
public function get($key)
{
return $this->client->exists($this->prefix . $key)->then(function ($result) use ($key) {
if ($result == false) {
return null;
}
return $this->client->get($this->prefix . $key);
});
}
And used like
$redisCache->get('foo')->then(
function ($result) {
if (null === $result) {
// cache miss
return createAndStoreAndReturn('foo');
}
return handle($result);
},
function (\Exception $e) {
call('WyriHaximus')->andTellHim('Theres a bug in your application or the redis server is down');
}
);
from cache.
+1 for returning null
on miss. Works well with ??
.
from cache.
An alternative approach would be to reject with an exception on cache miss. This way, a cache miss can be handled differently than exceptions thrown from the underlying storage system (requires react/promise ^2.0).
$cache->get('foo')
->otherwise(function (CacheMissException $e) {
return getFromDB('foo');
})
->otherwise(function($e) {
log($e);
})
->then('var_dump');
;
from cache.
@jsor If a cache miss returns null
and an issue in the underlying storage system throws an exception you can differentiate that as well. And you don't have exceptions for non-exceptional code paths.
from cache.
@kelunik You don't have to convience me, i've opened that issue ;)
from cache.
I agree that returning null
makes sense here as this clearly isn't "exceptional" 👍
from cache.
Work in Progress PR up at #22
from cache.
Related Issues (11)
- [RFC] Support TTL HOT 3
- Return values for set and remove HOT 4
- Limit cache size for ArrayCache HOT 2
- PSR-6 and PSR-16 bridge HOT 6
- Default value for get
- Support getMultiple and setMultiple
- There is a Chinese Edition of the ReactPHP-Cache document. HOT 3
- Support hasMultiple HOT 5
- Roadmap to reactphp/cache v3
- [RFC] Prepare stable release HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cache.