Example usage for com.google.common.cache CacheBuilder recordStats

List of usage examples for com.google.common.cache CacheBuilder recordStats

Introduction

In this page you can find the example usage for com.google.common.cache CacheBuilder recordStats.

Prototype

public CacheBuilder<K, V> recordStats() 

Source Link

Document

Enable the accumulation of CacheStats during the operation of the cache.

Usage

From source file:ca.exprofesso.guava.jcache.GuavaCache.java

public GuavaCache(String cacheName, CompleteConfiguration<K, V> configuration, CacheManager cacheManager) {
    this.cacheName = cacheName;
    this.configuration = configuration;
    this.cacheManager = cacheManager;

    String properties = cacheManager.getProperties().toString();

    CacheBuilderSpec cacheBuilderSpec = CacheBuilderSpec
            .parse(properties.substring(1, properties.length() - 1));

    CacheBuilder cacheBuilder = CacheBuilder.from(cacheBuilderSpec);

    ExpiryPolicy expiryPolicy = configuration.getExpiryPolicyFactory().create();

    if (expiryPolicy instanceof ModifiedExpiryPolicy) // == Guava expire after write
    {/*www .j ava  2s.c o m*/
        Duration d = expiryPolicy.getExpiryForUpdate();

        cacheBuilder.expireAfterWrite(d.getDurationAmount(), d.getTimeUnit());
    } else if (expiryPolicy instanceof TouchedExpiryPolicy) // == Guava expire after access
    {
        Duration d = expiryPolicy.getExpiryForAccess();

        cacheBuilder.expireAfterAccess(d.getDurationAmount(), d.getTimeUnit());
    }

    this.cacheEntryListenerConfigurations = Sets
            .newHashSet(configuration.getCacheEntryListenerConfigurations());

    if (!this.cacheEntryListenerConfigurations.isEmpty()) {
        cacheBuilder = cacheBuilder.removalListener(this);
    }

    if (configuration.isManagementEnabled()) {
        GuavaCacheMXBean bean = new GuavaCacheMXBean(this);

        try {
            ManagementFactory.getPlatformMBeanServer().registerMBean(bean,
                    new ObjectName(bean.getObjectName()));
        } catch (OperationsException | MBeanException e) {
            throw new CacheException(e);
        }
    }

    if (configuration.isStatisticsEnabled()) {
        GuavaCacheStatisticsMXBean bean = new GuavaCacheStatisticsMXBean(this);

        try {
            ManagementFactory.getPlatformMBeanServer().registerMBean(bean,
                    new ObjectName(bean.getObjectName()));
        } catch (OperationsException | MBeanException e) {
            throw new CacheException(e);
        }

        cacheBuilder.recordStats();
    }

    if (configuration.isReadThrough()) {
        Factory<CacheLoader<K, V>> factory = configuration.getCacheLoaderFactory();

        this.cache = (Cache<K, V>) cacheBuilder.build(new GuavaCacheLoader<>(factory.create()));
    } else {
        this.cache = (Cache<K, V>) cacheBuilder.build();
    }

    this.view = cache.asMap();
}