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

The following examples show how to use org.ehcache.config.builders.ExpiryPolicyBuilder#noExpiration() . 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: XAStoreTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  transactionManagerWrapper = new TransactionManagerWrapper(testTransactionManager, new NullXAResourceRegistry());
  classLoader = ClassLoader.getSystemClassLoader();
  keySerializer = new JavaSerializer<>(classLoader);
  valueSerializer = new JavaSerializer<>(classLoader);
  CopyProvider copyProvider = new DefaultCopyProvider(new DefaultCopyProviderConfiguration());
  keyCopier = copyProvider.createKeyCopier(Long.class, keySerializer);
  valueCopier = copyProvider.createValueCopier(valueClass, valueSerializer);
  Store.Configuration<Long, SoftLock<String>> onHeapConfig = new StoreConfigurationImpl<>(Long.class, valueClass,
    null, classLoader, ExpiryPolicyBuilder.noExpiration(), ResourcePoolsBuilder.newResourcePoolsBuilder()
    .heap(10, EntryUnit.ENTRIES)
    .build(),
    0, keySerializer, valueSerializer);
  testTimeSource = new TestTimeSource();
  eventDispatcher = NullStoreEventDispatcher.nullStoreEventDispatcher();
  onHeapStore = new OnHeapStore<>(onHeapConfig, testTimeSource, keyCopier, valueCopier, new NoopSizeOfEngine(), eventDispatcher, new DefaultStatisticsService());
  journal = new TransientJournal<>();
}
 
Example 3
Source File: OnHeapStoreEvictionTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvictionCandidateLimits() throws Exception {
  TestTimeSource timeSource = new TestTimeSource();
  StoreConfigurationImpl<String, String> configuration = new StoreConfigurationImpl<>(
    String.class, String.class, noAdvice(),
    getClass().getClassLoader(), ExpiryPolicyBuilder.noExpiration(), heap(1).build(), 1, null, null);
  TestStoreEventDispatcher<String, String> eventDispatcher = new TestStoreEventDispatcher<>();
  final String firstKey = "daFirst";
  eventDispatcher.addEventListener(event -> {
    if (event.getType().equals(EventType.EVICTED)) {
      assertThat(event.getKey(), is(firstKey));
    }
  });
  OnHeapStore<String, String> store = new OnHeapStore<>(configuration, timeSource,
    new IdentityCopier<>(), new IdentityCopier<>(), new NoopSizeOfEngine(), eventDispatcher, new DefaultStatisticsService());
  timeSource.advanceTime(10000L);
  store.put(firstKey, "daValue");
  timeSource.advanceTime(10000L);
  store.put("other", "otherValue");
}
 
Example 4
Source File: TieredStoreMutatorTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  // Not relevant to the test, just used to instantiate the OnHeapStore
  ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
    .heap(1, MemoryUnit.MB)
    .disk(1, MemoryUnit.GB, false)
  .build();

  // Not relevant to the test, just used to instantiate the OnHeapStore
  Store.Configuration<String, String> config = new StoreConfigurationImpl<>(String.class, String.class,
    null, getClass().getClassLoader(), ExpiryPolicyBuilder.noExpiration(), resourcePools, 0, null, null);

  // Here again, all parameters are useless, we only care about the beforeCompletingTheFault implementation
  CachingTier<String, String> cachingTier = new OnHeapStore<>(config, SystemTimeSource.INSTANCE,
    StringCopier.copier(), StringCopier.copier(), new NoopSizeOfEngine(), NullStoreEventDispatcher.
    <String, String>nullStoreEventDispatcher(), new DefaultStatisticsService());

  tieredStore = new TieredStore<>(cachingTier, authoritativeTier);
}
 
