net.sf.ehcache.Ehcache.get()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(267)

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

Ehcache.get介绍

[英]Gets an element from the cache. Updates Element Statistics

Note that the Element's lastAccessTime is always the time of this get. Use #getQuiet(Object) to peak into the Element to see its last access time with get
[中]从缓存中获取元素。更新元素统计信息
请注意,元素的lastAccessTime始终是此get的时间。使用#getQuiet(Object)到达元素的峰值,查看其使用get的最后访问时间

代码示例

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

@Nullable
private Element lookup(Object key) {
  return this.cache.get(key);
}

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

@Override
@SuppressWarnings("unchecked")
@Nullable
public <T> T get(Object key, @Nullable Class<T> type) {
  Element element = this.cache.get(key);
  Object value = (element != null ? element.getObjectValue() : null);
  if (value != null && type != null && !type.isInstance(value)) {
    throw new IllegalStateException(
        "Cached value is not of required type [" + type.getName() + "]: " + value);
  }
  return (T) value;
}

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

@SuppressWarnings("unchecked")
@Override
public <K, V> V get(K key) {
  final Element element = ehCache.get(key);
  return element == null ? null : (V) element.getObjectValue();
}

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

public <T> T get(String key, Supplier<T> compute) {
  Element element = ehcache.get(key);
  if (element != null) {
    return (T) element.getObjectValue();
  }
  synchronized (key.intern()) {
    element = ehcache.get(key);
    if (element != null) {
      return (T) element.getObjectValue();
    }
    T object = compute.get();
    ehcache.put(new Element(key, object));
    return object;
  }
}

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

private Object getWithoutTransactionCheck(String key) {
  Element element = ehCache.get(key);
  if (element == null) {
    return null;
  }
  Object value = element.getObjectValue();
  logUnsavedPersistentObjectInteraction(value, "PersistentObject {} without an id served out of cache.");
  return value;
}

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

@Override
@SuppressWarnings("unchecked")
@Nullable
public <T> T get(Object key, @Nullable Class<T> type) {
  Element element = this.cache.get(key);
  Object value = (element != null ? element.getObjectValue() : null);
  if (value != null && type != null && !type.isInstance(value)) {
    throw new IllegalStateException(
        "Cached value is not of required type [" + type.getName() + "]: " + value);
  }
  return (T) value;
}

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

public UserDetails getUserFromCache(String username) {
  Element element = cache.get(username);
  if (logger.isDebugEnabled()) {
    logger.debug("Cache hit: " + (element != null) + "; username: " + username);
  }
  if (element == null) {
    return null;
  }
  else {
    return (UserDetails) element.getValue();
  }
}

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

public CasAuthenticationToken getByTicketId(final String serviceTicket) {
  final Element element = cache.get(serviceTicket);
  if (logger.isDebugEnabled()) {
    logger.debug("Cache hit: " + (element != null) + "; service ticket: "
        + serviceTicket);
  }
  return element == null ? null : (CasAuthenticationToken) element.getValue();
}

代码示例来源:origin: apache/kylin

public ValueWrapper get(Object key) {
  Element element = this.cache.get(key);
  return (element != null ? new SimpleValueWrapper(element.getObjectValue()) : null);
}

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

public MutableAcl getFromCache(ObjectIdentity objectIdentity) {
  Assert.notNull(objectIdentity, "ObjectIdentity required");
  Element element = null;
  try {
    element = cache.get(objectIdentity);
  }
  catch (CacheException ignored) {
  }
  if (element == null) {
    return null;
  }
  return initializeTransientFields((MutableAcl) element.getValue());
}

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

public MutableAcl getFromCache(Serializable pk) {
  Assert.notNull(pk, "Primary key (identifier) required");
  Element element = null;
  try {
    element = cache.get(pk);
  }
  catch (CacheException ignored) {
  }
  if (element == null) {
    return null;
  }
  return initializeTransientFields((MutableAcl) element.getValue());
}

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

