You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by John Smith <ja...@gmail.com> on 2020/04/21 19:00:16 UTC

Best way to track if key was read more than X times?

Hi I want to store a key/value and If that key has been accessed more than
3 times for example remove it. What is the best way to do this?

Re: Best way to track if key was read more than X times?

Posted by John Smith <ja...@gmail.com>.
For those who are curious I did it like this...

Basically MyTuple is just "value" class that has two properties, the actual
value to be stored in the cache and a counter.

public class TestEntryProcessor implements EntryProcessor<String,
MyTuple, MyTuple> {
        @Override public MyTuple process(MutableEntry<String, MyTuple>
e, Object... args) {
            if(e.exists()) {
                MyTuple tuple = e.getValue();

                if(tuple.verificationCount-- == 0) {
                    e.remove();

                    return null;
                } else {
                    e.setValue(tuple);

                    return tuple;
                }
            } else {
                return null;
            }
        }
    }


On Thu, 23 Apr 2020 at 10:44, Ilya Kasnacheev <il...@gmail.com>
wrote:

> Hello!
>
> Yes, I think you can update an entry with EntryProcessor while also
> returning it.
>
> Regards,
> --
> Ilya Kasnacheev
>
>
> ср, 22 апр. 2020 г. в 19:35, John Smith <ja...@gmail.com>:
>
>> Hi, akonresh understood, but then I would need another cache to keep
>> track of those counts.
>>
>> Ilya would a EntryProcessor allow for that with the invoke? Because
>> creating a wrapper I still need to track the counts.
>>
>> On Wed, 22 Apr 2020 at 12:10, Ilya Kasnacheev <il...@gmail.com>
>> wrote:
>>
>>> Hello!
>>>
>>> I actually think that the optimal way is to have your own wrapper API
>>> which is only source of cache gets and which does this accounting under the
>>> hood.
>>>
>>> Then it can invoke the same cache entry to keep track of number of reads.
>>>
>>> Regards,
>>> --
>>> Ilya Kasnacheev
>>>
>>>
>>> вт, 21 апр. 2020 г. в 22:00, John Smith <ja...@gmail.com>:
>>>
>>>> Hi I want to store a key/value and If that key has been accessed more
>>>> than 3 times for example remove it. What is the best way to do this?
>>>>
>>>

Re: Best way to track if key was read more than X times?

Posted by Ilya Kasnacheev <il...@gmail.com>.
Hello!

Yes, I think you can update an entry with EntryProcessor while also
returning it.

Regards,
-- 
Ilya Kasnacheev


ср, 22 апр. 2020 г. в 19:35, John Smith <ja...@gmail.com>:

> Hi, akonresh understood, but then I would need another cache to keep track
> of those counts.
>
> Ilya would a EntryProcessor allow for that with the invoke? Because
> creating a wrapper I still need to track the counts.
>
> On Wed, 22 Apr 2020 at 12:10, Ilya Kasnacheev <il...@gmail.com>
> wrote:
>
>> Hello!
>>
>> I actually think that the optimal way is to have your own wrapper API
>> which is only source of cache gets and which does this accounting under the
>> hood.
>>
>> Then it can invoke the same cache entry to keep track of number of reads.
>>
>> Regards,
>> --
>> Ilya Kasnacheev
>>
>>
>> вт, 21 апр. 2020 г. в 22:00, John Smith <ja...@gmail.com>:
>>
>>> Hi I want to store a key/value and If that key has been accessed more
>>> than 3 times for example remove it. What is the best way to do this?
>>>
>>

Re: Best way to track if key was read more than X times?

Posted by John Smith <ja...@gmail.com>.
Hi, akonresh understood, but then I would need another cache to keep track
of those counts.

Ilya would a EntryProcessor allow for that with the invoke? Because
creating a wrapper I still need to track the counts.

On Wed, 22 Apr 2020 at 12:10, Ilya Kasnacheev <il...@gmail.com>
wrote:

> Hello!
>
> I actually think that the optimal way is to have your own wrapper API
> which is only source of cache gets and which does this accounting under the
> hood.
>
> Then it can invoke the same cache entry to keep track of number of reads.
>
> Regards,
> --
> Ilya Kasnacheev
>
>
> вт, 21 апр. 2020 г. в 22:00, John Smith <ja...@gmail.com>:
>
>> Hi I want to store a key/value and If that key has been accessed more
>> than 3 times for example remove it. What is the best way to do this?
>>
>

Re: Best way to track if key was read more than X times?

Posted by Ilya Kasnacheev <il...@gmail.com>.
Hello!

I actually think that the optimal way is to have your own wrapper API which
is only source of cache gets and which does this accounting under the hood.