Example 5
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 6
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 7
Source File: XAStoreTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomEvictionAdvisor() throws Exception {
  final AtomicBoolean invoked = new AtomicBoolean();

  EvictionAdvisor<Long, SoftLock<String>> evictionAdvisor = (key, value) -> {
    invoked.set(true);
    return false;
  };
  Store.Configuration<Long, SoftLock<String>> onHeapConfig = new StoreConfigurationImpl<>(Long.class, valueClass,
    evictionAdvisor, classLoader, ExpiryPolicyBuilder.noExpiration(), ResourcePoolsBuilder.newResourcePoolsBuilder()
    .heap(10, EntryUnit.ENTRIES)
    .build(),
    0, keySerializer, valueSerializer);
  OnHeapStore<Long, SoftLock<String>> onHeapStore = new OnHeapStore<>(onHeapConfig, testTimeSource, keyCopier, valueCopier, new NoopSizeOfEngine(), eventDispatcher, new DefaultStatisticsService());

  final XAStore<Long, String> xaStore = getXAStore(onHeapStore);

  testTransactionManager.begin();
  {
    xaStore.put(1L, "1");
  }
  testTransactionManager.rollback();
  assertThat(invoked.get(), is(false));

  testTransactionManager.begin();
  {
    xaStore.put(1L, "1");
  }
  testTransactionManager.commit();
  assertThat(invoked.get(), is(true));
}
 
