Coder Social home page Coder Social logo

Comments (8)

WyriHaximus avatar WyriHaximus commented on June 14, 2024

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.

jsor avatar jsor commented on June 14, 2024

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.

kelunik avatar kelunik commented on June 14, 2024

+1 for returning null on miss. Works well with ??.

from cache.

jsor avatar jsor commented on June 14, 2024

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.

kelunik avatar kelunik commented on June 14, 2024

@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.

jsor avatar jsor commented on June 14, 2024

@kelunik You don't have to convience me, i've opened that issue ;)

from cache.

clue avatar clue commented on June 14, 2024

I agree that returning null makes sense here as this clearly isn't "exceptional" 👍

from cache.

WyriHaximus avatar WyriHaximus commented on June 14, 2024

Work in Progress PR up at #22

from cache.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.