org.springframework.cache.Cache.clear()方法的使用及代码示例

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

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

Cache.clear介绍

[英]Remove all mappings from the cache.
[中]从缓存中删除所有映射。

代码示例

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

@Override
  public void afterCommit() {
    targetCache.clear();
  }
});

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

public void clearCache() {
    cache.clear();
  }
}

代码示例来源:origin: org.springframework/spring-context-support

@Override
  public void afterCommit() {
    targetCache.clear();
  }
});

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

/**
 * Execute {@link Cache#clear()} on the specified {@link Cache} and
 * invoke the error handler if an exception occurs.
 */
protected void doClear(Cache cache) {
  try {
    cache.clear();
  }
  catch (RuntimeException ex) {
    getErrorHandler().handleCacheClearError(ex, cache);
  }
}

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

@Override
public void clear() {
  if (TransactionSynchronizationManager.isSynchronizationActive()) {
    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
      @Override
      public void afterCommit() {
        targetCache.clear();
      }
    });
  }
  else {
    this.targetCache.clear();
  }
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Execute {@link Cache#clear()} on the specified {@link Cache} and
 * invoke the error handler if an exception occurs.
 */
protected void doClear(Cache cache) {
  try {
    cache.clear();
  }
  catch (RuntimeException ex) {
    getErrorHandler().handleCacheClearError(ex, cache);
  }
}

代码示例来源:origin: org.springframework/spring-context-support

@Override
public void clear() {
  if (TransactionSynchronizationManager.isSynchronizationActive()) {
    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
      @Override
      public void afterCommit() {
        targetCache.clear();
      }
    });
  }
  else {
    this.targetCache.clear();
  }
}

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

private Cache getCache() {
  Cache cache = cacheManager.getCache("springcasebasedacltests");
  cache.clear();
  return cache;
}

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

private Cache getCache() {
  Cache cache = cacheManager.getCache("springbasedusercachetests");
  cache.clear();
  return cache;
}

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

@Test
public void clearFailProperException() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
  willThrow(exception).given(this.cache).clear();
  this.cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler());
  this.thrown.expect(is(exception));
  this.simpleService.clear();
}

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

@Test
public void clearFail() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
  willThrow(exception).given(this.cache).clear();
  this.simpleService.clear();
  verify(this.errorHandler).handleCacheClearError(exception, cache);
}

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

@Test
public void clearFail() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict");
  willThrow(exception).given(this.cache).clear();
  this.simpleService.clear();
  verify(this.errorHandler).handleCacheClearError(exception, this.cache);
}

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

@Test
public void clearNonTransactional() {
  Cache target = new ConcurrentMapCache("testCache");
  Cache cache = new TransactionAwareCacheDecorator(target);
  Object key = new Object();
  cache.put(key, "123");
  cache.clear();
  assertNull(target.get(key));
}

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

public void testUnlessExpression(CacheableService<?> service) throws Exception {
  Cache cache = this.cm.getCache("testCache");
  cache.clear();
  service.unless(10);
  service.unless(11);
  assertThat(cache.get(10).get(), equalTo(10L));
  assertThat(cache.get(11), nullValue());
}

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

@Test
public void spr14230AdaptsToOptional() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr14230Config.class);
  Spr14230Service bean = context.getBean(Spr14230Service.class);
  Cache cache = context.getBean(CacheManager.class).getCache("itemCache");
  TestBean tb = new TestBean("tb1");
  bean.insertItem(tb);
  assertSame(tb, bean.findById("tb1").get());
  assertSame(tb, cache.get("tb1").get());
  cache.clear();
  TestBean tb2 = bean.findById("tb1").get();
  assertNotSame(tb, tb2);
  assertSame(tb2, cache.get("tb1").get());
}

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

@Test
public void spr14853AdaptsToOptionalWithSync() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr14853Config.class);
  Spr14853Service bean = context.getBean(Spr14853Service.class);
  Cache cache = context.getBean(CacheManager.class).getCache("itemCache");
  TestBean tb = new TestBean("tb1");
  bean.insertItem(tb);
  assertSame(tb, bean.findById("tb1").get());
  assertSame(tb, cache.get("tb1").get());
  cache.clear();
  TestBean tb2 = bean.findById("tb1").get();
  assertNotSame(tb, tb2);
  assertSame(tb2, cache.get("tb1").get());
}

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

@Test
  public void clearTransactional() {
    Cache target = new ConcurrentMapCache("testCache");
    Cache cache = new TransactionAwareCacheDecorator(target);
    Object key = new Object();
    cache.put(key, "123");

    TransactionStatus status = this.txManager.getTransaction(
        new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED));
    cache.clear();
    assertEquals("123", target.get(key, String.class));
    this.txManager.commit(status);

    assertNull(target.get(key));
  }
}

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

@Test
public void regularOperationsOnTarget() {
  Cache target = new ConcurrentMapCache("testCache");
  Cache cache = new TransactionAwareCacheDecorator(target);
  assertEquals(target.getName(), cache.getName());
  assertEquals(target.getNativeCache(), cache.getNativeCache());
  Object key = new Object();
  target.put(key, "123");
  assertEquals("123", cache.get(key).get());
  assertEquals("123", cache.get(key, String.class));
  cache.clear();
  assertNull(target.get(key));
}

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

@Test
public void testCacheClear() throws Exception {
  T cache = getCache();
  assertNull(cache.get("enescu"));
  cache.put("enescu", "george");
  assertNull(cache.get("vlaicu"));
  cache.put("vlaicu", "aurel");
  cache.clear();
  assertNull(cache.get("vlaicu"));
  assertNull(cache.get("enescu"));
}

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

@Test
public void getAndPut() {
  this.cache.clear();
  long key = 1;
  Long value = this.service.getAndPut(key);
  assertEquals("Wrong value for @Cacheable key", value, this.cache.get(key).get());
  assertEquals("Wrong value for @CachePut key", value, this.cache.get(value + 100).get()); // See @CachePut
  // CachePut forced a method call
  Long anotherValue = this.service.getAndPut(key);
  assertNotSame(value, anotherValue);
  // NOTE: while you might expect the main key to have been updated, it hasn't. @Cacheable operations
  // are only processed in case of a cache miss. This is why combining @Cacheable with @CachePut
  // is a very bad idea. We could refine the condition now that we can figure out if we are going
  // to invoke the method anyway but that brings a whole new set of potential regressions.
  //assertEquals("Wrong value for @Cacheable key", anotherValue, cache.get(key).get());
  assertEquals("Wrong value for @CachePut key", anotherValue, this.cache.get(anotherValue + 100).get());
}

相关文章