com.github.benmanes.caffeine.cache.Cache Java Examples

The following examples show how to use com.github.benmanes.caffeine.cache.Cache. 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: ParserBolt.java    From metron with Apache License 2.0 6 votes vote down vote up
protected Context initializeStellar() {
  Map<String, Object> cacheConfig = new HashMap<>();
  for (String sensorType: this.parserRunner.getSensorTypes()) {
    SensorParserConfig config = getSensorParserConfig(sensorType);

    if (config != null) {
      cacheConfig.putAll(config.getCacheConfig());
    }
  }
  Cache<CachingStellarProcessor.Key, Object> cache = CachingStellarProcessor.createCache(cacheConfig);

  Context.Builder builder = new Context.Builder()
          .with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client)
          .with(Context.Capabilities.GLOBAL_CONFIG, () -> getConfigurations().getGlobalConfig())
          .with(Context.Capabilities.STELLAR_CONFIG, () -> getConfigurations().getGlobalConfig())
          ;
  if(cache != null) {
    builder = builder.with(Context.Capabilities.CACHE, () -> cache);
  }
  Context stellarContext = builder.build();
  StellarFunctions.initialize(stellarContext);
  return stellarContext;
}
 
Example #2
Source File: WriteBehindCacheWriterTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test
public void givenCacheUpdateOnMultipleKeys_writeBehindIsCalled() {
  AtomicBoolean writerCalled = new AtomicBoolean(false);
  AtomicInteger numberOfEntries = new AtomicInteger(0);

  // Given this cache...
  Cache<Long, ZonedDateTime> cache = Caffeine.newBuilder()
      .writer(new WriteBehindCacheWriter.Builder<Long, ZonedDateTime>()
          .bufferTime(1, TimeUnit.SECONDS)
          .coalesce(BinaryOperator.maxBy(ZonedDateTime::compareTo))
          .writeAction(entries -> {
            numberOfEntries.set(entries.size());
            writerCalled.set(true);
          }).build())
      .build();

  // When these cache updates happen ...
  cache.put(1L, ZonedDateTime.now());
  cache.put(2L, ZonedDateTime.now());
  cache.put(3L, ZonedDateTime.now());

  // Then the write behind action gets 3 entries to write
  Awaitility.await().untilTrue(writerCalled);
  Assert.assertEquals(3, numberOfEntries.intValue());
}
 
Example #3
Source File: WriteBehindCacheWriterTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test
public void givenCacheUpdate_writeBehindIsCalled() {
  AtomicBoolean writerCalled = new AtomicBoolean(false);

  // Given this cache...
  Cache<Long, ZonedDateTime> cache = Caffeine.newBuilder()
      .writer(new WriteBehindCacheWriter.Builder<Long, ZonedDateTime>()
          .bufferTime(1, TimeUnit.SECONDS)
          .coalesce(BinaryOperator.maxBy(ZonedDateTime::compareTo))
          .writeAction(entries -> writerCalled.set(true))
          .build())
      .build();

  // When this cache update happens ...
  cache.put(1L, ZonedDateTime.now());

  // Then the write behind action is called
  Awaitility.await().untilTrue(writerCalled);
}
 
Example #4
Source File: DefaultMessageDistributor.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Flush all of the profiles maintained in a cache.
 *
 * @param cache The cache to flush.
 * @return The measurements captured when flushing the profiles.
 */
private List<ProfileMeasurement> flushCache(Cache<Integer, ProfileBuilder> cache) {

  List<ProfileMeasurement> measurements = new ArrayList<>();
  for(ProfileBuilder profileBuilder: cache.asMap().values()) {

    // only need to flush, if the profile has been initialized
    if(profileBuilder.isInitialized()) {

      // flush the profiler and save the measurement, if one exists
      Optional<ProfileMeasurement> measurement = profileBuilder.flush();
      measurement.ifPresent(m -> measurements.add(m));
    }
  }

  return measurements;
}
 
