Java Code Examples for org.cache2k.Cache#invoke()

The following examples show how to use org.cache2k.Cache#invoke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void setException_propagation() {
  final String _TEXT = "set inside process";
  Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.retryInterval(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    }
  });
  c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      e.setException(new IllegalStateException(_TEXT));
      return null;
    }
  });
  try {
    c.get(KEY);
    fail();
  } catch (CacheLoaderException ex) {
    assertTrue(ex.getCause().toString().contains(_TEXT));
  }
}
 
Example 2
Source File: ExpiryTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void manualExpiryPut_sharp() {
  Cache<Integer, Integer> c = builder(Integer.class, Integer.class)
    .expireAfterWrite(LONG_DELTA, TimeUnit.MILLISECONDS)
    .refreshAhead(true)
    .loader(new CacheLoader<Integer, Integer>() {
      @Override
      public Integer load(final Integer key) throws Exception {
        return 4711;
      }
    })
    .build();
  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      e.setExpiryTime(millis() + LONG_DELTA);
      e.setValue(7);
      return null;
    }
  });
  assertTrue(c.containsKey(1));
  assertEquals((Integer) 7, c.peek(1));
}
 
Example 3
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void load_changeRefreshTimeInLoaderNoRecord() {
  final long _probeTime = 4711;
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.wrappingLoader(new AdvancedCacheLoader<Integer, LoadDetail<Integer>>() {
          @Override
          public LoadDetail<Integer> load(final Integer key, final long startTime,
                                          final CacheEntry<Integer,
                                            LoadDetail<Integer>> currentEntry) throws Exception {
            return Loaders.wrapRefreshedTime(key, _probeTime);
          }
        });
    }
  });
  c.get(1);
  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) {
      assertEquals(0, e.getRefreshedTime());
      return null;
    }
  });
}
 
Example 4
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void load_changeRefreshTimeInLoader_triggeredViaEntryProcessor() {
  final long _probeTime = 4711;
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.recordRefreshedTime(true)
       .wrappingLoader(new AdvancedCacheLoader<Integer, LoadDetail<Integer>>() {
        @Override
        public LoadDetail<Integer> load(final Integer key, final long startTime,
                                        final CacheEntry<Integer,
                                          LoadDetail<Integer>> currentEntry) throws Exception {
          return Loaders.wrapRefreshedTime(key, _probeTime);
        }
      });
    }
  });
  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) {
      Integer v = e.getValue();
      assertEquals(_probeTime, e.getRefreshedTime());
      return null;
    }
  });
}
 
Example 5
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void load_changeRefreshTimeInLoader() {
  final long _probeTime = 4711;
  Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
    @Override
    public void extend(final Cache2kBuilder<Integer, Integer> b) {
      b.recordRefreshedTime(true)
       .wrappingLoader(new AdvancedCacheLoader<Integer, LoadDetail<Integer>>() {
        @Override
        public LoadDetail<Integer> load(final Integer key, final long startTime,
                                        final CacheEntry<Integer,
                                          LoadDetail<Integer>> currentEntry) throws Exception {
          return Loaders.wrapRefreshedTime(key, _probeTime);
        }
      });
    }
  });
  c.get(1);
  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) {
      assertEquals(_probeTime, e.getRefreshedTime());
      return null;
    }
  });
}
 
Example 6
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void initial_Not_Existing() {
  Cache<Integer, Integer> c = target.cache();
  final AtomicBoolean _reached = new AtomicBoolean(false);
  final int _KEY = 123;
  EntryProcessor p = new EntryProcessor() {
    @Override
    public Object process(MutableCacheEntry e) throws Exception {
      assertFalse(e.exists());
      assertEquals(0, e.getRefreshedTime());
      assertEquals(_KEY, e.getKey());
      _reached.set(true);
      return null;
    }
  };
  Object _result = c.invoke(_KEY, p);
  assertNull(_result);
}
 
Example 7
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void initial_GetYieldsNull() {
  Cache<Integer, Integer> c = target.cache();
  final AtomicBoolean _reached = new AtomicBoolean(false);
  EntryProcessor p = new EntryProcessor() {
    @Override
    public Object process(MutableCacheEntry e) throws Exception {
      assertNull(e.getValue());
      _reached.set(true);
      return null;
    }
  };
  final int _KEY = 123;
  Object _result = c.invoke(_KEY, p);
  assertNull(_result);
  assertTrue("no exception during process", _reached.get());
  assertFalse(c.containsKey(_KEY));
}
 
Example 8
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void initial_exists_Empty() {
  Cache<Integer, Integer> c = target.cache();
  c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      assertFalse(e.exists());
      return null;
    }
  });
  assertEquals(0, target.info().getSize());
}
 
Example 9
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void initial_getRefreshTime() {
  Cache<Integer,Integer> c = target.cache();
  final long t0 = System.currentTimeMillis();
  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) {
      assertEquals(0L, e.getRefreshedTime());
      return null;
    }
  });
}
 
