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

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

Introduction

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

Prototype

public <K1 extends K, V1 extends V> Cache<K1, V1> build() 

Source Link

Document

Builds a cache which does not automatically load values when keys are requested.

Usage

From source file:org.opencms.workplace.CmsWorkplaceManager.java

/**
 * Creates a new instance for the workplace manager, will be called by the workplace configuration manager.<p>
 *///from w ww .  j  a  v  a 2  s.c om
public CmsWorkplaceManager() {

    if (CmsLog.INIT.isInfoEnabled()) {
        CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WORKPLACE_INITIALIZE_START_0));
    }

    m_locales = new ArrayList<Locale>();
    m_labelSiteFolders = new ArrayList<String>();
    m_localizedFolders = new ArrayList<String>();
    m_autoLockResources = true;
    m_categoryFolder = CmsCategoryService.REPOSITORY_BASE_FOLDER;
    m_xmlContentAutoCorrect = true;
    m_showUserGroupIcon = true;
    m_dialogHandler = new HashMap<String, I_CmsDialogHandler>();
    m_views = new ArrayList<CmsWorkplaceView>();
    m_exportPoints = new HashSet<CmsExportPoint>();
    m_editorHandler = new CmsEditorHandler();
    m_fileMaxUploadSize = -1;
    m_fileViewSettings = new CmsRfsFileViewer();
    m_explorerTypeSettingsFromXml = new ArrayList<CmsExplorerTypeSettings>();
    m_explorerTypeSettingsFromModules = new ArrayList<CmsExplorerTypeSettings>();
    m_defaultPropertiesOnStructure = true;
    m_enableAdvancedPropertyTabs = true;
    m_defaultUserSettings = new CmsDefaultUserSettings();
    m_defaultAccess = new CmsExplorerTypeAccess();
    m_galleries = new HashMap<String, A_CmsAjaxGallery>();
    m_menuRules = new ArrayList<CmsMenuRule>();
    m_menuRulesMap = new HashMap<String, CmsMenuRule>();
    flushMessageCache();
    m_multiContextMenu = new CmsExplorerContextMenu();
    m_multiContextMenu.setMultiMenu(true);
    m_preEditorConditionDefinitions = new ArrayList<I_CmsPreEditorActionDefinition>();
    m_editorCssHandlers = new ArrayList<I_CmsEditorCssHandler>();
    m_customFoot = new CmsWorkplaceCustomFoot();
    m_synchronizeExcludePatterns = new ArrayList<Pattern>();

    // important to set this to null to avoid unnecessary overhead during configuration phase
    m_explorerTypeSettings = null;
    CacheBuilder<Object, Object> cb = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.MINUTES)
            .concurrencyLevel(3);
    m_workplaceServerUserChecks = cb.build();
}

From source file:org.geowebcache.storage.blobstore.memory.guava.GuavaCacheProvider.java

/**
 * This method is used for creating a new cache object, from the defined configuration.
 * //from   www  .j  a  v a 2  s.c o  m
 * @param configuration
 */
private void initCache(CacheConfiguration configuration) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Building new Cache");
    }
    // Initialization step
    int concurrency = configuration.getConcurrencyLevel();
    maxMemory = configuration.getHardMemoryLimit() * BYTES_TO_MB;
    long evictionTime = configuration.getEvictionTime();
    EvictionPolicy policy = configuration.getPolicy();

    // If Cache already exists, flush it
    if (cache != null) {
        cache.invalidateAll();
    }
    // Create the CacheBuilder
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    // Add weigher
    Weigher<String, TileObject> weigher = new Weigher<String, TileObject>() {

        @Override
        public int weigh(String key, TileObject value) {
            currentSize.addAndGet(value.getBlobSize());
            return value.getBlobSize();
        }
    };
    // Create the builder
    CacheBuilder<String, TileObject> newBuilder = builder.maximumWeight(maxMemory).recordStats()
            .weigher(weigher).concurrencyLevel(concurrency)
            .removalListener(new RemovalListener<String, TileObject>() {

                @Override
                public void onRemoval(RemovalNotification<String, TileObject> notification) {
                    // TODO This operation is not atomic
                    TileObject obj = notification.getValue();
                    // Update the current size
                    currentSize.addAndGet(-obj.getBlobSize());
                    final String tileKey = generateTileKey(obj);
                    final String layerName = obj.getLayerName();
                    multimap.removeTile(layerName, tileKey);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Removed tile " + tileKey + " for layer " + layerName + " due to reason:"
                                + notification.getCause().toString());
                        LOGGER.debug("Removed tile was evicted? " + notification.wasEvicted());
                    }
                }
            });
    // Handle eviction policy
    boolean configuredPolicy = false;
    if (policy != null && evictionTime > 0) {
        if (policy == EvictionPolicy.EXPIRE_AFTER_ACCESS) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Configuring Expire After Access eviction policy");
            }
            newBuilder.expireAfterAccess(evictionTime, TimeUnit.SECONDS);
            configuredPolicy = true;
        } else if (policy == EvictionPolicy.EXPIRE_AFTER_WRITE) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Configuring Expire After Write eviction policy");
            }
            newBuilder.expireAfterWrite(evictionTime, TimeUnit.SECONDS);
            configuredPolicy = true;
        }
    }

    // Build the cache
    cache = newBuilder.build();

    // Created a new multimap
    multimap = new LayerMap();

    // Configure a new scheduling task if needed
    if (configuredPolicy) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Configuring Scheduled Task for cache eviction");
        }
        Runnable command = new Runnable() {

            @Override
            public void run() {
                if (configured.get()) {
                    // Increment the number of current operations
                    // This behavior is used in order to wait
                    // the end of all the operations after setting
                    // the configured parameter to false
                    actualOperations.incrementAndGet();
                    try {
                        cache.cleanUp();
                    } finally {
                        // Decrement the number of current operations.
                        actualOperations.decrementAndGet();
                    }
                }
            }
        };
        // Initialization of the internal Scheduler task for scheduling cache cleanup
        scheduledPool = Executors.newScheduledThreadPool(CORE_POOL_SIZE);
        scheduledPool.scheduleAtFixedRate(command, 10, evictionTime + 1, TimeUnit.SECONDS);
    }

    // Update the configured parameter
    configured.getAndSet(true);
}