Example #5
Source File: ActiveQueryLog.java    From datawave with Apache License 2.0 6 votes vote down vote up
public void setMaxIdle(long maxIdle) {
    if (this.maxIdle != maxIdle || this.CACHE == null) {
        if (maxIdle > 0) {
            cacheLock.writeLock().lock();
            try {
                Cache<String,ActiveQuery> newCache = setupCache(maxIdle);
                if (this.CACHE == null) {
                    this.CACHE = newCache;
                } else {
                    Cache<String,ActiveQuery> oldCache = this.CACHE;
                    this.CACHE = newCache;
                    this.CACHE.putAll(oldCache.asMap());
                }
            } finally {
                cacheLock.writeLock().unlock();
            }
        } else {
            log.error("Bad value: (" + maxIdle + ") for maxIdle");
        }
    }
}
 
Example #6
Source File: GenericWebSocket.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Checks if session has sent too many bad messages, if so we ignore messages from that session
 * and return an empty. Otherwise we will convert the message JSON string to a {@code
 * MessageEnvelope} and return that. If the message is badly formatted, we'll send back an error
 * message response to the session, increment the bad message count and return an empty.
 */
private static Optional<MessageEnvelope> readJsonMessage(
    final Session session,
    final String message,
    final Cache<InetAddress, AtomicInteger> badMessageCache,
    final MessageSender messageSender) {

  if (burnMessagesFromThisSession(session, badMessageCache)) {
    // Burn the message -> no-op
    // To conserve server resources, we do not even log the message.
    return Optional.empty();
  }

  try {
    return Optional.of(GSON.fromJson(message, MessageEnvelope.class));
  } catch (final JsonSyntaxException e) {
    final InetAddress inetAddress = InetExtractor.extract(session.getUserProperties());
    incrementBadMessageCount(session, badMessageCache);
    logBadMessage(inetAddress, message);
    respondWithServerError(messageSender, session);
    return Optional.empty();
  }
}
 
Example #7
Source File: CaffeineMetricSupport.java    From armeria with Apache License 2.0 6 votes vote down vote up
private boolean updateCacheStats(boolean force) {
    final Cache<?, ?> cache = get();
    if (cache == null) {
        return true; // GC'd
    }

    final long currentTimeNanos = ticker.read();
    if (!force) {
        if (currentTimeNanos - lastStatsUpdateTime < UPDATE_INTERVAL_NANOS) {
            return false; // Not GC'd
        }
    }

    cacheStats = cache.stats();
    estimatedSize = cache.estimatedSize();

    // Write the volatile field last so that cacheStats and estimatedSize are visible
    // after reading the volatile field.
    lastStatsUpdateTime = currentTimeNanos;
    return false; // Not GC'd
}
 
Example #8
Source File: CaffeineCache.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
private Cache<K, V> buildCache(Cache<K, V> prev) {
  @SuppressWarnings({"rawtypes"})
  Caffeine builder = Caffeine.newBuilder()
      .initialCapacity(initialSize)
      .executor(executor)
      .removalListener(this)
      .recordStats();
  if (maxIdleTimeSec > 0) {
    builder.expireAfterAccess(Duration.ofSeconds(maxIdleTimeSec));
  }
  if (maxRamBytes != Long.MAX_VALUE) {
    builder.maximumWeight(maxRamBytes);
    builder.weigher((k, v) -> (int) (RamUsageEstimator.sizeOfObject(k) + RamUsageEstimator.sizeOfObject(v)));
  } else {
    builder.maximumSize(maxSize);
  }
  Cache<K, V> newCache = builder.build();
  if (prev != null) {
    newCache.putAll(prev.asMap());
  }
  return newCache;
}
 
