com.github.benmanes.caffeine.cache.Cache.put()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(197)

本文整理了Java中com.github.benmanes.caffeine.cache.Cache.put()方法的一些代码示例,展示了Cache.put()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.put()方法的具体详情如下:
包路径:com.github.benmanes.caffeine.cache.Cache
类名称:Cache
方法名:put

Cache.put介绍

[英]Associates the value with the key in this cache. If the cache previously contained a value associated with the key, the old value is replaced by the new value.

Prefer #get(Object,Function) when using the conventional "if cached, return; otherwise create, cache and return" pattern.
[中]

代码示例

代码示例来源:origin: ben-manes/caffeine

@Override
public void put(K key, V value) {
 cache.put(key, value);
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void put_sameWeight(Cache<String, List<Integer>> cache,
  CacheContext context, Eviction<?, ?> eviction) {
 cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));
 cache.put("a", asList(-1, -2, -3));
 assertThat(cache.estimatedSize(), is(2L));
 assertThat(eviction.weightedSize().getAsLong(), is(4L));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void put(Object key, @Nullable Object value) {
  this.cache.put(key, toStoreValue(value));
}

代码示例来源:origin: apache/incubator-druid

@Override
public void put(NamedKey key, byte[] value)
{
 cache.put(key, serialize(value));
}

代码示例来源:origin: dropwizard/dropwizard

/**
 * Gets the human friendly location of where the violation was raised.
 */
public static String getMessage(ConstraintViolation<?> v, Invocable invocable) {
  final Pair<Path, ? extends ConstraintDescriptor<?>> of =
      Pair.of(v.getPropertyPath(), v.getConstraintDescriptor());
  final String cachePrefix = PREFIX_CACHE.getIfPresent(of);
  if (cachePrefix == null) {
    final String prefix = calculatePrefix(v, invocable);
    PREFIX_CACHE.put(of, prefix);
    return prefix + v.getMessage();
  }
  return cachePrefix + v.getMessage();
}

代码示例来源:origin: ben-manes/caffeine

private void checkEvict(Cache<Integer, Integer> cache, List<Integer> keys, Integer... expect) {
 keys.forEach(i -> cache.put(i, i));
 checkContainsInOrder(cache, expect);
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
  maximumSize = Maximum.UNREACHABLE, weigher = CacheWeigher.VALUE)
public void weightOf(Cache<Integer, Integer> cache, CacheContext context,
  Eviction<Integer, Integer> eviction) {
 Integer key = 1;
 cache.put(key, 1);
 assertThat(eviction.weightOf(key).getAsInt(), is(1));
 cache.put(key, 2);
 assertThat(eviction.weightOf(key).getAsInt(), is(2));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void put(Cache<String, List<Integer>> cache,
  CacheContext context, Eviction<?, ?> eviction) {
 cache.put("a", asList(1, 2, 3));
 assertThat(cache.estimatedSize(), is(1L));
 assertThat(eviction.weightedSize().getAsLong(), is(3L));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
  maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE,
  mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE },
  expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS },
  expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
  expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void computeIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
 cache.put(1, ImmutableList.of(1));
 context.ticker().advance(1, TimeUnit.MINUTES);
 cache.asMap().computeIfAbsent(1, k -> ImmutableList.of(1, 2, 3));
 assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine,
  population = Population.FULL, maximumSize = Maximum.FULL,
  executorFailure = ExecutorFailure.EXPECTED, executor = CacheExecutor.REJECTING,
  removalListener = Listener.CONSUMING)
public void scheduleDrainBuffers_rejected(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(context.absentKey(), context.absentValue());
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void put_nullKeyAndValue(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(null, null);
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
  maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE,
  mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE },
  expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS },
  expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
  expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void compute_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
 cache.put(1, ImmutableList.of(1));
 context.ticker().advance(1, TimeUnit.MINUTES);
 cache.asMap().compute(1, (k, v) -> ImmutableList.of(1, 2, 3));
 assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine,
  population = Population.EMPTY, maximumSize = Maximum.FULL)
public void drain_onWrite(Cache<Integer, Integer> cache, CacheContext context) {
 BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
 cache.put(1, 1);
 int size = localCache.accessOrderEdenDeque().size()
   + localCache.accessOrderProbationDeque().size();
 assertThat(localCache.writeBuffer().size(), is(0));
 assertThat(size, is(1));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_insert(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(context.absentKey(), context.absentValue());
 assertThat(cache.estimatedSize(), is(context.initialSize() + 1));
 assertThat(cache.getIfPresent(context.absentKey()), is(context.absentValue()));
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = WriteException.class)
@CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG,
  compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING)
public void put_insert_writerFails(Cache<Integer, Integer> cache, CacheContext context) {
 try {
  cache.put(context.absentKey(), context.absentValue());
 } finally {
  assertThat(cache.asMap(), equalTo(context.original()));
 }
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = WriteException.class)
@CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG,
  population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING)
public void put_replace_writerFails(Cache<Integer, Integer> cache, CacheContext context) {
 try {
  cache.put(context.middleKey(), context.absentValue());
 } finally {
  assertThat(cache.asMap(), equalTo(context.original()));
 }
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void put_nullKey(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(null, context.absentValue());
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void put_nullValue(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(context.absentKey(), null);
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter
@CacheSpec(maximumSize = Maximum.FULL,
  weigher = CacheWeigher.NEGATIVE, population = Population.EMPTY)
@Test(dataProvider = "caches",
  expectedExceptions = { IllegalArgumentException.class, IllegalStateException.class })
public void put_negativeWeight(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(context.absentKey(), context.absentValue());
}

代码示例来源:origin: ben-manes/caffeine

@CacheSpec(maximumSize = Maximum.FULL,
  weigher = CacheWeigher.ZERO, population = Population.EMPTY)
@Test(dataProvider = "caches")
public void put_zeroWeight(Cache<Integer, Integer> cache, CacheContext context) {
 cache.put(context.absentKey(), context.absentValue());
 verifyWriter(context, (verifier, writer) -> {
  verifier.wrote(context.absentKey(), context.absentValue());
 });
}

相关文章