These are chat archives for ReactiveX/RxJava

2nd
Dec 2015
Josh Durbin
@joshdurbin
Dec 02 2015 11:58

Newb question for you all. I have a basic understanding of the reactive extensions and to date have used them for basic operations. I’m building a POC where I’m leveraging RxMongo and Lettuce 4.0 RX within Ratpack w/ RX and Hystrix. My aim is to create a hystrix command similar to existing commands, but adds object caching support in redis. I have all the pieces working individually and need to compose them in the “correct” or best Rx manner.

Essentially I’ve got an input of String and I’m returning and Observable of type Product.

I’ve got two objects “redisProductsCache” and “productService”. “redisProductsCache” has a method “exists” that takes a String and returns Observable<Boolean>, “get” that takes a String and returns Observable<Product>, and “set” that takes a Product and returns a String.

“productService” has a method “getByCode” which takes a String and returns Observable<Product>.

With these I need to do the basic logic of…

  • Check if the String “code” is in the cache, if it’s there, return it, else continue.
  • Given it’s not there, call the product service, get the item, if an item is emitted, place said individual item into the cache and return it.

I’ve sort of “hacked” together a few solutions which don’t seem to be working using chained flatMap functions.

Does anyone have any pointers for doing such a thing — like use compose instead of flatMap?

Thanks in advance.

Josh Durbin
@joshdurbin
Dec 02 2015 12:14
This snippet obviously doesn’t work, but is more or less where I’m at at the moment:
  @Override
  protected Observable<Product> construct() {

    redisProductsCache
      .exists(requestParameters.productNDC)
      .flatMap({Boolean cacheHit ->

      final Observable<Product> response

      if (cacheHit) {

        response = redisProductsCache.get(requestParameters.productNDC)

      } else {

        productService.getByCode(requestParameters.productNDC)
          .flatMap({Product product ->

          redisProductsCache.set(requestParameters.productNDC, product)
        })
      }

      response
    } as Func1)
  }
Dorus
@Dorus
Dec 02 2015 13:13

From https://realm.io/news/gotocph-mattias-kappler-reactive-architecture-android/ we linked above:

Observable<Model> maybeCached() {
  return Observable.concat(cachedModel(), remoteModel()).first()
}

The idea is that cachedModel will return an empty sequence when there is no cached value, and remoteModel will then be subscribed (concat delays the second subscription until the first one finishes). first however will instantly unsubscribe if cachedModel does yield, so that remoteModel will never be called.

@joshdurbin
Josh Durbin
@joshdurbin
Dec 02 2015 18:27
Gotcha. Okay. That doesn’t actually ever cache the entry, though, correct?
Dorus
@Dorus
Dec 02 2015 18:33
eehm no, that should probably happen somewhere in remoteModel.
Josh Durbin
@joshdurbin
Dec 02 2015 18:34
Gotcha. Ok.
Josh Durbin
@joshdurbin
Dec 02 2015 21:08
Another question — what’s the best way to subscribe to an observable, in this case a Product object, pass that product to another observable method call (that runs a string), passing the product, and then ultimately return that product as an observable?
productService.getProduct(…).flatMap { Product product ->

  otherService.doSomething(product) // I need to subscribe to this, correct, but as of now I don't care about what it returns.

  return product
}
Dorus
@Dorus
Dec 02 2015 22:11
flatMap does exactly that
flatMap{product -> otherService.doSomething(product)}
or do you mean, subscribe to the other observable as a side effect, and return the original product?
Josh Durbin
@joshdurbin
Dec 02 2015 22:12
correct
Dorus
@Dorus
Dec 02 2015 22:12
In that case i would use Do
Josh Durbin
@joshdurbin
Dec 02 2015 22:12
second
ahh! ok
Dorus
@Dorus
Dec 02 2015 22:13
.doOnNext{product -> otherService.doSomething(product).subscribe()}
Josh Durbin
@joshdurbin
Dec 02 2015 22:14
perfect. i’ll give that a shot. thanks!!!
Dorus
@Dorus
Dec 02 2015 22:14
The idea is that you explictly indicate side effects in Do. Also, it's now clear product is not mutated.
Josh Durbin
@joshdurbin
Dec 02 2015 22:18
yeah!