Create a cache from cacheable sources
@param sources source
@return cache
189971
public static PoliciesCache fromSources(final Iterable<CacheableYamlSource> sources, final Set<Attribute> context) {
        return fromSourceProvider(
                new SourceProvider() {
                    @Override
                    public Iterator<CacheableYamlSource> getSourceIterator() {
                        return sources.iterator();
                    }
                },
                context
        );
    }
726304
public static <F, T> Function<F, T> cache(@javax.annotation.Nonnull final Function<F, T> inner) {
    @javax.annotation.Nonnull final LoadingCache<F, T> cache = CacheBuilder.newBuilder().build(new CacheLoader<F, T>() {
      @Override
      public T load(final F key) throws Exception {
        return inner.apply(key);
      }
    });
    return cache::apply;
  }
338616
private void rebuildProducerCache(Collection<IStatsProducer> producers){
		log.debug("Rebuilding producer cache with "+producers.size()+" producers.");
		log.debug("Following producers known: "+producers);

        synchronized(cacheLock){
			//lets create lists with more place to store as actually need, we will probably need it.
			final int approxSize = (int)(producers.size()*1.5);

            _cachedProducerList = new ArrayList<ProducerReference>(approxSize);
            _cachedProducerMap = new HashMap<String,ProducerReference>(approxSize);

            for (IStatsProducer sp : producers){
				final ProducerReference reference = new ProducerReference(sp);

                _cachedProducerList.add(reference);
                _cachedProducerMap.put(sp.getProducerId(), reference);
            }
		}

		if (log.isDebugEnabled()){
			log.debug("Cachedproducer list contains "+_cachedProducerList.size()+" producers: ");
			log.debug(String.valueOf(_cachedProducerList));
			log.debug("Cached producer map contains: "+_cachedProducerMap.size()+" producers");
			log.debug(String.valueOf(_cachedProducerMap));
		}
	}
1269630
function (namespaces) {
    _.forEach(namespaces, function (level, namespace) {
      cache.add(namespace, level);
    });
    return this;
  }
443489
@Override
    public <T> CachedResource<T> getResource(Resource resource, Loader<T> builder)
            throws IOException {
        return this.getResource(resource, builder, this.checkInterval);
    }
1335096
private CachedProviderServiceInstance getCachedServiceInstance(
            String serviceName, String providerAddress) {
        
		ServiceInstanceId id = new ServiceInstanceId(serviceName, providerAddress);
		return getCacheServiceInstances().get(id);
       
    }
248022
public Config setCacheConfigs(Map<String, CacheSimpleConfig> cacheConfigs) {
        this.cacheConfigs.clear();
        this.cacheConfigs.putAll(cacheConfigs);
        for (final Entry<String, CacheSimpleConfig> entry : this.cacheConfigs.entrySet()) {
            entry.getValue().setName(entry.getKey());
        }
        return this;
    }
1286960
public final Cache2kBuilder<K, V> with(ConfigurationSectionBuilder<? extends ConfigurationSection>... sectionBuilders) {
    for (ConfigurationSectionBuilder<? extends ConfigurationSection> b : sectionBuilders) {
      config().getSections().add(b.buildConfigurationSection());
    }
    return this;
  }
116857
public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) {
        return new CachingClassResolver(
                new ClassLoaderClassResolver(defaultClassLoader(classLoader)),
                new WeakReferenceMap<String, Class<?>>(
                        PlatformDependent.<String, Reference<Class<?>>>newConcurrentHashMap()));
    }
1141708
public static Cache createDefaultCache(Context context) {
    File dir = defaultCacheDir(context);
    return new Cache(dir, calculateDiskCacheSize(dir));
  }
459252
public static PoliciesCache fromSourceProvider(
            final SourceProvider provider,
            final Set<Attribute> forcedContext
    )
    {
        return new PoliciesCache(provider, forcedContext);
    }
59252
public final UserManagedCacheBuilder<K, V, T> withResourcePools(ResourcePools resourcePools) {
    UserManagedCacheBuilder<K, V, T> otherBuilder = new UserManagedCacheBuilder<>(this);
    otherBuilder.resourcePools = resourcePools;
    return otherBuilder;
  }
392389
public Object createObjectCache(String cacheName) throws DynamicCacheServiceNotStarted, IllegalStateException {
    	if (objectCacheUnit == null) {
    		throw new DynamicCacheServiceNotStarted("Object cache service has not been started."); 
    	}
    	return objectCacheUnit.createObjectCache(cacheName);
    }
20078
@Override
  public Iterable<K> keys() {
    return new Iterable<K>() {
      @Override
      public Iterator<K> iterator() {
        final Iterator<CacheEntry<K,V>> it = BaseCache.this.iterator();
        return new Iterator<K>() {
          @Override
          public boolean hasNext() {
            return it.hasNext();
          }

          @Override
          public K next() {
            return it.next().getKey();
          }

          @Override
          public void remove() {
            throw new UnsupportedOperationException();
          }
        };
      }
    };
  }