@Override
  public Object extractPrincipal(X509Certificate cert) {
    try {
      Element element = cache.get(cert);
      if (element != null) {
        return element.getObjectValue();
      }
    } catch (CacheException cacheException) {
      throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());
    }

    final Object principal = delegate.extractPrincipal(cert);
    cache.put(new Element(cert, principal));

    return principal;
  }
}

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

@Test
public void getFromCacheObjectIdentity() throws Exception {
  when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl));
  assertThat(myCache.getFromCache(acl.getId())).isEqualTo(acl);
}

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

@Test
public void getFromCacheSerializable() throws Exception {
  when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl));
  assertThat(myCache.getFromCache(acl.getId())).isEqualTo(acl);
}

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

@Test
public void getFromCacheSerializablePopulatesTransient() throws Exception {
  when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl));
  myCache.putInCache(acl);
  ReflectionTestUtils.setField(acl, "permissionGrantingStrategy", null);
  ReflectionTestUtils.setField(acl, "aclAuthorizationStrategy", null);
  MutableAcl fromCache = myCache.getFromCache(acl.getId());
  assertThat(ReflectionTestUtils.getField(fromCache, "aclAuthorizationStrategy"))
      .isNotNull();
  assertThat(ReflectionTestUtils.getField(fromCache, "permissionGrantingStrategy"))
      .isNotNull();
}

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

@Test
public void getFromCacheObjectIdentityPopulatesTransient() throws Exception {
  when(cache.get(acl.getObjectIdentity()))
      .thenReturn(new Element(acl.getId(), acl));
  myCache.putInCache(acl);
  ReflectionTestUtils.setField(acl, "permissionGrantingStrategy", null);
  ReflectionTestUtils.setField(acl, "aclAuthorizationStrategy", null);
  MutableAcl fromCache = myCache.getFromCache(acl.getObjectIdentity());
  assertThat(ReflectionTestUtils.getField(fromCache, "aclAuthorizationStrategy"))
      .isNotNull();
  assertThat(ReflectionTestUtils.getField(fromCache, "permissionGrantingStrategy"))
      .isNotNull();
}

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

@Test
public void evictCacheSerializable() throws Exception {
  when(cache.get(acl.getObjectIdentity()))
      .thenReturn(new Element(acl.getId(), acl));
  myCache.evictFromCache(acl.getObjectIdentity());
  verify(cache).remove(acl.getId());
  verify(cache).remove(acl.getObjectIdentity());
}

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

@Test
  public void evictCacheObjectIdentity() throws Exception {
    when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl));

    myCache.evictFromCache(acl.getId());

    verify(cache).remove(acl.getId());
    verify(cache).remove(acl.getObjectIdentity());
  }
}

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

@Test
public void testEhCacheFactoryBeanWithSelfPopulatingCache() {
  EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
  cacheManagerFb.afterPropertiesSet();
  try {
    CacheManager cm = cacheManagerFb.getObject();
    EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
    cacheFb.setCacheManager(cm);
    cacheFb.setCacheName("myCache1");
    cacheFb.setCacheEntryFactory(key -> key);
    assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class);
    cacheFb.afterPropertiesSet();
    Ehcache myCache1 = cm.getEhcache("myCache1");
    assertTrue(myCache1 instanceof SelfPopulatingCache);
    assertEquals("myKey1", myCache1.get("myKey1").getObjectValue());
  }
  finally {
    cacheManagerFb.destroy();
  }
}

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

@Test
public void testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache() {
  EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
  cacheManagerFb.afterPropertiesSet();
  try {
    CacheManager cm = cacheManagerFb.getObject();
    EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
    cacheFb.setCacheManager(cm);
    cacheFb.setCacheName("myCache1");
    cacheFb.setCacheEntryFactory(new UpdatingCacheEntryFactory() {
      @Override
      public Object createEntry(Object key) {
        return key;
      }
      @Override
      public void updateEntryValue(Object key, Object value) {
      }
    });
    assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class);
    cacheFb.afterPropertiesSet();
    Ehcache myCache1 = cm.getEhcache("myCache1");
    assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache);
    assertEquals("myKey1", myCache1.get("myKey1").getObjectValue());
  }
  finally {
    cacheManagerFb.destroy();
  }
}

相关文章

Ehcache类方法