Java Code Examples for org.ehcache.core.spi.service.StatisticsService

Following code examples demonstrate how to use org.ehcache.core.spi.service.StatisticsServicefrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.core.spi.service.StatisticsServiceand various code implementation of this class.

  public StatisticsService create(ServiceCreationConfiguration<StatisticsService> configuration) {
    if (configuration != null && !(configuration instanceof StatisticsServiceConfiguration)) {
      throw new IllegalArgumentException("Expected a configuration of type StatisticsServiceConfiguration but got " + configuration.getClass().getSimpleName());
    }
    if (configuration == null) {
      configuration = new DefaultStatisticsServiceConfiguration();
    }
    return new DefaultStatisticsService((StatisticsServiceConfiguration) configuration);
  } 


  public void createNoConfig() {
    StatisticsServiceConfiguration configuration = new DefaultStatisticsServiceConfiguration();
    StatisticsService statisticsService = factory.create(null);
    assertThat(statisticsService.getConfiguration().getDefaultHistogramWindow()).isEqualTo(configuration.getDefaultHistogramWindow());
  } 

  public void createWithConfig() {
    StatisticsService statisticsService = factory.create(new DefaultStatisticsServiceConfiguration()
      .withDefaultHistogramWindow(Duration.ofHours(1)));
    assertThat(statisticsService.getConfiguration().getDefaultHistogramWindow()).isEqualTo(Duration.ofHours(1));
  } 

  StandardEhcacheStatistics(ManagementRegistryServiceConfiguration registryConfiguration, CacheBinding cacheBinding, StatisticsService statisticsService, TimeSource timeSource) {
    super(registryConfiguration, cacheBinding);
    this.statisticRegistry = new StatisticRegistry(cacheBinding.getCache(), timeSource::getTimeMillis);

    String cacheName = cacheBinding.getAlias();
    CacheStatistics cacheStatistics = statisticsService.getCacheStatistics(cacheName);
    cacheStatistics.getKnownStatistics().forEach(statisticRegistry::registerStatistic);
  } 

  Eh107CacheStatisticsMXBean(String cacheName, URI cacheManagerURI, StatisticsService statisticsService) {
    super(cacheName, cacheManagerURI, "CacheStatistics");
    this.cacheName = cacheName;
    this.statisticsService = statisticsService;
  } 

  public EhcacheStatisticsProvider(ManagementRegistryServiceConfiguration configuration, StatisticsService statisticsService, TimeSource timeSource) {
    super(configuration);
    this.statisticsService = Objects.requireNonNull(statisticsService);
    this.timeSource = Objects.requireNonNull(timeSource);
  } 

    public void test() throws IOException {
      StatisticsService statisticsService = new DefaultStatisticsService();

      try(PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .with(CacheManagerBuilder.persistence("myData"))
        .using(statisticsService)
        .withCache("threeTieredCache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
              .heap(10, EntryUnit.ENTRIES)
              .offheap(1, MemoryUnit.MB)
              .disk(20, MemoryUnit.MB, true)
          )
        ).build(true)) {

        Cache<Long, String> cache = persistentCacheManager.getCache("threeTieredCache", Long.class, String.class);
        for(long i = 0; i < 1000; i++) {
          cache.put(i, "test");
        }
        System.out.println("Length: " + getFolderSize("mydata"));
        TierStatistics tierStatistics = statisticsService
          .getCacheStatistics("threeTieredCache")
          .getTierStatistics()
          .get("Disk");
        System.out.println("Occupied: " + tierStatistics.getOccupiedByteSize());
        System.out.println("Allocated: " + tierStatistics.getAllocatedByteSize());
      }
    } 

    public EhCacheFacade(Class<K> keyClass, Class<V> valueClass, int maxSize) {
        statisticsService = new DefaultStatisticsService();

        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .using(statisticsService)
                .build();
        cacheManager.init();

        this.cache = cacheManager.createCache(
                "VCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(keyClass, valueClass, ResourcePoolsBuilder.heap(maxSize))
        );

    } 

    public static void main(String[] args) {
        StatisticsService statisticsService = new DefaultStatisticsService();
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache("preConfigured",
                        CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
                                String.class, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES)))
                .using(statisticsService).build(true);

        Cache<Long, String> aCache = cacheManager.getCache("preConfigured", Long.class, String.class);
        aCache.put(1L, "one");
        aCache.put(0L, "zero");
        aCache.get(1L);
        aCache.get(0L);
        aCache.get(0L);
        aCache.get(0L);
        aCache.get(7L);


        CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("preConfigured");
        System.out.println(ehCacheStat.getCacheHitPercentage());
        System.out.println(ehCacheStat.getCacheGets());

    } 

Advertisement
Javadoc
Service providing raw statistics for cache and tier usage.
Advertisement