Git Product home page Git Product logo

Comments (3)

rdruss avatar rdruss commented on August 19, 2024

It looks like rxExecuteBlocking now returns a Maybe instead of a Single. Using toSingle().ignoreElement() seems to address this for all methods but get(K key) when a Singe<Optional > needs to be returned.

from vertx-cache-example.

cescoffier avatar cescoffier commented on August 19, 2024

It's a bit more complicated because it depends on what do we do with the result.

If we expect a Completable, using ignoreElement() is fine
If we expect a Single, using toSingle is fine. However, in case of absence, it propagates an error (which is fine in our case).

The resulting class is:

public class Cache<K, V> {

    private final Vertx vertx;
    private final RemoteCache<K, V> cache;

    public static <K, V> Single<Cache<K, V>> create(Vertx vertx) {
        Configuration config = new ConfigurationBuilder().addServer().host("cache-server").port(11222).build();
        return vertx.
            <RemoteCache<K, V>>rxExecuteBlocking(
                future -> {
                    RemoteCache<K, V> cache = new RemoteCacheManager(config).getCache();
                    future.complete(cache);
                }
            )
            .toSingle()
            .map(rc -> new Cache<>(vertx, rc));
    }

    private Cache(Vertx vertx, RemoteCache<K, V> rc) {
        this.vertx = vertx;
        this.cache = rc;
    }

    public Completable remove(K key) {
        return vertx.rxExecuteBlocking(
            future -> {
                cache.remove(key);
                future.complete();
            }
        ).ignoreElement();
    }

    public Single<Optional<V>> get(K key) {
        // While this method can use the Maybe type, I found easier to use an Optional.
        return vertx.<Optional<V>>rxExecuteBlocking(future -> {
            V value = cache.get(key);
            future.complete(Optional.ofNullable(value));
        }).toSingle();
    }

    public Completable put(K key, V value, long ttl) {
        return vertx.rxExecuteBlocking(future -> {
            cache.put(key, value, ttl, TimeUnit.SECONDS);
            future.complete();
        }).ignoreElement();
    }

}

from vertx-cache-example.

cescoffier avatar cescoffier commented on August 19, 2024

I've pushed the fix in the update-to-vertx-3.6.0 branch.

from vertx-cache-example.

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.