425576
public Reader createReader(byte[] cachedBytes) throws IOException {
	if (cachedBytes != null) {
	    return new InputStreamReader(new ByteArrayInputStream(cachedBytes));
	}
        if (inputStream != null) {
            return new InputStreamReader(inputStream, charset);
        }
        if (reader != null) {
            return reader;
        }
        return new InputStreamReader(new FileInputStream(file), charset);
    }
572007
protected void putIntoCache(List<HalResource<?>> notCachedResources) {
    Cache cache = getCache();
    for (HalResource<?> notCachedResource : notCachedResources) {
      cache.put(getResourceId(notCachedResource), notCachedResource);
    }
  }
1190542
@SuppressWarnings({"unchecked", "SuspiciousToArrayCall"})
  public Cache<K, V> buildAsIs() {
    if (config.getValueType() == null) {
      config.setValueType((Class<V>) Object.class);
    }
    if (config.getKeyType() == null) {
      config.setKeyType((Class<K>) Object.class);
    }
    if (config.getName() == null) {
      config.setName(deriveNameFromStackTrace());
    }
    checkConfiguration();
    Class<?> _implClass = HeapCache.class;
    Class<?> _keyType = config.getKeyType().getType();
    if (_keyType == Integer.class) {
      _implClass = IntHeapCache.class;
    } else if (_keyType == Long.class) {
      _implClass = LongHeapCache.class;
    }
    InternalCache<K, V> _cache = constructImplementationAndFillParameters(_implClass);
    InternalClock _timeReference = (InternalClock) _cache.createCustomization(config.getTimeReference());
    if (_timeReference == null) {
      _timeReference = ClockDefaultImpl.INSTANCE;
    }
    HeapCache bc = (HeapCache) _cache;
    bc.setCacheManager(manager);
    if (config.hasCacheClosedListeners()) {
      bc.setCacheClosedListeners(config.getCacheClosedListeners());
    }
    configureViaSettersDirect(bc);
    bc.setClock(_timeReference);

    if (config.isRefreshAhead() && !(
          config.getAsyncLoader() != null ||
          config.getLoader() != null ||
          config.getAdvancedLoader() != null)) {
      throw new IllegalArgumentException("refresh ahead enabled, but no loader defined");
    }

    boolean _wrap =
      config.getWeigher() != null ||
      config.hasListeners() ||
      config.hasAsyncListeners() ||
      config.getWriter() != null ||
      config.getAsyncLoader() != null;


    WiredCache<K, V> wc = null;
    if (_wrap) {
      if (_keyType == Integer.class) {
        wc = (WiredCache<K, V>) new IntWiredCache<V>();
      } else if (_keyType == Long.class) {
        wc = (WiredCache<K, V>) new LongWiredCache<V>();
      } else {
        wc = new WiredCache<K, V>();
      }
      wc.heapCache = bc;
      _cache = wc;
    }

    String _name = manager.newCache(_cache, bc.getName());
    bc.setName(_name);
    if (_wrap) {
      wc.loader = bc.loader;
      wc.writer = (CacheWriter<K, V>) bc.createCustomization(config.getWriter());
      wc.asyncLoader = (AsyncCacheLoader<K, V>) bc.createCustomization(config.getAsyncLoader());
      List<CacheEntryCreatedListener<K, V>> _syncCreatedListeners = new ArrayList<CacheEntryCreatedListener<K, V>>();
      List<CacheEntryUpdatedListener<K, V>> _syncUpdatedListeners = new ArrayList<CacheEntryUpdatedListener<K, V>>();
      List<CacheEntryRemovedListener<K, V>> _syncRemovedListeners = new ArrayList<CacheEntryRemovedListener<K, V>>();
      List<CacheEntryExpiredListener<K, V>> _syncExpiredListeners = new ArrayList<CacheEntryExpiredListener<K, V>>();
      List<CacheEntryEvictedListener<K, V>> _syncEvictedListeners = new ArrayList<CacheEntryEvictedListener<K,V>>();
      List<CacheEntryExpiredListener<K, V>> _expiredListeners = new ArrayList<CacheEntryExpiredListener<K, V>>();
      if (config.hasListeners()) {
        for (CustomizationSupplier<CacheEntryOperationListener<K, V>> f : config.getListeners()) {
          CacheEntryOperationListener<K, V> el = ( CacheEntryOperationListener<K, V>) bc.createCustomization(f);
          if (el instanceof CacheEntryCreatedListener) {
            _syncCreatedListeners.add((CacheEntryCreatedListener) el);
          }
          if (el instanceof CacheEntryUpdatedListener) {
            _syncUpdatedListeners.add((CacheEntryUpdatedListener) el);
          }
          if (el instanceof CacheEntryRemovedListener) {
            _syncRemovedListeners.add((CacheEntryRemovedListener) el);
          }
          if (el instanceof CacheEntryExpiredListener) {
            _expiredListeners.add((CacheEntryExpiredListener) el);
          }
          if (el instanceof CacheEntryEvictedListener) {
            _syncEvictedListeners.add((CacheEntryEvictedListener) el);
          }
        }
      }
      if (config.hasAsyncListeners() || !_expiredListeners.isEmpty()) {
        Executor _executor = DEFAULT_ASYNC_LISTENER_EXECUTOR;
        if (config.getAsyncListenerExecutor() != null) {
          _executor = _cache.createCustomization(config.getAsyncListenerExecutor());
        }
        AsyncDispatcher<K> _asyncDispatcher = new AsyncDispatcher<K>(wc, _executor);
        List<CacheEntryCreatedListener<K, V>> cll = new ArrayList<CacheEntryCreatedListener<K, V>>();
        List<CacheEntryUpdatedListener<K, V>> ull = new ArrayList<CacheEntryUpdatedListener<K, V>>();
        List<CacheEntryRemovedListener<K, V>> rll = new ArrayList<CacheEntryRemovedListener<K, V>>();
        List<CacheEntryExpiredListener<K, V>> ell = new ArrayList<CacheEntryExpiredListener<K, V>>();
        List<CacheEntryEvictedListener<K, V>> evl = new ArrayList<CacheEntryEvictedListener<K, V>>();
        for (CustomizationSupplier<CacheEntryOperationListener<K, V>> f : config.getAsyncListeners()) {
          CacheEntryOperationListener<K, V> el = (CacheEntryOperationListener<K, V>) bc.createCustomization(f);
          if (el instanceof CacheEntryCreatedListener) {
            cll.add((CacheEntryCreatedListener) el);
          }
          if (el instanceof CacheEntryUpdatedListener) {
            ull.add((CacheEntryUpdatedListener) el);
          }
          if (el instanceof CacheEntryRemovedListener) {
            rll.add((CacheEntryRemovedListener) el);
          }
          if (el instanceof CacheEntryExpiredListener) {
            ell.add((CacheEntryExpiredListener) el);
          }
          if (el instanceof CacheEntryEvictedListener) {
            evl.add((CacheEntryEvictedListener) el);
          }
        }
        for (CacheEntryCreatedListener l : cll) {
          _syncCreatedListeners.add(new AsyncCreatedListener<K, V>(_asyncDispatcher, l));
        }
        for (CacheEntryUpdatedListener l : ull) {
          _syncUpdatedListeners.add(new AsyncUpdatedListener<K, V>(_asyncDispatcher, l));
        }
        for (CacheEntryRemovedListener l : rll) {
          _syncRemovedListeners.add(new AsyncRemovedListener<K, V>(_asyncDispatcher, l));
        }
        for (CacheEntryExpiredListener l : ell) {
          _syncExpiredListeners.add(new AsyncExpiredListener<K, V>(_asyncDispatcher, l));
        }
        for (CacheEntryExpiredListener l : _expiredListeners) {
          _syncExpiredListeners.add(new AsyncExpiredListener<K, V>(_asyncDispatcher, l));
        }
        for (CacheEntryEvictedListener l : evl) {
          _syncEvictedListeners.add(new AsyncEvictedListener<K, V>(_asyncDispatcher, l));
        }
      }
      if (!_syncCreatedListeners.isEmpty()) {
        wc.syncEntryCreatedListeners = _syncCreatedListeners.toArray(new CacheEntryCreatedListener[0]);
      }
      if (!_syncUpdatedListeners.isEmpty()) {
        wc.syncEntryUpdatedListeners = _syncUpdatedListeners.toArray(new CacheEntryUpdatedListener[0]);
      }
      if (!_syncRemovedListeners.isEmpty()) {
        wc.syncEntryRemovedListeners = _syncRemovedListeners.toArray(new CacheEntryRemovedListener[0]);
      }
      if (!_syncExpiredListeners.isEmpty()) {
        wc.syncEntryExpiredListeners = _syncExpiredListeners.toArray(new CacheEntryExpiredListener[0]);
      }
      if (!_syncEvictedListeners.isEmpty()) {
        wc.syncEntryEvictedListeners = _syncEvictedListeners.toArray(new CacheEntryEvictedListener[0]);
      }
      bc.eviction = constructEviction(bc, wc, config);
      TimingHandler rh = TimingHandler.of(_timeReference, config);
      bc.setTiming(rh);
      wc.init();
    } else {
      TimingHandler rh = TimingHandler.of(_timeReference, config);
      bc.setTiming(rh);
       bc.eviction = constructEviction(bc, HeapCacheListener.NO_OPERATION, config);
      bc.init();
    }
    manager.sendCreatedEvent(_cache, config);
    return _cache;
  }