org.springframework.cache.Cache类的使用及代码示例

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

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

Cache介绍

[英]Interface that defines common cache operations. Note: Due to the generic use of caching, it is recommended that implementations allow storage of null values (for example to cache methods that return null).
[中]定义通用缓存操作的接口。注意:由于缓存的一般使用,建议实现允许存储空值(例如缓存返回空值的方法)。

代码示例

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

@Override
protected Mono<String> resolveUrlPathInternal(String resourceUrlPath,
    List<? extends Resource> locations, ResourceResolverChain chain) {
  String key = RESOLVED_URL_PATH_CACHE_KEY_PREFIX + resourceUrlPath;
  String cachedUrlPath = this.cache.get(key, String.class);
  if (cachedUrlPath != null) {
    logger.trace("Path resolved from cache");
    return Mono.just(cachedUrlPath);
  }
  return chain.resolveUrlPath(resourceUrlPath, locations)
      .doOnNext(resolvedPath -> this.cache.put(key, resolvedPath));
}

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

@Override
  public void afterCommit() {
    TransactionAwareCacheDecorator.this.targetCache.evict(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: shopizer-ecommerce/shopizer

public void removeAllFromCache(MerchantStore store) throws Exception {
   net.sf.ehcache.Cache cacheImpl = (net.sf.ehcache.Cache) cache.getNativeCache();
   for (Object key: cacheImpl.getKeys()) {
      try {
        String sKey = (String)key;
        
        // a key should be <storeId>_<rest of the key>
        int delimiterPosition = sKey.indexOf(KEY_DELIMITER);
        
        if(delimiterPosition>0 && Character.isDigit(sKey.charAt(0))) {
        
          cache.evict(key);
        
        }
      } catch (Exception e) {
        LOGGER.equals("key " + key + " cannot be converted to a String or parsed");
      }  
   }
}

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

public void testCacheUpdate(CacheableService<?> service) {
  Object o = new Object();
  Cache cache = this.cm.getCache("testCache");
  assertNull(cache.get(o));
  Object r1 = service.update(o);
  assertSame(r1, cache.get(o).get());
  o = new Object();
  assertNull(cache.get(o));
  Object r2 = service.update(o);
  assertSame(r2, cache.get(o).get());
}

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

@Test
public void testDynamicMode() {
  CacheManager cm = new CaffeineCacheManager();
  Cache cache1 = cm.getCache("c1");
  assertTrue(cache1 instanceof CaffeineCache);
  Cache cache1again = cm.getCache("c1");
  assertSame(cache1again, cache1);
  Cache cache2 = cm.getCache("c2");
  assertTrue(cache2 instanceof CaffeineCache);
  Cache cache2again = cm.getCache("c2");
  assertSame(cache2again, cache2);
  Cache cache3 = cm.getCache("c3");
  assertTrue(cache3 instanceof CaffeineCache);
  Cache cache3again = cm.getCache("c3");
  assertSame(cache3again, cache3);
  cache1.put("key1", "value1");
  assertEquals("value1", cache1.get("key1").get());
  cache1.put("key2", 2);
  assertEquals(2, cache1.get("key2").get());
  cache1.put("key3", null);
  assertNull(cache1.get("key3").get());
  cache1.evict("key3");
  assertNull(cache1.get("key3"));
}

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

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

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

@Test
public void testNoOpCache() throws Exception {
  String name = createRandomKey();
  Cache cache = this.manager.getCache(name);
  assertEquals(name, cache.getName());
  Object key = new Object();
  cache.put(key, new Object());
  assertNull(cache.get(key));
  assertNull(cache.get(key, Object.class));
  assertSame(cache, cache.getNativeCache());
}

代码示例来源: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 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

public void testMultiCacheAndEvict(CacheableService<?> service) {
  String methodName = "multiCacheAndEvict";
  Cache primary = this.cm.getCache("primary");
  Cache secondary = this.cm.getCache("secondary");
  Object key = 1;
  secondary.put(key, key);
  assertNull(secondary.get(methodName));
  assertSame(key, secondary.get(key).get());
  Object r1 = service.multiCacheAndEvict(key);
  assertSame(r1, service.multiCacheAndEvict(key));
  // assert the method name is used
  assertSame(r1, primary.get(methodName).get());
  assertNull(secondary.get(methodName));
  assertNull(secondary.get(key));
}

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

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

代码示例来源:origin: kaaproject/kaa

@Override
public List<SdkKey> getCachedSdkKeys(String applicationId) {
 List<SdkKey> keys = new ArrayList<>();
 Ehcache cache = (Ehcache) adminCacheManager.getCache(SDK_CACHE).getNativeCache();
 List<?> cachedKeys = cache.getKeysWithExpiryCheck();
 for (Object cachedKey : cachedKeys) {
  if (cachedKey instanceof SdkKey) {
   SdkKey cachedSdkKey = (SdkKey) cachedKey;
   if (applicationId.equals(cachedSdkKey.getSdkProfile().getApplicationId())) {
    keys.add(cachedSdkKey);
   }
  }
 }
 return keys;
}

代码示例来源: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());
}

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

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

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

/**
 * Execute {@link Cache#put(Object, Object)} on the specified {@link Cache}
 * and invoke the error handler if an exception occurs.
 */
protected void doPut(Cache cache, Object key, @Nullable Object result) {
  try {
    cache.put(key, result);
  }
  catch (RuntimeException ex) {
    getErrorHandler().handleCachePutError(ex, cache, key, result);
  }
}

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

@Override
public String getName() {
  return this.targetCache.getName();
}

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

@Override
public Object getNativeCache() {
  return this.targetCache.getNativeCache();
}

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

public CacheEntry(Cache cache, String cacheManager) {
  super(cache.getNativeCache().getClass().getName());
  this.name = cache.getName();
  this.cacheManager = cacheManager;
}

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

@Test
public void testCacheCallable() throws Exception {
  String name = createRandomKey();
  Cache cache = this.manager.getCache(name);
  Object returnValue = new Object();
  Object value = cache.get(new Object(), () -> returnValue);
  assertEquals(returnValue, value);
}

相关文章