Example #9
Source File: AuthCache.java    From timely with Apache License 2.0 6 votes vote down vote up
private static Cache<String, TimelyPrincipal> getCache() {
    if (-1 == cacheExpirationMinutes) {
        throw new IllegalStateException("Cache session max age not configured.");
    }
    if (null == CACHE) {
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
        caffeine.expireAfterWrite(cacheExpirationMinutes, TimeUnit.MINUTES);
        if (cacheRefreshMinutes > 0) {
            caffeine.refreshAfterWrite(cacheRefreshMinutes, TimeUnit.MINUTES);
            CACHE = caffeine.build(key -> getTimelyPrincipal(key));
        } else {
            CACHE = caffeine.build();
        }
    }
    return CACHE;
}
 
Example #10
Source File: CaffeineMetricSupport.java    From armeria with Apache License 2.0 6 votes vote down vote up
void add(Cache<?, ?> cache, Ticker ticker) {
    synchronized (cacheRefs) {
        for (CacheReference ref : cacheRefs) {
            if (ref.get() == cache) {
                // Do not aggregate more than once for the same instance.
                return;
            }
        }

        cacheRefs.add(new CacheReference(cache, ticker));
    }

    if (cache instanceof LoadingCache && hasLoadingCache.compareAndSet(false, true)) {
        // Add the following meters only for LoadingCache and only once.
        final String loads = idPrefix.name("loads");

        parent.more().counter(loads, idPrefix.tags("result", "success"), this,
                              func(LOAD_SUCCESS_COUNT, ref -> ref.cacheStats.loadSuccessCount()));
        parent.more().counter(loads, idPrefix.tags("result", "failure"), this,
                              func(LOAD_FAILURE_COUNT, ref -> ref.cacheStats.loadFailureCount()));
        parent.more().counter(idPrefix.name(Flags.useLegacyMeterNames() ? "loadDuration"
                                                                        : "load.duration"),
                              idPrefix.tags(), this,
                              func(TOTAL_LOAD_TIME, ref -> ref.cacheStats.totalLoadTime()));
    }
}
 
Example #11
Source File: ApplicationConfiguration.java    From cerberus with Apache License 2.0 5 votes vote down vote up
@Bean
public Cache<AwsIamKmsAuthRequest, EncryptedAuthDataWrapper> kmsAuthCache(
    MetricsService metricsService,
    @Value("${cerberus.auth.iam.kms.cache.maxAgeInSeconds:10}") int maxAge) {

  return new MetricReportingCache<>("auth.kms", maxAge, metricsService, null);
}
 
Example #12
Source File: CacheValidationListener.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Free memory by clearing unused resources after test execution. */
private void cleanUp(ITestResult testResult) {
  boolean briefParams = !detailedParams.get();

  if (testResult.isSuccess() && briefParams) {
    testResult.setParameters(EMPTY_PARAMS);
    testResult.setThrowable(null);
  }

  Object[] params = testResult.getParameters();
  for (int i = 0; i < params.length; i++) {
    Object param = params[i];
    if ((param instanceof AsyncCache<?, ?>) || (param instanceof Cache<?, ?>)
        || (param instanceof Map<?, ?>) || (param instanceof Eviction<?, ?>)
        || (param instanceof Expiration<?, ?>) || (param instanceof VarExpiration<?, ?>)
        || ((param instanceof CacheContext) && briefParams)) {
      params[i] = simpleNames.get(param.getClass(), key -> ((Class<?>) key).getSimpleName());
    } else if (param instanceof CacheContext) {
      params[i] = simpleNames.get(param.toString(), Object::toString);
    } else {
      params[i] = Objects.toString(param);
    }
  }

  /*
  // Enable in TestNG 7.0
  if ((testResult.getName() != null) && briefParams) {
    testResult.setTestName(simpleNames.get(testResult.getName(), Object::toString));
  }
  */

  CacheSpec.interner.get().clear();
}
 
Example #13
Source File: GenericWebSocket.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private static void incrementBadMessageCount(
    final Session session, final Cache<InetAddress, AtomicInteger> badMessageCache) {
  badMessageCache
      .asMap()
      .computeIfAbsent(
          InetExtractor.extract(session.getUserProperties()), inet -> new AtomicInteger(0))
      .incrementAndGet();
}
 
