Java Code Examples for org.ehcache.config.builders.ExpiryPolicyBuilder#timeToLiveExpiration()

The following examples show how to use org.ehcache.config.builders.ExpiryPolicyBuilder#timeToLiveExpiration() . 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: CoreCacheConfigurationParser.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "deprecation"})
private static ExpiryPolicy<? super Object, ? super Object> getExpiry(ClassLoader cacheClassLoader, Expiry parsedExpiry)
  throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  if (parsedExpiry.isUserDef()) {
    try {
      return getInstanceOfName(parsedExpiry.type(), cacheClassLoader, ExpiryPolicy.class);
    } catch (ClassCastException e) {
      return ExpiryUtils.convertToExpiryPolicy(getInstanceOfName(parsedExpiry.type(), cacheClassLoader, org.ehcache.expiry.Expiry.class));
    }
  } else if (parsedExpiry.isTTL()) {
    return ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(parsedExpiry.value(), parsedExpiry.unit()));
  } else if (parsedExpiry.isTTI()) {
    return ExpiryPolicyBuilder.timeToIdleExpiration(Duration.of(parsedExpiry.value(), parsedExpiry.unit()));
  } else {
    return ExpiryPolicyBuilder.noExpiration();
  }
}
 
Example 2
Source File: CacheManagerProvider.java    From TweetwallFX with MIT License 5 votes vote down vote up
private static ExpiryPolicy<Object, Object> createExpiryPolicy(final CacheSettings.CacheExpiry cacheExpiry) {
    switch (cacheExpiry.getType()) {
        case NONE:
            return ExpiryPolicyBuilder.noExpiration();
        case TIME_TO_IDLE:
            return ExpiryPolicyBuilder.timeToIdleExpiration(cacheExpiry.produceDuration());
        case TIME_TO_LIVE:
            return ExpiryPolicyBuilder.timeToLiveExpiration(cacheExpiry.produceDuration());
        default:
            throw new IllegalStateException(CacheExpiryType.class.getSimpleName() + " '" + cacheExpiry.getType() + "' is not supported!");
    }
}
 
Example 3
Source File: XmlConfigurationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiryIsParsed() throws Exception {
  URL resource = XmlConfigurationTest.class.getResource("/configs/expiry-caches.xml");
  final XmlConfiguration xmlConfiguration = new XmlConfiguration(resource);

  ExpiryPolicy<?, ?> expiry = xmlConfiguration.getCacheConfigurations().get("none").getExpiryPolicy();
  ExpiryPolicy<?, ?> value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = xmlConfiguration.getCacheConfigurations().get("notSet").getExpiryPolicy();
  value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = xmlConfiguration.getCacheConfigurations().get("class").getExpiryPolicy();
  assertThat(expiry, CoreMatchers.instanceOf(com.pany.ehcache.MyExpiry.class));

  expiry = xmlConfiguration.getCacheConfigurations().get("deprecatedClass").getExpiryPolicy();
  assertThat(expiry.getExpiryForCreation(null, null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForAccess(null, () -> null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForUpdate(null, () -> null, null), is(Duration.ofSeconds(42)));

  expiry = xmlConfiguration.getCacheConfigurations().get("tti").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(500));
  assertThat(expiry, equalTo(value));

  expiry = xmlConfiguration.getCacheConfigurations().get("ttl").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(30));
  assertThat(expiry, equalTo(value));
}
 
Example 4
Source File: CoreCacheConfigurationParserTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void parseConfigurationExpiryPolicy() throws Exception {
  Configuration configuration = new XmlConfiguration(getClass().getResource("/configs/expiry-caches.xml"));

  ExpiryPolicy<?, ?> expiry = configuration.getCacheConfigurations().get("none").getExpiryPolicy();
  ExpiryPolicy<?, ?> value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = configuration.getCacheConfigurations().get("notSet").getExpiryPolicy();
  value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = configuration.getCacheConfigurations().get("class").getExpiryPolicy();
  assertThat(expiry, CoreMatchers.instanceOf(com.pany.ehcache.MyExpiry.class));

  expiry = configuration.getCacheConfigurations().get("deprecatedClass").getExpiryPolicy();
  assertThat(expiry.getExpiryForCreation(null, null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForAccess(null, () -> null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForUpdate(null, () -> null, null), is(Duration.ofSeconds(42)));

  expiry = configuration.getCacheConfigurations().get("tti").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(500));
  assertThat(expiry, equalTo(value));

  expiry = configuration.getCacheConfigurations().get("ttl").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(30));
  assertThat(expiry, equalTo(value));
}
 