Example 8
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 9
Source File: ClusteredStoreProviderTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private Store.Configuration<Long, String> getStoreConfig() {
  return new Store.Configuration<Long, String>() {
    @Override
    public Class<Long> getKeyType() {
      return Long.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public EvictionAdvisor<? super Long, ? super String> getEvictionAdvisor() {
      return Eviction.noAdvice();
    }

    @Override
    public ClassLoader getClassLoader() {
      return getClass().getClassLoader();
    }

    @Override
    public ExpiryPolicy<? super Long, ? super String> getExpiry() {
      return ExpiryPolicyBuilder.noExpiration();
    }

    @Override
    @SuppressWarnings("unchecked")
    public ResourcePools getResourcePools() {
      Map<ClusteredResourceType<DedicatedClusteredResourcePool>, DedicatedClusteredResourcePoolImpl> poolMap = Collections
          .singletonMap(ClusteredResourceType.Types.DEDICATED, new DedicatedClusteredResourcePoolImpl("test", 10, MemoryUnit.MB));
      return new ResourcePoolsImpl((Map) poolMap);
    }

    @Override
    public Serializer<Long> getKeySerializer() {
      return new LongSerializer();
    }

    @Override
    public Serializer<String> getValueSerializer() {
      return new StringSerializer();
    }

    @Override
    public int getDispatcherConcurrency() {
      return 1;
    }

    @Override
    public CacheLoaderWriter<? super Long, String> getCacheLoaderWriter() {
      return null;
    }
  };
}
 
Example 10
Source File: OffHeapDiskStoreProviderTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private Store.Configuration<Long, String> getStoreConfig() {
  return new Store.Configuration<Long, String>() {
    @Override
    public Class<Long> getKeyType() {
      return Long.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public EvictionAdvisor<? super Long, ? super String> getEvictionAdvisor() {
      return Eviction.noAdvice();
    }

    @Override
    public ClassLoader getClassLoader() {
      return getClass().getClassLoader();
    }

    @Override
    public ExpiryPolicy<? super Long, ? super String> getExpiry() {
      return ExpiryPolicyBuilder.noExpiration();
    }

    @Override
    public ResourcePools getResourcePools() {
      return new ResourcePools() {
        @Override
        @SuppressWarnings("unchecked")
        public <P extends ResourcePool> P getPoolForResource(ResourceType<P> resourceType) {
          return (P) new SizedResourcePool() {
            @Override
            public ResourceType<SizedResourcePool> getType() {
              return ResourceType.Core.DISK;
            }

            @Override
            public long getSize() {
              return 1;
            }

            @Override
            public ResourceUnit getUnit() {
              return MemoryUnit.MB;
            }

            @Override
            public boolean isPersistent() {
              return false;
            }

            @Override
            public void validateUpdate(ResourcePool newPool) {
              throw new UnsupportedOperationException("TODO Implement me!");
            }
          };
        }

        @Override
        @SuppressWarnings("unchecked")
        public Set<ResourceType<?>> getResourceTypeSet() {
          return (Set) singleton(ResourceType.Core.OFFHEAP);
        }

        @Override
        public ResourcePools validateAndMerge(ResourcePools toBeUpdated) throws IllegalArgumentException, UnsupportedOperationException {
          throw new UnsupportedOperationException("TODO Implement me!");
        }
      };
    }

    @Override
    public Serializer<Long> getKeySerializer() {
      return new LongSerializer();
    }

    @Override
    public Serializer<String> getValueSerializer() {
      return new StringSerializer();
    }

    @Override
    public int getDispatcherConcurrency() {
      return 1;
    }

    @Override
    public CacheLoaderWriter<? super Long, String> getCacheLoaderWriter() {
      return null;
    }

  };
}
 
Example 11
Source File: CompoundCachingTierSPITest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  cachingTierFactory = new CachingTierFactory<String, String>() {

    @Override
    public CachingTier<String, String> newCachingTier() {
      return newCachingTier(null);
    }

    @Override
    public CachingTier<String, String> newCachingTier(long capacity) {
      return newCachingTier((Long) capacity);
    }

    private CachingTier<String, String> newCachingTier(Long capacity) {
      Store.Configuration<String, String> config = new StoreConfigurationImpl<>(getKeyType(), getValueType(), null,
        ClassLoader.getSystemClassLoader(), ExpiryPolicyBuilder.noExpiration(), buildResourcePools(capacity), 0, new JavaSerializer<>(getSystemClassLoader()), new JavaSerializer<>(getSystemClassLoader()));

      StoreEventDispatcher<String, String> eventDispatcher = NullStoreEventDispatcher.nullStoreEventDispatcher();
      OffHeapStore<String, String> offHeapStore = new OffHeapStore<>(config, SystemTimeSource.INSTANCE, eventDispatcher, 10 * 1024 * 1024, new DefaultStatisticsService());
      OffHeapStoreLifecycleHelper.init(offHeapStore);
      IdentityCopier<String> copier = new IdentityCopier<>();
      OnHeapStore<String, String> onHeapStore = new OnHeapStore<>(config, SystemTimeSource.INSTANCE, copier, copier, new NoopSizeOfEngine(), eventDispatcher, new DefaultStatisticsService());
      CompoundCachingTier<String, String> compoundCachingTier = new CompoundCachingTier<>(onHeapStore, offHeapStore);
      map.put(compoundCachingTier, offHeapStore);
      return compoundCachingTier;
    }

    @Override
    public Store.ValueHolder<String> newValueHolder(final String value) {
      throw new UnsupportedOperationException();
    }

    @Override
    public Store.Provider newProvider() {
      return new OnHeapStore.Provider();
    }

    private ResourcePools buildResourcePools(Comparable<Long> capacityConstraint) {
      if (capacityConstraint == null) {
        return newResourcePoolsBuilder().heap(Long.MAX_VALUE, EntryUnit.ENTRIES).offheap(1, MemoryUnit.MB).build();
      } else {
        return newResourcePoolsBuilder().heap((Long)capacityConstraint, EntryUnit.ENTRIES).offheap(1, MemoryUnit.MB).build();
      }
    }

    @Override
    public Class<String> getKeyType() {
      return String.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public ServiceConfiguration<?, ?>[] getServiceConfigurations() {
      return new ServiceConfiguration<?, ?>[0];
    }

    @Override
    public String createKey(long seed) {
      return "" + seed;
    }

    @Override
    public String createValue(long seed) {
      return "" + seed;
    }

    @Override
    public void disposeOf(CachingTier<String, String> tier) {
      OffHeapStore<?, ?> offHeapStore = map.remove(tier);
      OffHeapStoreLifecycleHelper.close(offHeapStore);
    }

    @Override
    public ServiceProvider<Service> getServiceProvider() {
      return dependencySet().build();
    }

  };
}
 
Example 12
Source File: OnHeapStoreCachingTierByRefSPITest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() {
  cachingTierFactory = new CachingTierFactory<String, String>() {

    @Override
    public CachingTier<String, String> newCachingTier() {
      return newCachingTier(null);
    }

    @Override
    public CachingTier<String, String> newCachingTier(long capacity) {
      return newCachingTier((Long) capacity);
    }

    private CachingTier<String, String> newCachingTier(Long capacity) {
      Store.Configuration<String, String> config = new StoreConfigurationImpl<>(getKeyType(), getValueType(), null,
        ClassLoader.getSystemClassLoader(), ExpiryPolicyBuilder.noExpiration(), buildResourcePools(capacity), 0, null, null);

      return new OnHeapStore<>(config, SystemTimeSource.INSTANCE, IdentityCopier.identityCopier(), IdentityCopier.identityCopier(),
        new NoopSizeOfEngine(), NullStoreEventDispatcher.nullStoreEventDispatcher(), new DefaultStatisticsService());
    }

    @Override
    public Store.ValueHolder<String> newValueHolder(final String value) {
      return new CopiedOnHeapValueHolder<>(value, SystemTimeSource.INSTANCE.getTimeMillis(), false, IdentityCopier.identityCopier());
    }

    @Override
    public Store.Provider newProvider() {
      return new OnHeapStore.Provider();
    }

    private ResourcePools buildResourcePools(Comparable<Long> capacityConstraint) {
      if (capacityConstraint == null) {
        return newResourcePoolsBuilder().heap(Long.MAX_VALUE, EntryUnit.ENTRIES).build();
      } else {
        return newResourcePoolsBuilder().heap((Long)capacityConstraint, EntryUnit.ENTRIES).build();
      }
    }

    @Override
    public Class<String> getKeyType() {
      return String.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public ServiceConfiguration<?, ?>[] getServiceConfigurations() {
      return new ServiceConfiguration<?, ?>[0];
    }

    @Override
    public String createKey(long seed) {
      return "" + seed;
    }

    @Override
    public String createValue(long seed) {
      return "" + seed;
    }

    @Override
    public void disposeOf(CachingTier<String, String> tier) {
    }

    @Override
    public ServiceProvider<Service> getServiceProvider() {
      return dependencySet().build();
    }

  };
}
 
Example 13
Source File: OnHeapStoreCachingTierByRefSPITest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() {
  cachingTierFactory = new CachingTierFactory<String, String>() {

    @Override
    public CachingTier<String, String> newCachingTier() {
      return newCachingTier(null);
    }

    @Override
    public CachingTier<String, String> newCachingTier(long capacity) {
      return newCachingTier((Long) capacity);
    }

    private CachingTier<String, String> newCachingTier(Long capacity) {
      Store.Configuration<String, String> config = new StoreConfigurationImpl<>(getKeyType(), getValueType(), null,
        ClassLoader.getSystemClassLoader(), ExpiryPolicyBuilder.noExpiration(), buildResourcePools(capacity), 0, null, null);

      return new OnHeapStore<>(config, SystemTimeSource.INSTANCE, IdentityCopier.identityCopier(), IdentityCopier.identityCopier(),
          new DefaultSizeOfEngine(Long.MAX_VALUE, Long.MAX_VALUE), NullStoreEventDispatcher.nullStoreEventDispatcher(), new DefaultStatisticsService());
    }

    @Override
    public Store.ValueHolder<String> newValueHolder(final String value) {
      return new CopiedOnHeapValueHolder<>(value, SystemTimeSource.INSTANCE.getTimeMillis(), false, IdentityCopier.identityCopier());
    }

    @Override
    public Store.Provider newProvider() {
      return new OnHeapStore.Provider();
    }

    private ResourcePools buildResourcePools(Comparable<Long> capacityConstraint) {
      if (capacityConstraint == null) {
        capacityConstraint = 10L;
      }
      return newResourcePoolsBuilder().heap((Long)capacityConstraint, MemoryUnit.MB).build();
    }

    @Override
    public Class<String> getKeyType() {
      return String.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public ServiceConfiguration<?, ?>[] getServiceConfigurations() {
      return new ServiceConfiguration<?, ?>[0];
    }

    @Override
    public String createKey(long seed) {
      return Long.toString(seed);
    }

    @Override
    public String createValue(long seed) {
      char[] chars = new char[600 * 1024];
      Arrays.fill(chars, (char) (0x1 + (seed & 0x7e)));
      return new String(chars);
    }

    @Override
    public void disposeOf(CachingTier<String, String> tier) {
    }

    @Override
    public ServiceProvider<Service> getServiceProvider() {
      return dependencySet().build();
    }

  };
}
 
Example 14
Source File: OnHeapStoreCachingTierByValueSPITest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  cachingTierFactory = new CachingTierFactory<String, String>() {

    final Serializer<String> defaultSerializer = new JavaSerializer<>(getClass().getClassLoader());
    final Copier<String> defaultCopier = new SerializingCopier<>(defaultSerializer);

    @Override
    public CachingTier<String, String> newCachingTier() {
      return newCachingTier(null);
    }

    @Override
    public CachingTier<String, String> newCachingTier(long capacity) {
      return newCachingTier((Long) capacity);
    }

    private CachingTier<String, String> newCachingTier(Long capacity) {
      Store.Configuration<String, String> config = new StoreConfigurationImpl<>(getKeyType(), getValueType(), null,
        ClassLoader.getSystemClassLoader(), ExpiryPolicyBuilder.noExpiration(), buildResourcePools(capacity), 0,
        new JavaSerializer<>(getSystemClassLoader()), new JavaSerializer<>(getSystemClassLoader()));
      return new OnHeapStore<>(config, SystemTimeSource.INSTANCE, defaultCopier, defaultCopier,
        new DefaultSizeOfEngine(Long.MAX_VALUE, Long.MAX_VALUE), NullStoreEventDispatcher.nullStoreEventDispatcher(), new DefaultStatisticsService());
    }

    @Override
    public Store.ValueHolder<String> newValueHolder(final String value) {
      return new SerializedOnHeapValueHolder<>(value, SystemTimeSource.INSTANCE.getTimeMillis(), false, defaultSerializer);
    }

    @Override
    public Store.Provider newProvider() {
      Store.Provider service = new OnHeapStore.Provider();
      service.start(dependencySet().build());
      return service;
    }

    private ResourcePools buildResourcePools(Comparable<Long> capacityConstraint) {
      if (capacityConstraint == null) {
        capacityConstraint = 10L;
      }
      return newResourcePoolsBuilder().heap((Long)capacityConstraint, MemoryUnit.MB).build();
    }

    @Override
    public Class<String> getKeyType() {
      return String.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public ServiceConfiguration<?, ?>[] getServiceConfigurations() {
      return new ServiceConfiguration<?, ?>[0];
    }

    @Override
    public String createKey(long seed) {
      return Long.toString(seed);
    }

    @Override
    public String createValue(long seed) {
      char[] chars = new char[600 * 1024];
      Arrays.fill(chars, (char) (0x1 + (seed & 0x7e)));
      return new String(chars);
    }

    @Override
    public void disposeOf(CachingTier<String, String> tier) {
    }

    @Override
    public ServiceProvider<Service> getServiceProvider() {
      return dependencySet().build();
    }

  };
}
 
Example 15
Source File: OnHeapStoreEvictionTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
protected <K, V> OnHeapStoreForTests<K, V> newStore(final TimeSource timeSource,
    final EvictionAdvisor<? super K, ? super V> evictionAdvisor) {
  return new OnHeapStoreForTests<>(new Store.Configuration<K, V>() {
    @SuppressWarnings("unchecked")
    @Override
    public Class<K> getKeyType() {
      return (Class<K>) String.class;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<V> getValueType() {
      return (Class<V>) Serializable.class;
    }

    @Override
    public EvictionAdvisor<? super K, ? super V> getEvictionAdvisor() {
      return evictionAdvisor;
    }

    @Override
    public ClassLoader getClassLoader() {
      return getClass().getClassLoader();
    }

    @Override
    public ExpiryPolicy<? super K, ? super V> getExpiry() {
      return ExpiryPolicyBuilder.noExpiration();
    }

    @Override
    public ResourcePools getResourcePools() {
      return newResourcePoolsBuilder().heap(500, MemoryUnit.B).build();
    }

    @Override
    public Serializer<K> getKeySerializer() {
      throw new AssertionError();
    }

    @Override
    public Serializer<V> getValueSerializer() {
      throw new AssertionError();
    }

    @Override
    public int getDispatcherConcurrency() {
      return 0;
    }

    @Override
    public CacheLoaderWriter<? super K, V> getCacheLoaderWriter() {
      return null;
    }
  }, timeSource, new DefaultSizeOfEngine(Long.MAX_VALUE, Long.MAX_VALUE));
}
 
Example 16
Source File: OnHeapStoreCachingTierByValueSPITest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  cachingTierFactory = new CachingTierFactory<String, String>() {

    final Serializer<String> defaultSerializer = new JavaSerializer<>(getClass().getClassLoader());
    final Copier<String> defaultCopier = new SerializingCopier<>(defaultSerializer);

    @Override
    public CachingTier<String, String> newCachingTier() {
      return newCachingTier(null);
    }

    @Override
    public CachingTier<String, String> newCachingTier(long capacity) {
      return newCachingTier((Long) capacity);
    }

    private CachingTier<String, String> newCachingTier(Long capacity) {
      Store.Configuration<String, String> config = new StoreConfigurationImpl<>(getKeyType(), getValueType(), null,
        ClassLoader.getSystemClassLoader(), ExpiryPolicyBuilder.noExpiration(), buildResourcePools(capacity), 0,
        new JavaSerializer<>(getSystemClassLoader()), new JavaSerializer<>(getSystemClassLoader()));

      return new OnHeapStore<>(config, SystemTimeSource.INSTANCE, defaultCopier, defaultCopier, new NoopSizeOfEngine(),
        NullStoreEventDispatcher.<String, String>nullStoreEventDispatcher(), new DefaultStatisticsService());
    }

    @Override
    public Store.ValueHolder<String> newValueHolder(final String value) {
      return new SerializedOnHeapValueHolder<>(value, SystemTimeSource.INSTANCE.getTimeMillis(), false, defaultSerializer);
    }

    @Override
    public Store.Provider newProvider() {
      Store.Provider service = new OnHeapStore.Provider();
      service.start(dependencySet().build());
      return service;
    }

    private ResourcePools buildResourcePools(Comparable<Long> capacityConstraint) {
      if (capacityConstraint == null) {
        return newResourcePoolsBuilder().heap(Long.MAX_VALUE, EntryUnit.ENTRIES).build();
      } else {
        return newResourcePoolsBuilder().heap((Long)capacityConstraint, EntryUnit.ENTRIES).build();
      }
    }

    @Override
    public Class<String> getKeyType() {
      return String.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public ServiceConfiguration<?, ?>[] getServiceConfigurations() {
      return new ServiceConfiguration<?, ?>[0];
    }

    @Override
    public String createKey(long seed) {
      return new String("" + seed);
    }

    @Override
    public String createValue(long seed) {
      return new String("" + seed);
    }

    @Override
    public void disposeOf(CachingTier<String, String> tier) {
    }

    @Override
    public ServiceProvider<Service> getServiceProvider() {
      return dependencySet().build();
    }

  };
}
 
Example 17
Source File: OnHeapStoreEvictionTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
protected <K, V> OnHeapStoreForTests<K, V> newStore(final TimeSource timeSource,
    final EvictionAdvisor<? super K, ? super V> evictionAdvisor) {
  return new OnHeapStoreForTests<>(new Store.Configuration<K, V>() {
    @SuppressWarnings("unchecked")
    @Override
    public Class<K> getKeyType() {
      return (Class<K>) String.class;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<V> getValueType() {
      return (Class<V>) Serializable.class;
    }

    @Override
    public EvictionAdvisor<? super K, ? super V> getEvictionAdvisor() {
      return evictionAdvisor;
    }

    @Override
    public ClassLoader getClassLoader() {
      return getClass().getClassLoader();
    }

    @Override
    public ExpiryPolicy<? super K, ? super V> getExpiry() {
      return ExpiryPolicyBuilder.noExpiration();
    }

    @Override
    public ResourcePools getResourcePools() {
      return newResourcePoolsBuilder().heap(1, EntryUnit.ENTRIES).build();
    }

    @Override
    public Serializer<K> getKeySerializer() {
      throw new AssertionError();
    }

    @Override
    public Serializer<V> getValueSerializer() {
      throw new AssertionError();
    }

    @Override
    public int getDispatcherConcurrency() {
      return 1;
    }

    @Override
    public CacheLoaderWriter<? super K, V> getCacheLoaderWriter() {
      return null;
    }
  }, timeSource);
}