Example #14
Source File: GenericWebSocket.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private static boolean burnMessagesFromThisSession(
    final Session session, final Cache<InetAddress, AtomicInteger> badMessageCache) {
  final InetAddress inetAddress = InetExtractor.extract(session.getUserProperties());

  final int badMessageCount =
      Optional.ofNullable(badMessageCache.getIfPresent(inetAddress))
          .map(AtomicInteger::get)
          .orElse(0);

  return badMessageCount > MAX_BAD_MESSAGES;
}
 
Example #15
Source File: GenericWebSocket.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
@VisibleForTesting
static void onMessage(
    final Session session,
    final String message,
    final Cache<InetAddress, AtomicInteger> badMessageCache,
    final MessageSender messageSender) {

  readJsonMessage(session, message, badMessageCache, messageSender)
      .ifPresent(
          envelope ->
              ((WebSocketMessagingBus) session.getUserProperties().get(MESSAGING_BUS_KEY))
                  .onMessage(session, envelope));
}
 
Example #16
Source File: CaffeineTest.java    From cache2k-benchmark with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
	Cache c =
		Caffeine.newBuilder().maximumWeight(1000).weigher(new Weigher<Object, Object>() {
			@Override
			public int weigh(final Object key, final Object value) {
				return value.hashCode() & 0x7f;
			}
		}).build();
	c.put(1, 123);
	c.put(1, 512);
	c.put(1, 0);
}
 
Example #17
Source File: CaffeineCacheImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
protected Cache getCache(String cacheName) {
    Cache cache = cacheMap.get(cacheName);
    if (cache == null) {
        synchronized (CaffeineCacheImpl.class) {
            if (cache == null) {
                cache = createCacheBuilder().build();
                cacheMap.put(cacheName,cache);
            }
        }
    }
    return cache;
}
 
Example #18
Source File: CacheHelper.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
public static <K, V> Cache<K, V> createLocalCache(int maxSize, int expireInSeconds) {
    return Caffeine.newBuilder()
            .initialCapacity(maxSize)
            .maximumSize(maxSize)
            .expireAfterWrite(expireInSeconds, TimeUnit.SECONDS)
            .build();
}
 
Example #19
Source File: CacheHelper.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
public static <K, V> Cache<K, V> createLocalCache(int maxSize, int expireInSeconds, RemovalListener<K, V> listener) {
    return Caffeine.newBuilder()
            .initialCapacity(maxSize)
            .maximumSize(maxSize)
            .removalListener(listener)
            .expireAfterWrite(expireInSeconds, TimeUnit.SECONDS)
            .build();
}
 
Example #20
Source File: RouteCache.java    From armeria with Apache License 2.0 5 votes vote down vote up
CachingRouter(Router<V> delegate, Function<V, Route> routeResolver,
              Cache<RoutingContext, V> findCache,
              Cache<RoutingContext, List<V>> findAllCache,
              Set<Route> ambiguousRoutes) {
    this.delegate = requireNonNull(delegate, "delegate");
    this.routeResolver = requireNonNull(routeResolver, "routeResolver");
    this.findCache = requireNonNull(findCache, "findCache");
    this.findAllCache = requireNonNull(findAllCache, "findAllCache");

    final Set<Route> newAmbiguousRoutes =
            Collections.newSetFromMap(new IdentityHashMap<>(ambiguousRoutes.size()));
    newAmbiguousRoutes.addAll(requireNonNull(ambiguousRoutes, "ambiguousRoutes"));
    this.ambiguousRoutes = Collections.unmodifiableSet(newAmbiguousRoutes);
}
 
