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
<T> T get(Object key, Callable<T> valueLoader);

Source Link

Document

Return the value to which this cache maps the specified key, obtaining that value from valueLoader if necessary.

Usage

From source file:us.swcraft.springframework.cache.aerospike.usage.AerospikeCacheManagerKryoIT.java

@Test
public void getCacheNew_writeRead() {
    String name = "cache:ITNEW";
    Cache c = aerospikeCacheManager.getCache(name);
    c.put("B", "DEADBEEF");
    assertThat(c.get("B", String.class), is("DEADBEEF"));
    c.evict("B");
    assertThat(c.get("B"), nullValue());
}

From source file:us.swcraft.springframework.cache.aerospike.usage.AerospikeCacheManagerFastSerializeIT.java

@Test
public void getCacheDefault_writeRead() {
    String name = "cache:ITPRECONF";
    Cache c = aerospikeCacheManager.getCache(name);
    c.put("A", "DEADBEEF");
    assertThat(c.get("A", String.class), is("DEADBEEF"));
    c.evict("A");
    assertThat(c.get("A"), nullValue());
}

From source file:us.swcraft.springframework.cache.aerospike.usage.spring.AerospikeCacheManagerSpringIT.java

@Test
public void service_cacheableName() {
    String name1i = cacheableService.getName(1);
    String name1c = cacheableService.getName(1);
    assertThat(name1c, is(name1i));//from   ww  w  .  j  a  va 2s  .c  om
    assertThat(name1c, not(sameInstance(name1i)));

    String name = "cache:ITDEFAULT";
    Cache c = aerospikeCacheManager.getCache(name);
    String cachedName = c.get(1, String.class);
    assertThat(cachedName, is(name1i));
}

From source file:us.swcraft.springframework.cache.aerospike.usage.spring.AerospikeCacheManagerSpringIT.java

@Test
public void service_cacheableDescription() {
    String desc1i = cacheableService.getDescription(1);
    String desc1c = cacheableService.getDescription(1);
    assertThat(desc1c, is(desc1i));/*ww w .ja v a  2  s  . c o  m*/
    assertThat(desc1c, not(sameInstance(desc1i)));

    String name = "cache:ITUUID";
    Cache c = aerospikeCacheManager.getCache(name);
    String cachedDescription = c.get(1, String.class);
    assertThat(cachedDescription, is(desc1i));
}

From source file:us.swcraft.springframework.cache.aerospike.usage.AerospikeCacheManagerFastSerializeIT.java

@Test
public void getCacheNew_writeRead() {
    String name = "cache:ITNEW";
    Cache c = aerospikeCacheManager.getCache(name);
    String id = UUID.randomUUID().toString();
    c.put(id, "DEADBEEF");
    assertThat(c.get(id, String.class), is("DEADBEEF"));
    c.evict(id);// ww  w  .  j  av  a2 s.  c o m
    assertThat(c.get(id), nullValue());
    System.out.print(false);
}

From source file:us.swcraft.springframework.cache.aerospike.config.annotation.EnableAerospikeCacheManagerKryoReflectionIT.java

@Test
public void storeObject_noDefaultConstructor() {
    String name = "ITPRECONF";
    Cache c = cacheManager.getCache(name);
    assertThat(c, notNullValue());//w ww  .  jav  a2  s  .c  o m

    StoredNoDefaultConstructor s = new StoredNoDefaultConstructor("ID", "NAME");
    c.put("NDC", s);
    StoredNoDefaultConstructor result = c.get("NDC", StoredNoDefaultConstructor.class);
    assertThat(result, notNullValue());
    assertThat(result.getId(), is("ID"));
    assertThat(result.getName(), is("NAME"));
}

From source file:us.swcraft.springframework.cache.aerospike.config.annotation.EnableAerospikeCacheManagerKryoIT.java

@Test
public void storeObject_notImplementingSerializable() {
    String name = "ITPRECONF";
    Cache c = cacheManager.getCache(name);
    assertThat(c, notNullValue());//from w  w  w  .  jav a2 s  . c  o m

    StoredNotSerializable s = new StoredNotSerializable();
    s.setId("ID");
    s.setName("NAME");
    c.put("NS", s);
    StoredNotSerializable result = c.get("NS", StoredNotSerializable.class);
    assertThat(result, notNullValue());
    assertThat(result.getId(), is("ID"));
    assertThat(result.getName(), is("NAME"));
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ??/* www.  j a v  a 2  s  .  c  o  m*/
 *
 * @param id       token id
 * @param password ?
 */
public void resetPassword(String id, String password, String confirmPassword) {

    if (!StringUtils.equals(password, confirmPassword)) {
        throw new ServiceException("????");
    }

    Cache cache = cacheManager.getCache(resetPasswordCacheName);
    ValidToken entity = cache.get(id, ValidToken.class);

    if (entity == null) {
        throw new TokenNotFoundException("???");
    }

    long creationTime = entity.getCreationDate().getTime();
    long currentTime = System.currentTimeMillis();

    try {
        if (currentTime - creationTime > resetPasswordExpiredTime) {
            throw new TokenExpiredException("ID[" + entity.getId() + "]");
        }

        Integer userId = Casts.cast(entity.getPrincipal().get("id"), Integer.class);

        Apis.invoke("accountService", "updateUserPassword", userId, password);
    } finally {
        cache.evict(id);
    }
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ?? key/*  w  ww  .ja v  a 2  s  . com*/
 *
 * @param id key
 *
 * @return ??? map?
 */
public ValidToken validForgotPassword(String id) {

    Cache cache = cacheManager.getCache(forgotPasswordCacheName);
    ValidToken entity = cache.get(id, ValidToken.class);
    if (entity == null) {
        throw new TokenNotFoundException("???");
    }

    try {

        long creationTime = entity.getCreationDate().getTime();
        long currentTime = System.currentTimeMillis();

        if (currentTime - creationTime > forgotPasswordExpiredTime) {
            throw new TokenExpiredException("ID[" + entity.getId() + "]");
        }
    } finally {
        cache.evict(id);
    }

    Map<String, Object> principal = entity.getPrincipal();
    id = new Md5Hash(principal.get("id").toString() + System.currentTimeMillis()).toHex();
    ValidToken validToken = new ValidToken(id, new Date(), principal);

    cache = cacheManager.getCache(resetPasswordCacheName);
    cache.put(id, validToken);

    return validToken;
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ?// www  . j  a v a 2s  .  com
 *
 * @param id  id
 *
 */
public void validMail(String id) {

    Cache cache = cacheManager.getCache(validMailCacheName);
    MailValidToken entity = cache.get(id, MailValidToken.class);

    if (entity == null) {
        throw new TokenNotFoundException("?ID[" + id + "]?");
    }

    try {
        long creationTime = entity.getCreationDate().getTime();
        long currentTime = System.currentTimeMillis();

        if (currentTime - creationTime > validMailExpiredTime) {
            cache.evict(id);
            throw new TokenExpiredException("ID[" + entity.getId() + "]");
        }

        Map<String, Object> principal = Subjects.getPrincipal();

        if (!principal.get("id").equals(entity.getPrincipal().get("id"))) {
            throw new ServiceException("???");
        }

        principal.put("isBindingMail", 1);
        principal.put("email", entity.getMail());

        Apis.invoke("accountService", "saveUser", principal, new ArrayList<>());
    } finally {
        cache.evict(id);
    }
}