Example usage for org.springframework.cache Cache get

List of usage examples for org.springframework.cache Cache get

Introduction

In this page you can find the example usage for org.springframework.cache Cache get.

Prototype

@Nullable
ValueWrapper get(Object key);

Source Link

Document

Return the value to which this cache maps the specified key.

Usage

From source file:example.caching.AbstractBookRepositoryTest.java

private static void assertCacheHit(Object key, Book book, Cache... caches) {

    for (Cache cache : caches) {

        Cache.ValueWrapper wrapper = cache.get(key);

        assertThat("An entry should have been found in " + cache + " with key " + key, wrapper,
                is(notNullValue()));/*from   www.j  a va2s  .c  om*/
        assertThat("Wrong value for entry in " + cache + " with key " + key, (Book) wrapper.get(), is(book));
    }
}

From source file:example.caching.AbstractBookRepositoryTest.java

private static void assertCacheMiss(Object key, Cache... caches) {
    for (Cache cache : caches) {
        assertThat("No entry should have been found in " + cache + " with key " + key, cache.get(key),
                is(nullValue()));//from   w w  w .  j a  v a2s  .  co  m
    }
}

From source file:com.baidu.rigel.biplatform.cache.StoreManager.java

/**
 * cache?KEY???//from  ww  w .j a  va  2  s. c  om
 * 
 * @param cache
 *            ?cache
 * @param key
 *            key
 * @param clazz
 *            ?
 * @return 
 * @throws IllegalArgumentException
 *             ?
 */
@SuppressWarnings("unchecked")
public static <T> T getFromCache(Cache cache, String key, Class<T> clazz) {
    if (cache == null || StringUtils.isBlank(key)) {
        throw new IllegalArgumentException("illegal params ,cache:" + cache + " key:" + key);
    }
    ValueWrapper valueWrapper = cache.get(key);
    if (valueWrapper != null) {
        return (T) valueWrapper.get();
    }
    return null;
}

From source file:springfox.documentation.spring.web.caching.CachingAspect.java

private Object cachedValue(ProceedingJoinPoint joinPoint, String cacheName, Object key) throws Throwable {
    Cache cache = this.cache.getCache(cacheName);
    if (cache.get(key) == null) {
        cache.put(key, joinPoint.proceed());
    }//from  w  ww .  j a  v a2s  .c  om
    return cache.get(key).get();
}

From source file:example.springdata.jpa.caching.CachingRepositoryTests.java

@Test
public void cachesValuesReturnedForQueryMethod() {

    User dave = new User();
    dave.setUsername("dmatthews");

    dave = repository.save(dave);//from w  w  w  .j  a v  a 2s  .  c  o  m

    User result = repository.findByUsername("dmatthews");
    assertThat(result, is(dave));

    // Verify entity cached
    Cache cache = cacheManager.getCache("byUsername");
    ValueWrapper wrapper = cache.get("dmatthews");
    assertThat(wrapper.get(), is((Object) dave));
}

From source file:sample.cache.SampleCacheApplicationTests.java

@Test
public void validateCache() {
    Cache countries = this.cacheManager.getCache("countries");
    assertThat(countries, is(notNullValue()));
    countries.clear(); // Simple test assuming the cache is empty
    assertThat(countries.get("BE"), is(nullValue()));
    Country be = this.countryRepository.findByCode("BE");
    assertThat((Country) countries.get("BE").get(), is(be));
}

From source file:net.eusashead.spring.gaecache.GaeCacheManagerITCase.java

@Test
public void testCacheManager() throws Exception {
    Cache cache = cacheManager.getCache("default");

    // Check cache is empty
    Assert.assertNotNull(cache);/*from   www  .  j  a  va 2  s.c o  m*/
    GaeCacheKey cacheKey = GaeCacheKey.create("key");
    Assert.assertNull(cache.get(cacheKey));

    // Put something in the cache
    cache.put(cacheKey, "foo");
    Assert.assertEquals(new SimpleValueWrapper("foo").get(), cache.get(cacheKey).get());

    // Check consistency
    Assert.assertNotNull(cache.get(cacheKey));
}

From source file:de.iew.services.impl.MessageBundleServiceImpl.java

protected MessageBundleStore getBundleFromCache(Locale locale, String basename) {
    MessageBundleStore messageBundleStore = null;
    String cacheKey = messageBundleKey(locale, basename);

    Cache cache = this.cacheManager.getCache(this.cacheName);
    if (cache != null) {
        Cache.ValueWrapper value = cache.get(cacheKey);
        if (value != null) {
            if (log.isTraceEnabled()) {
                log.trace("Cache HIT: MessageBundle fr Cache Key " + cacheKey + " im Cache vorhanden.");
            }/*  w ww  .ja  v a2  s. c o  m*/
            messageBundleStore = (MessageBundleStore) value.get();
        }
    }
    return messageBundleStore;
}

From source file:net.eusashead.spring.gaecache.GaeCacheITCase.java

@Test
public void testCacheNullKey() throws Exception {

    // Create a cache
    Cache cache = new GaeCache("nullCache");

    // Cache a null value
    Foo foo = new Foo(new FooKey(1l), "null");
    GaeCacheKey cacheKey = GaeCacheKey.create(null);
    cache.put(cacheKey, foo);// w  w w . jav  a 2  s.  c om

    // Check cached value
    Assert.assertNotNull(cache.get(cacheKey));
    Assert.assertNotNull(cache.get(cacheKey).get());
    Assert.assertEquals(foo, cache.get(cacheKey).get());

}

From source file:org.hsweb.web.oauth2.service.OAuth2ServiceImpl.java

@Override
@Transactional(noRollbackFor = AccessTimeoutException.class)
public User getUserByAccessToken(String accessToken) {
    OAuth2Access auth2Access = null;/*from ww  w  . ja  v  a2 s .  c o m*/
    Cache cache = null;
    String cacheKey = "accessToken:".concat(accessToken);
    boolean inCache = false;
    if (cacheManager != null) {
        cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            Cache.ValueWrapper wrapper = cache.get(cacheKey);
            if (wrapper != null) {
                auth2Access = (OAuth2Access) wrapper.get();
                inCache = true;
            }
        }
    }
    if (auth2Access == null)
        auth2Access = oAuth2AccessMapper.selectByAccessToken(accessToken);
    if (auth2Access == null) {
        return null;
    }
    //?
    if (auth2Access.getLeftTime() <= 0) {
        if (cache != null) {
            cache.evict(cacheKey);
        }
        // TODO: 16-8-17 token
        oAuth2AccessMapper.deleteById(auth2Access.getId());
        throw new AuthorizeException("expired_token");
    }
    if (!inCache) {
        User user = userService.selectByPk(auth2Access.getUserId());
        user.initRoleInfo();
        user.setPassword(null);
        User newUser = new User();
        try {
            BeanUtilsBean.getInstance().getPropertyUtils().copyProperties(newUser, user);
        } catch (Exception e) {
        }
        auth2Access.setUser(newUser);
        cache.put(cacheKey, auth2Access);
        return newUser;
    } else {
        return auth2Access.getUser();
    }
}