Example #21
Source File: AuthenticationService.java    From cerberus with Apache License 2.0 5 votes vote down vote up
@Autowired
public AuthenticationService(
    AwsIamRoleDao awsIamRoleDao,
    AuthConnector authConnector,
    KmsService kmsService,
    KmsClientFactory kmsClientFactory,
    ObjectMapper objectMapper,
    @Value("${cerberus.admin.roles:#{null}}") String adminRoleArns,
    @Value("${cerberus.admin.group}") String adminGroup,
    @Value("${cerberus.auth.user.token.maxRefreshCount:#{0}}") int maxTokenRefreshCount,
    DateTimeSupplier dateTimeSupplier,
    AwsIamRoleArnParser awsIamRoleArnParser,
    AuthTokenService authTokenService,
    @Value("${cerberus.auth.user.token.ttl}") String userTokenTTL,
    @Value("${cerberus.auth.iam.token.ttl}") String iamTokenTTL,
    AwsIamRoleService awsIamRoleService,
    @Value("${cerberus.auth.iam.kms.cache.enabled:#{false}}") boolean cacheEnabled,
    Cache<AwsIamKmsAuthRequest, EncryptedAuthDataWrapper> kmsAuthCache) {

  this.awsIamRoleDao = awsIamRoleDao;
  this.authServiceConnector = authConnector;
  this.kmsService = kmsService;
  this.kmsClientFactory = kmsClientFactory;
  this.objectMapper = objectMapper;
  this.adminRoleArns = adminRoleArns;
  this.adminGroup = adminGroup;
  this.dateTimeSupplier = dateTimeSupplier;
  this.awsIamRoleArnParser = awsIamRoleArnParser;
  this.maxTokenRefreshCount = maxTokenRefreshCount;
  this.authTokenService = authTokenService;
  this.userTokenTTL = userTokenTTL;
  this.iamTokenTTL = iamTokenTTL;
  this.awsIamRoleService = awsIamRoleService;
  this.cacheEnabled = cacheEnabled;
  this.kmsAuthCache = kmsAuthCache;
}
 
Example #22
Source File: FileService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Cache<PathAndEncoding, AggregatedHttpFile> newCache(String cacheSpec) {
    final Caffeine<Object, Object> b = Caffeine.from(cacheSpec);
    b.recordStats()
     .removalListener((RemovalListener<PathAndEncoding, AggregatedHttpFile>) (key, value, cause) -> {
         if (value != null) {
             final HttpData content = value.content();
             if (content instanceof ByteBufHolder) {
                 ((ByteBufHolder) content).release();
             }
         }
     });
    return b.build();
}
 
Example #23
Source File: CaffeineCacheMetricsTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void doNotReportMetricsForNonLoadingCache() {
    MeterRegistry meterRegistry = new SimpleMeterRegistry();
    Cache<Object, Object> cache = Caffeine.newBuilder().build();
    CaffeineCacheMetrics metrics = new CaffeineCacheMetrics(cache, "testCache", expectedTag);
    metrics.bindTo(meterRegistry);

    assertThat(meterRegistry.find("cache.load.duration").timeGauge()).isNull();
}
 
Example #24
Source File: CaffeineMetricSupport.java    From armeria with Apache License 2.0 5 votes vote down vote up
public static void setup(MeterRegistry registry, MeterIdPrefix idPrefix, Cache<?, ?> cache, Ticker ticker) {
    requireNonNull(cache, "cache");
    if (!cache.policy().isRecordingStats()) {
        return;
    }
    final CaffeineMetrics metrics = MicrometerUtil.register(
            registry, idPrefix, CaffeineMetrics.class, CaffeineMetrics::new);
    metrics.add(cache, ticker);
}
 
Example #25
Source File: AuthCache.java    From qonduit with Apache License 2.0 5 votes vote down vote up
public static Cache<String, Authentication> getCache() {
    if (-1 == sessionMaxAge) {
        throw new IllegalStateException("Cache session max age not configured.");
    }
    if (null == CACHE) {
        CACHE = Caffeine.newBuilder().expireAfterAccess(sessionMaxAge, TimeUnit.SECONDS).build();
    }
    return CACHE;
}
 