Then it can invoke the same cache entry to keep track of number of reads.

Regards,
-- 
Ilya Kasnacheev


вт, 21 апр. 2020 г. в 22:00, John Smith <ja...@gmail.com>:

> Hi I want to store a key/value and If that key has been accessed more than
> 3 times for example remove it. What is the best way to do this?
>

Re: Best way to track if key was read more than X times?

Posted by akorensh <al...@gmail.com>.
Hi, 

   You can try it w/the EntryProcessor.

   With Events you would "listen" for the get operation, and when the count
of a certain key gets to a
   specified threshold, take the appropriate action.


Thanks, Alex



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: Best way to track if key was read more than X times?

Posted by John Smith <ja...@gmail.com>.
Ok bu the event just tells me if the key was read correct? I need to keep a
count of how many times each key was read globally.

The other way I was thinking of doing it is by having a cache as
Cache<String, MyTuple> and then use cache.invoke(...., new
CounterEntryProcessor())

And then in the EntryProcessor...

MyTuple is just a a value class that holds 2 values, one being the counter.

class CounterEntryProcessor implements EntryProcessor<String, MyTuple,
Integer> {
    @Override public Integer process(MutableEntry<String, MyTuple> e,
Object... args) {


      MyTuple newVal = e.getValue();
      newVal.counter++

      // Update cache.
      e.setValue(newVal);

      return newVal.counter;
    }
  }

And then check if cache.invoke(...., new CounterEntryProcessor()) >= 3
remove the entry from cache.


On Tue, 21 Apr 2020 at 16:28, akorensh <al...@gmail.com> wrote:

> Hi,
>
> You can use Events:https://apacheignite.readme.io/docs/events
> In Particular: EVT_CACHE_OBJECT_READ
> see:
>
> https://github.com/apache/ignite/blob/master/examples/src/main/java/org/apache/ignite/examples/events/EventsExample.java
>
>
>
> Here is an example: (modified localListen() fn in the above example)
>  private static void localListen() throws IgniteException,
> InterruptedException {
>         System.out.println(">>> Local event listener example.");
>
>         Ignite ignite = Ignition.ignite();
>
>         IgnitePredicate<CacheEvent> lsnr = evt -> {
>             System.out.println("Received task event [evt=" + evt.name() +
> ",
> keyName=" + evt.key() + ']');
>
>             return true; // Return true to continue listening.
>         };
>
>         ignite.events().localListen(lsnr, EVT_CACHE_OBJECT_READ);
>
>         ignite.getOrCreateCache("test").put("a", "a");
>         ignite.getOrCreateCache("test").put("b", "b");
>
>         for (int i = 0; i < 100 ; i++) {
>             if(i %2 ==0 ) ignite.getOrCreateCache("test").get("a");
>             else ignite.getOrCreateCache("test").get("b");
>             Thread.sleep(1000);
>             System.out.println("sleeping..");
>         }
>
>         // Unsubscribe local task event listener.
>         ignite.events().stopLocalListen(lsnr);
>     }
>
>
> You can also use continuous queries to listen for updates:
> https://apacheignite.readme.io/docs/continuous-queries  for
> updates/inserts
>
>
>
>
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
>

Re: Best way to track if key was read more than X times?

Posted by akorensh <al...@gmail.com>.
Hi,

You can use Events:https://apacheignite.readme.io/docs/events
In Particular: EVT_CACHE_OBJECT_READ
see:
https://github.com/apache/ignite/blob/master/examples/src/main/java/org/apache/ignite/examples/events/EventsExample.java



Here is an example: (modified localListen() fn in the above example)
 private static void localListen() throws IgniteException,
InterruptedException {
        System.out.println(">>> Local event listener example.");

        Ignite ignite = Ignition.ignite();

        IgnitePredicate<CacheEvent> lsnr = evt -> {
            System.out.println("Received task event [evt=" + evt.name() + ",
keyName=" + evt.key() + ']');

            return true; // Return true to continue listening.
        };

        ignite.events().localListen(lsnr, EVT_CACHE_OBJECT_READ);

        ignite.getOrCreateCache("test").put("a", "a");
        ignite.getOrCreateCache("test").put("b", "b");

        for (int i = 0; i < 100 ; i++) {
            if(i %2 ==0 ) ignite.getOrCreateCache("test").get("a");
            else ignite.getOrCreateCache("test").get("b");
            Thread.sleep(1000);
            System.out.println("sleeping..");
        }

        // Unsubscribe local task event listener.
        ignite.events().stopLocalListen(lsnr);
    }


You can also use continuous queries to listen for updates:
https://apacheignite.readme.io/docs/continuous-queries  for updates/inserts 







--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/