You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by kevin <ke...@leonardo.com> on 2015/12/29 19:01:15 UTC

Is there a hook on cache entry expiry?

Does Ignite provide a hook on when a cache entry gets expired, due to the
expiry policy?

I guess what I'm looking for is something like
org.apache.ignite.cache.CacheInterceptor with a method "onExpiry", if that
exists.

If not, do you guys have any recommendations on how to do that?

My use case is this:
I have multiple cache entries belonging to a "group" that I must expire
together. I thought I could create a second cache instead for that "group",
and when the "group" cache entry expires, then I'll manually evict the
entries belonging to that group.

Thanks



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Is-there-a-hook-on-cache-entry-expiry-tp2344.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Is there a hook on cache entry expiry?

Posted by Denis Magda <dm...@gridgain.com>.
Now I see what you're trying to achieve.

When you're executing these lines of the code

   for (int i = 0; i < 100_000; i++) {
      cache2.put(i, i);

each cache entry will have expiration time that can be calculated as ET 
= current_time + policy_expire_time.
It's worth to mention that 'current_time' of each entry will differ on 
some delta cause entries are inserted one by one into the cache and thus 
will be evicted in an order they were inserted.
Transactions don't have any affect on the value of expiration time of an 
entry.

I would recommend you using cache2.putAll(allEntries) instead in order 
to minimize the delta, mentioned above, as much as possible.
This should let you to support almost 'grouped' expiration of entries.

--
Denis

On 12/30/2015 8:37 PM, kevin wrote:
> I thought of a test to put together but it didn't work.
>
>          CacheConfiguration<Integer, Integer> cfg = new
> CacheConfiguration<>();
>          cfg.setName("test1");
>          cfg.setCacheMode(CacheMode.PARTITIONED);
>          cfg.setRebalanceMode(CacheRebalanceMode.SYNC);
>          cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
>          cfg.setBackups(1);
>
>          IgniteCache<Integer, Integer> cache1 = ignite.getOrCreateCache(cfg);
>          IgniteCache<Integer, Integer> cache2 = cache1.withExpiryPolicy(
>                  new AccessedExpiryPolicy(new Duration(TimeUnit.SECONDS,
> 2)));
>
>          try (Transaction tx = ignite.transactions().txStart()) {
>              for (int i = 0; i < 100_000; i++) {
>                  cache2.put(i, i);
>              }
>              tx.commit();
>          }
>
>          int lastSize = -1;
>          while (true) {
>              int size = cache2.size();
> //            int size = cache2.localSize();
> //            int size = cache2.query(new ScanQuery<>()).getAll().size();
>              if (size != lastSize) {
>                  System.out.println(size);
>                  lastSize = size;
>              }
>          }
>
> I would've expected to see
> 100000
> 0
>
> But I got
> 100000
> 99846
> 99431
> 98927
> etc...
>
> I also tried a few ways of accessing the cache as in the commented code.
>
>
>
> --
> View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Is-there-a-hook-on-cache-entry-expiry-tp2344p2360.html
> Sent from the Apache Ignite Users mailing list archive at Nabble.com.


Re: Is there a hook on cache entry expiry?

Posted by kevin <ke...@leonardo.com>.
I thought of a test to put together but it didn't work.

        CacheConfiguration<Integer, Integer> cfg = new
CacheConfiguration<>();
        cfg.setName("test1");
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setRebalanceMode(CacheRebalanceMode.SYNC);
        cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cfg.setBackups(1);

        IgniteCache<Integer, Integer> cache1 = ignite.getOrCreateCache(cfg);
        IgniteCache<Integer, Integer> cache2 = cache1.withExpiryPolicy(
                new AccessedExpiryPolicy(new Duration(TimeUnit.SECONDS,
2)));

        try (Transaction tx = ignite.transactions().txStart()) {
            for (int i = 0; i < 100_000; i++) {
                cache2.put(i, i);
            }
            tx.commit();
        }

        int lastSize = -1;
        while (true) {
            int size = cache2.size();
//            int size = cache2.localSize();
//            int size = cache2.query(new ScanQuery<>()).getAll().size();
            if (size != lastSize) {
                System.out.println(size);
                lastSize = size;
            }
        }

I would've expected to see
100000
0

But I got
100000
99846
99431
98927
etc...

I also tried a few ways of accessing the cache as in the commented code.



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Is-there-a-hook-on-cache-entry-expiry-tp2344p2360.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Is there a hook on cache entry expiry?

Posted by Denis Magda <dm...@gridgain.com>.
Yes, it would work. Just give a try.

--

Denis

On Wednesday, December 30, 2015, kevin <ke...@leonardo.com> wrote:

> Thanks Denis for pointing me to EVT_CACHE_OBJECT_EXPIRED.
>
> But let me expand, what I want is to have multiple entries expire in one
> transaction.
>
> Example:
> IgniteCache cache = ignite.cache("testCache").withExpiryPolicy(expiry);
>
> try (ignite.transactions.txStart()) {
>     // I want these 10 entries to expire atomically
>     for (int i = 0; i < 10; i++)
>         cache.put(String.valueOf(i), i);
> }
>
> // Eg. if I do something like
> cache.query(...where key < 10...).getAll();
> // I want either all of the entries, or none of them
>
> Would the expiry policy still work for this?
>
>
>
> --
> View this message in context:
> http://apache-ignite-users.70518.x6.nabble.com/Is-there-a-hook-on-cache-entry-expiry-tp2344p2357.html
> Sent from the Apache Ignite Users mailing list archive at Nabble.com.
>

Re: Is there a hook on cache entry expiry?

Posted by kevin <ke...@leonardo.com>.
Thanks Denis for pointing me to EVT_CACHE_OBJECT_EXPIRED.

But let me expand, what I want is to have multiple entries expire in one
transaction.

Example:
IgniteCache cache = ignite.cache("testCache").withExpiryPolicy(expiry);

try (ignite.transactions.txStart()) {
    // I want these 10 entries to expire atomically
    for (int i = 0; i < 10; i++)
        cache.put(String.valueOf(i), i); 
}

// Eg. if I do something like
cache.query(...where key < 10...).getAll();
// I want either all of the entries, or none of them

Would the expiry policy still work for this?



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Is-there-a-hook-on-cache-entry-expiry-tp2344p2357.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Is there a hook on cache entry expiry?

Posted by Denis Magda <dm...@gridgain.com>.
Hi,

When a cache entry gets expired Ignite fires EVT_CACHE_OBJECT_EXPIRED event.
Just subscribe for this kind of event notifications if you want to know when
this happens. This article [1] contains more info on how to work with events
in Ignite.

However, when a cache entry is expired it's automatically removed from a
cache, you don't need to remove expired entries manually.

To fully support your use case you can use the code snippet below as an
example:

// Create a specific expire policy for a "group" of entries you're going to
put into a cache.
ExpiryPolicy expiry = new CreatedExpiryPolicy(new Duration(MINUTES, 5));

// Get an instance to a cache with this expire policy set.
IgniteCache cache = ignite.cache("testCache").withExpiryPolicy(expiry);

// All these 10 entries will expire in 5 minutes and will be removed
automatically. The rest of the entries are not affected
for (int i = 0; i < 10; i++)
    cache.put(String.valueOf(i), i);


Regards,
Denis

[1] https://apacheignite.readme.io/docs/events



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Is-there-a-hook-on-cache-entry-expiry-tp2344p2351.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.