ignite-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yohan Fernando <Yohan.Ferna...@tudor.com>
Subject RE: IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)
Date Wed, 07 Aug 2019 12:47:58 GMT
Yes I did think of that approach but wanted to avoid the risk of the expensive load from the
DB being called more than once unnecessarily. I suppose that should be balanced the cost of
the IgniteCache.lock() call.

Thanks for the reply Andei

From: Andrei Aleksandrov <aealexsandrov@gmail.com>
Sent: 07 August 2019 13:02
To: user@ignite.apache.org
Subject: Re: IgniteCache.lock behaviour on a key that doesn't exist in the cache (yet)

Caution: This email originated from outside of Tudor.



Hi,

I tested your code and looks like it worked fine.

However, you also can try to use next method:

https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/IgniteCache.html#getAndPutIfAbsent-K-V-<https://urldefense.proofpoint.com/v2/url?u=https-3A__ignite.apache.org_releases_latest_javadoc_org_apache_ignite_IgniteCache.html-23getAndPutIfAbsent-2DK-2DV-2D&d=DwMDaQ&c=lcVbikor4usg5Rj5OmznbA&r=m3tOHOhTDmqyOCq26PYainXMtahLsVWUQ4sqnN5broM&m=UcWLm-g-gCe-3iHnMWXZQbhbx_-cv6pxY4avfRjelKE&s=SRwQdmYBeAelb1jM_p-FLF7epPbWWn00-TwQUQf9C74&e=>

In this case you can:

1)Check cache.containsKey(key)
2)If true then prepare the default value
3)Run getAndPutIfAbsent(key, defaultValue)

If another client was able to put the value during current prepare the default then you will
get that value, otherwise default value will be returned.

BR,
Andrei
8/7/2019 12:08 PM, Yohan Fernando пишет:
I'm trying to write a transaction-safe way of performing a lazy load of an object from the
database if it doesnt exist. However as IgniteCache doesn't have the equivalent of HashMap.computeIfAbsent,
I'm trying to use the IgniteCache.lock method to achieve this.

The question is whether IgniteCache.lock will lock the cache for that key even if <b>the
key does not yet exist in the cache</b>.

Following is the example code,

public GridOrder getOrder(OrderKey key) {
        Lock orderKeyLock = cache.lock(key);
        try {
            orderKeyLock.lock();
            if (!cache.containsKey(key)) {
                GridOrder order = loadOrderFromDb(key);
                if ( order == null) {
                    throw new IllegalStateException("Key " + key + " not in Order Cache or
in Database DB Name ");
                }
                cache.put(key,order);
            }
            return cache.get(key);
        } finally {
            orderKeyLock.unlock();
        }
    }

Alternatively, is there a better way to achieve this?

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended only for the
addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality
by misdelivery. If you have received this email in error, please notify the sender immediately
and delete it. You should not copy it, forward it or otherwise use the contents, attachments
or information in any way. Any liability for viruses is excluded to the fullest extent permitted
by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Conduct Authority
(the FCA). TCE is registered as a limited liability partnership in England and Wales No: OC340673
with its registered office at 10 New Burlington Street, London, W1S 3BE, United Kingdom

_________________________________________________________

This email, its contents, and any attachments transmitted with it are intended only for the
addressee(s) and may be confidential and legally privileged. We do not waive any confidentiality
by misdelivery. If you have received this email in error, please notify the sender immediately
and delete it. You should not copy it, forward it or otherwise use the contents, attachments
or information in any way. Any liability for viruses is excluded to the fullest extent permitted
by law.

Tudor Capital Europe LLP (TCE) is authorised and regulated by The Financial Conduct Authority
(the FCA). TCE is registered as a limited liability partnership in England and Wales No: OC340673
with its registered office at 10 New Burlington Street, London, W1S 3BE, United Kingdom
Mime
View raw message