ignite-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Lin" ...@linlyu.com>
Subject Re: Can I set an expiry policy for some specify entry?
Date Wed, 02 Dec 2015 01:31:12 GMT
Hi Vladimir,


Thank you for your help. I have tested your test case, but it was not worked. It is failed
as the got cache1 is null.
  Here is the output result and exception,


[09:23:09,404][INFO ][test-runner][GridDiscoveryManager] Topology snapshot [ver=1, servers=1,
clients=0, CPUs=8, heap=3.5GB]
[09:23:09,432][INFO ][exchange-worker-#43%examples.EventsExamplesSelfTest0%][GridCacheProcessor]
Started cache [name=DiffCache, mode=PARTITIONED]
[09:23:09,472][INFO ][exchange-worker-#43%examples.EventsExamplesSelfTest0%][GridDhtPreloader]
<DiffCache> Starting rebalancing in SYNC mode: DiffCache
EventsExamplesSelfTest.testExpiry cache1 = null
[09:23:09,475][INFO ][main][root] >>> Stopping test: testExpiry in 1557 ms <<<
[09:23:09,476][INFO ][main][root] >>> Stopping grid [name=examples.EventsExamplesSelfTest0,
id=003ae91f-041c-4cd0-a36f-2cad4e238000]
[09:23:09,474][ERROR][main][root] Test failed.
java.lang.NullPointerException
	at org.apache.ignite.examples.EventsExamplesSelfTest.testExpiry(EventsExamplesSelfTest.java:61)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at junit.framework.TestCase.runTest(TestCase.java:176)
	at org.apache.ignite.testframework.junits.GridAbstractTest.runTestInternal(GridAbstractTest.java:1665)
	at org.apache.ignite.testframework.junits.GridAbstractTest.access$000(GridAbstractTest.java:111)
	at org.apache.ignite.testframework.junits.GridAbstractTest$6.run(GridAbstractTest.java:1603)



Here is the testing case:


public class EventsExamplesSelfTest extends GridAbstractExamplesTest {
    public void testExpiry() throws Exception {
        Ignite ignite = startGrid(0);

        CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>();

        cfg.setName("DiffCache");
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setRebalanceMode(CacheRebalanceMode.SYNC);
        cfg.setBackups(1);

        ignite.getOrCreateCache(cfg);

        IgniteCache<Object, Object> cache1 = ignite.cache(null);

        System.out.println("EventsExamplesSelfTest.testExpiry cache1 = " + cache1);

        IgniteCache<Object, Object> cache2 = cache1.withExpiryPolicy(
                new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 1)));

        cache1.put(1, 1);
        cache1.put(2, 2);
        cache2.put(3, 3);

        cache2.get(1); // Does not affect ExpiryPolicy.

        U.sleep(2000);

        assert cache1.get(1) == 1;
        assert cache2.get(1) == 1; // not Expired
        assert cache1.get(2) == 2;
        assert cache1.get(3) == null; // Expired.
    }
}


I have tried to do something like the expiry policies wrapper, and it is just a semi-finished,
and I am not sure will it be worked for the all situations.


I would like to share my idea and codes in the next response, hope you and other guys to given
any suggestions.


Thanks again.


Lin.


------------------ Original ------------------
From:  "Vladimir Ershov";<vershov@gridgain.com>;
Date:  Tue, Dec 1, 2015 08:33 PM
To:  "user"<user@ignite.apache.org>; 

Subject:  Re: Can I set an expiry policy for some specify entry?



Hi Lin,

An expiry policy is working for all values, which were added through cacheWithExpiryPolicy
according to the next example:
IgniteCache<Object, Object> cacheWithExpiryPolicy = cache.withExpiryPolicy(     new
CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5)));


You are welcome to find an explaining example in the end of this message.
It is also possible, that actually you are looking for something like eviction policy. Please
take a look here then: https://apacheignite.readme.io/v1.5/docs/evictions

Please, provide the feedback, if this answer was useful, or not.

Thanks!



    public void test() throws Exception {
        Ignite ignite = startGrid(0); // some starting util method

        CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>();

        cfg.setName(CACHE);
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setRebalanceMode(CacheRebalanceMode.SYNC);
        cfg.setBackups(1);

        ignite.getOrCreateCache(cfg);

        IgniteCache<Object, Object> cache1 = ignite.cache(null);

        IgniteCache<Object, Object> cache2 = cache1.withExpiryPolicy(
            new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 1)));

        cache1.put(1, 1);
        cache1.put(2, 2);
        cache2.put(3, 3);

        cache2.get(1); // Does not affect ExpiryPolicy.

        U.sleep(2000);

        assert cache1.get(1) == 1;
        assert cache2.get(1) == 1; // not Expired
        assert cache1.get(2) == 2;
        assert cache1.get(3) == null; // Expired.
    }

On Tue, Dec 1, 2015 at 10:47 AM, Lin <me@linlyu.com> wrote:
Hi,


I have read the docs on jcache expiry policies, the policy  will be used for each operation
invoked on the returned cache instance.


IgniteCache<Object, Object> cache = cache.withExpiryPolicy(     new CreatedExpiryPolicy(new
Duration(TimeUnit.SECONDS, 5)));


and searched the nabble faq and found 
http://apache-ignite-users.70518.x6.nabble.com/Does-IgniteCache-withExpiryPolicy-affect-existing-cache-entries-td1870.html


As I know, the expiry policy is worked for all the entries in the cache. I would like to specify
different expiry policies for some different entries,
How can I do?


Thanks for you help.




Regards,


Lin
Mime
View raw message