@Cacheable(value="accountCache")
例:
@Service
public class AccountService{
private final Logger logger = LoggerFactory.getLogger(AccountService2.class);
//使用缓存名叫 accountCache
@Cacheable(value="accountCache")
public Account getAccountByName(String accountName) {
logger.info("real querying account... {}", accountName);
Optional<Account> accountOptional = getFromDB(accountName);
if (!accountOptional.isPresent()) {
throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
}
return accountOptional.get();
}
private Optional<Account> getFromDB(String accountName) {
logger.info("real querying db... {}", accountName);
//Todo query data from database
return Optional.fromNullable(new Account(accountName));
}
}