Example 10
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void load_getRefreshTime() {
  CacheWithLoader cwl = cacheWithLoader();
  Cache<Integer,Integer> c = cwl.cache;
  final long t0 = millis();
  c.get(1);
  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) {
      assertThat(e.getCurrentTime(), greaterThanOrEqualTo(t0));
      assertThat("refresh time updated by put()", e.getRefreshedTime(), greaterThanOrEqualTo(t0));
      return null;
    }
  });
}
 
Example 11
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void nomap_getRefreshTime() {
  Cache<Integer,Integer> c = target.cache();
  final long t0 = millis();

  c.invoke(1, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) {
      assertThat(e.getCurrentTime(), greaterThanOrEqualTo(t0));
      assertEquals(0, e.getRefreshedTime());
      return null;
    }
  });
}
 
Example 12
Source File: BasicCacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
long getRefreshedTimeViaEntryProcessor(Cache<Integer, Integer> c, int key) {
  return c.invoke(key, new EntryProcessor<Integer, Integer, Long>() {
    @Override
    public Long process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      return e.getRefreshedTime();
    }
  });
}
 
Example 13
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void test_Initial_Set() {
  Cache<Integer, Integer> c = target.cache();
  EntryProcessor p = new EntryProcessor() {
    @Override
    public Object process(MutableCacheEntry e) throws Exception {
      e.setValue("dummy");
      return "abc";
    }
  };
  Object _result = c.invoke(123, p);
  assertEquals("abc", _result);
}
 
Example 14
Source File: ExceptionPropagatorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
Cache<Integer, Integer> prepCache() {
  Cache c = target.cache();
  c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      e.setException(new IllegalArgumentException("Test"));
      return null;
    }
  });
  assertTrue(c.containsKey(KEY));
  return c;
}
 
Example 15
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void initial_Return() {
  Cache<Integer, Integer> c = target.cache();
  EntryProcessor p = new EntryProcessor() {
    @Override
    public Object process(MutableCacheEntry e) throws Exception {
      return "abc";
    }
  };
  Object _result = c.invoke(123, p);
  assertEquals("abc", _result);
}
 
Example 16
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Test that exceptions get propagated, otherwise we cannot use assert inside the processor.
 */
@Test(expected = EntryProcessingException.class)
public void exceptionPropagation() {
  Cache<Integer, Integer> c = target.cache();
  c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      throw new IllegalStateException("test");
    }
  });
}
 
Example 17
Source File: SlowEntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Modification timestamp should be set to the time just before the processor
 * was invoked. Needs some reorganization in the time handling of entry action.
 */
@Test @Ignore("TODO: SlowEntryProcessorTest.modificationTime_reference_before_invoke")
public void modificationTime_reference_before_invoke() {
  Cache<Integer, Integer> c = target.cache();
  final long t0 = System.currentTimeMillis();
  c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
    @Override
    public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      e.setValue((int) (System.currentTimeMillis() - t0));
      Thread.sleep(1);
      return null;
    }
  });
  assertThat(c.peekEntry(KEY).getLastModification(), Matchers.lessThanOrEqualTo((t0 + c.peek(KEY))));
}
 
Example 18
Source File: EntryProcessorTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void intial_otherResult() {
  Cache<Integer, Integer> c = target.cache();
  EntryProcessor p = new EntryProcessor() {
    @Override
    public Object process(MutableCacheEntry e) throws Exception {
      return null;
    }
  };
  Object _result = c.invoke(123, p);
  assertNull(_result);
}
 
Example 19
Source File: BasicCacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
void checkExistingAndTimeStampGreaterOrEquals(Cache<Integer, Integer> c, int key, final long t) {
  assertTrue(c.containsKey(key));
  c.invoke(key, new EntryProcessor<Integer, Integer, Long>() {
    @Override
    public Long process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
      assertTrue("entry present", e.exists());
      assertThat(e.getRefreshedTime(), greaterThanOrEqualTo(t));
      return null;
    }
  });
}
 
Example 20
Source File: AllMutatorsExpireTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
int mutate(final int variant, final Cache<Integer, Integer> c) {
  int opCnt = 1;
  switch (variant) {
    case 0:
      c.put(KEY, VALUE);
      break;
    case 1:
      c.putIfAbsent(KEY, VALUE);
      break;
    case 2:
      c.peekAndPut(KEY, VALUE);
      break;
    case 3:
      c.put(1,1);
      c.peekAndReplace(KEY, VALUE);
      opCnt++;
      break;
    case 4:
      c.put(KEY, VALUE);
      c.replace(KEY, VALUE);
      opCnt++;
      break;
    case 5:
      c.put(KEY, VALUE);
      c.replaceIfEquals(KEY, VALUE, VALUE);
      opCnt++;
      break;
    case 6:
      c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
        @Override
        public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
          e.setValue(VALUE);
          return null;
        }
      });
      break;
    case 7:
      c.put(KEY, VALUE);
      c.put(KEY, VALUE);
      opCnt++;
      break;
  }
  return opCnt;
}