Example 5
Source File: ClusteredStoreEventsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
  UnitTestConnectionService.add(
      CLUSTER_URI,
      new UnitTestConnectionService.PassthroughServerBuilder().resource("defaultResource", 8, MemoryUnit.MB).build()
  );

  Connection connection = new UnitTestConnectionService().connect(CLUSTER_URI, new Properties());
  ClusterTierManagerClientEntityFactory entityFactory = new ClusterTierManagerClientEntityFactory(connection, Runnable::run);

  ServerSideConfiguration serverConfig =
      new ServerSideConfiguration("defaultResource", Collections.emptyMap());
  entityFactory.create("TestCacheManager", serverConfig);

  ClusteredResourcePool resourcePool = ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB);
  ServerStoreConfiguration serverStoreConfiguration = new ServerStoreConfiguration(resourcePool.getPoolAllocation(),
    Long.class.getName(), String.class.getName(), LongSerializer.class.getName(), StringSerializer.class.getName(), null, false);
  ClusterTierClientEntity clientEntity = entityFactory.fetchOrCreateClusteredStoreEntity("TestCacheManager", CACHE_IDENTIFIER, serverStoreConfiguration, ClusteringServiceConfiguration.ClientMode.AUTO_CREATE, false);
  clientEntity.validate(serverStoreConfiguration);
  ServerStoreProxy serverStoreProxy = new CommonServerStoreProxy(CACHE_IDENTIFIER, clientEntity, mock(ServerCallback.class));

  testTimeSource = new TestTimeSource();

  codec = new OperationsCodec<>(new LongSerializer(), new StringSerializer());
  ChainResolver<Long, String> resolver = new ExpiryChainResolver<>(codec, ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(1000)));

  StoreEventDispatcher<Long, String> storeEventDispatcher = mock(StoreEventDispatcher.class);
  storeEventSink = mock(StoreEventSink.class);
  when(storeEventDispatcher.eventSink()).thenReturn(storeEventSink);

  ClusteredStore<Long, String> store = new ClusteredStore<>(config, codec, resolver, serverStoreProxy, testTimeSource, storeEventDispatcher, new DefaultStatisticsService());
  serverCallback = new ClusteredStore.Provider().getServerCallback(store);
}
 
Example 6
Source File: ClusteredCacheExpirationTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private ExpiryPolicy<Object, Object> oneSecondExpiration() {
  return ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(1));
}
 
Example 7
Source File: EHCacheXKMSClientCache.java    From cxf with Apache License 2.0 4 votes vote down vote up
public EHCacheXKMSClientCache(Bus cxfBus, Path diskstorePath, long diskSize,
                              long heapEntries, boolean persistent) throws XKMSClientCacheException {
    // Do some sanity checking on the arguments
    if (persistent && diskstorePath == null) {
        throw new NullPointerException();
    }
    if (diskstorePath != null && (diskSize < 5 || diskSize > 10000)) {
        throw new IllegalArgumentException("The diskSize parameter must be between 5 and 10000 (megabytes)");
    }
    if (heapEntries < 100) {
        throw new IllegalArgumentException("The heapEntries parameter must be greater than 100 (entries)");
    }

    if (cxfBus == null) {
        cxfBus = BusFactory.getThreadDefaultBus(true);
    }
    if (cxfBus != null) {
        cxfBus.getExtension(BusLifeCycleManager.class).registerLifeCycleListener(this);
    }

    this.bus = cxfBus;
    this.diskstorePath = diskstorePath;
    this.persistent = persistent;

    cacheKey = UUID.randomUUID().toString();

    ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder()
            .heap(heapEntries, EntryUnit.ENTRIES);
    if (diskstorePath != null) {
        resourcePoolsBuilder = resourcePoolsBuilder.disk(diskSize, MemoryUnit.MB, persistent);
    }

    ExpiryPolicy<Object, Object> expiryPolicy =
            ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(3600, ChronoUnit.SECONDS));
    CacheConfigurationBuilder<String, XKMSCacheToken> configurationBuilder =
            CacheConfigurationBuilder.newCacheConfigurationBuilder(
                    String.class, XKMSCacheToken.class, resourcePoolsBuilder)
                    .withExpiry(expiryPolicy);

    if (diskstorePath != null) {
        cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .with(CacheManagerBuilder.persistence(diskstorePath.toFile()))
                .withCache(cacheKey, configurationBuilder)
                .build();
    } else {
        cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache(cacheKey, configurationBuilder)
                .build();
    }

    cacheManager.init();
    cache = cacheManager.getCache(cacheKey, String.class, XKMSCacheToken.class);

}