Example #26
Source File: CaffeineCacheImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public void setTtl(String cacheName, Object key, int seconds) {
    Cache cache = getCacheOnly(cacheName);
    if (cache == null) {
        return;
    }

    CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key);
    if (data == null) {
        return;
    }

    data.setLiveSeconds(seconds);
    putData(cache, key, data);
}
 
Example #27
Source File: CacheGenerator.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"deprecation", "unchecked", "BoxedPrimitiveConstructor"})
private void populate(CacheContext context, Cache<Integer, Integer> cache) {
  if (context.population.size() == 0) {
    return;
  }

  int maximum = (int) Math.min(context.maximumSize(), context.population.size());
  int first = BASE + (int) Math.min(0, context.population.size());
  int last = BASE + maximum - 1;
  int middle = Math.max(first, BASE + ((last - first) / 2));

  context.disableRejectingCacheWriter();
  for (int i = 0; i < maximum; i++) {
    Map.Entry<Integer, Integer> entry = INTS.get(i);

    // Reference caching (weak, soft) require unique instances for identity comparison
    Integer key = context.isStrongKeys() ? entry.getKey() : new Integer(BASE + i);
    Integer value = context.isStrongValues() ? entry.getValue() : new Integer(-key);

    if (key == first) {
      context.firstKey = key;
    }
    if (key == middle) {
      context.middleKey = key;
    }
    if (key == last) {
      context.lastKey = key;
    }
    cache.put(key, value);
    context.original.put(key, value);
    context.ticker().advance(context.advance.timeNanos(), TimeUnit.NANOSECONDS);
  }
  context.enableRejectingCacheWriter();
  if (context.writer() == Writer.MOCKITO) {
    reset(context.cacheWriter());
  }
}
 
Example #28
Source File: CaffeineCacheImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(String cacheName, Object key, IDataLoader dataLoader, int liveSeconds) {
    Cache cache = getCache(cacheName);
    CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key);
    if (data == null || data.isDue()) {
        Object newValue = dataLoader.load();
        if (newValue != null) {
            data = new CaffeineCacheObject(newValue, liveSeconds);
            putData(cache, key, data);
        }
        return (T) newValue;
    } else {
        return (T) data.getValue();
    }
}
 
Example #29
Source File: CaffeineCacheImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(String cacheName, Object key, IDataLoader dataLoader) {
    Cache cache = getCache(cacheName);
    CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key);
    if (data == null || data.isDue()) {
        Object newValue = dataLoader.load();
        if (newValue != null) {
            data = new CaffeineCacheObject(newValue);
            putData(cache, key, data);
        }
        return (T) newValue;
    } else {
        return (T) data.getValue();
    }
}
 
Example #30
Source File: CacheMetricsCollectorTest.java    From client_java with Apache License 2.0 5 votes vote down vote up
@Test
public void cacheExposesMetricsForHitMissAndEviction() throws Exception {
    Cache<String, String> cache = Caffeine.newBuilder().maximumSize(2).recordStats().executor(new Executor() {
        @Override
        public void execute(Runnable command) {
            // Run cleanup in same thread, to remove async behavior with evictions
            command.run();
        }
    }).build();
    CollectorRegistry registry = new CollectorRegistry();

    CacheMetricsCollector collector = new CacheMetricsCollector().register(registry);
    collector.addCache("users", cache);

    cache.getIfPresent("user1");
    cache.getIfPresent("user1");
    cache.put("user1", "First User");
    cache.getIfPresent("user1");

    // Add to cache to trigger eviction.
    cache.put("user2", "Second User");
    cache.put("user3", "Third User");
    cache.put("user4", "Fourth User");

    assertMetric(registry, "caffeine_cache_hit_total", "users", 1.0);
    assertMetric(registry, "caffeine_cache_miss_total", "users", 2.0);
    assertMetric(registry, "caffeine_cache_requests_total", "users", 3.0);
    assertMetric(registry, "caffeine_cache_eviction_total", "users", 2.0);
}