public void clearCache(String scene, IAccount me) throws ServiceException { // 清除缓存 cacheService.del(AreaDivisionQueryer.AREA_DIVISION_KEY, scene); }
public Long get(String phone, String type) throws ServiceException { return cache.get(SMS_LAST_SEND_TIME, getKey(phone, type), Long.class); }
public void checkChangeEmailValidCode(String email, String validCode, boolean remove) throws ServiceException { String msg = "Failed to CheckChangePhoneCode. "; checkEmail(email, msg); if (VerifyTools.isBlank(validCode)) { log.error(msg + "params is null: validCode"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } if (!cache.exist(CacheKeys.VALID_CODE_CHANGE_PHONE.name(), email)) { throw new ServiceException(UserErrorCode.USER_EMAIL_VALID_CODE_EXPIRED); } if (!validCode.equals(cache.get(CacheKeys.VALID_CODE_CHANGE_PHONE.name(), email, String.class))) { throw new ServiceException(UserErrorCode.USER_EMAIL_VALID_CODE_ERROR); } if (remove) { cache.del(CacheKeys.VALID_CODE_REPASSWORD.name(), email); } }
@Override public V put(K key, V value) throws CacheException { if (log.isTraceEnabled()) { log.trace("Putting object in cache[{}] for key[{}]", name, key); } try { cacheService.set(name, key.toString(), serializeValue(value)); return null; } catch (Throwable t) { throw new CacheException(t); } }
@Override public AreaBean findByCode(String scene, String areaCode) throws ServiceException { if (VerifyTools.isBlank(scene)) { log.error("params is null: SceneType"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } if (VerifyTools.isBlank(areaCode)) { log.error("params is null: areaCode"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } AreaBean bean = cacheService.hget(AREA_DIVISION_KEY, scene, areaCode, AreaBean.class); if (bean == null) { AreaDivisionWhere w = new AreaDivisionWhere(); w.setSceneType(scene); w.setAreaCode(areaCode); AreaDivisionBean temp = areaDivisionBasic.find(w); if (temp == null) { bean = null; } else { bean = temp.to(AreaBean.class); cacheService.hset(AREA_DIVISION_KEY, scene, areaCode, bean); } } return bean; }
@Override public V put(K key, V value) throws CacheException { if (log.isTraceEnabled()) { log.trace("Putting object in cache[{}] for key[{}]", name, key); } try { cacheService.hset(name, key.toString(), serializeValue(value)); return null; } catch (Throwable t) { throw new CacheException(t); } }
@Override public V remove(K key) throws CacheException { if (log.isTraceEnabled()) { log.trace("Removing object from cache[{}] for key[{}]", name, key); } try { V previous = get(key); cacheService.hdel(name, key.toString()); return previous; } catch (Throwable t) { throw new CacheException(t); } }
/** 设置过期时间 **/ public void expire(K key, Duration time) { cacheService.expire(name, key.toString(), time); if (log.isTraceEnabled()) { String duration = ConvertTools.toDuration(time.toMillis()); log.trace("Set expire time from cache[{}] for key[{}] --> {}", name, key, duration); } }
@Override public V get(K key) throws CacheException { if (log.isTraceEnabled()) { log.trace("Getting object from cache[{}] for key[{}]", name, key); } try { if (key == null) { if (log.isTraceEnabled()) { log.trace("Value for key[{}] is null.", key); } return null; } else { String rawValue = cacheService.hget(name, key.toString(), String.class); return deserializeValue(rawValue); } } catch (Throwable t) { throw new CacheException(t); } }
@Override public Collection<V> values() { try { Map<String, String> all = cacheService.haget(name, String.class); if (VerifyTools.isBlank(all)) { return Collections.emptyList(); } else { List<V> values = new ArrayList<V>(all.size()); for (Entry<String, String> entry : all.entrySet()) { String rawValue = entry.getValue(); if (VerifyTools.isNotBlank(rawValue)) { values.add(deserializeValue(rawValue)); } } return Collections.unmodifiableList(values); } } catch (Throwable t) { throw new CacheException(t); } }
@SuppressWarnings("unchecked") @Override public Set<K> keys() { try { Set<String> keys = cacheService.hkeys(name); if (VerifyTools.isBlank(keys)) { return Collections.emptySet(); } else { Set<K> newKeys = new HashSet<K>(); for (String key : keys) { newKeys.add((K) key); } return newKeys; } } catch (Throwable t) { throw new CacheException(t); } }
public void checkLoginValidCode(String email, String validCode, boolean remove) throws ServiceException { String msg = "Failed to CheckLoginCode. "; checkEmail(email, msg); if (VerifyTools.isBlank(validCode)) { log.error(msg + "params is null: validCode"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } // 获得验证码 if (!cache.exist(CacheKeys.VALID_CODE_LOGIN.name(), email)) { throw new ServiceException(UserErrorCode.USER_EMAIL_VALID_CODE_EXPIRED); } if (!validCode.equals(cache.get(CacheKeys.VALID_CODE_LOGIN.name(), email, String.class))) { throw new ServiceException(UserErrorCode.USER_EMAIL_VALID_CODE_ERROR); } if (remove) { cache.del(CacheKeys.VALID_CODE_LOGIN.name(), email); } }
public void sendLoginValidCode(String email) throws ServiceException { String msg = "Failed to SendLoginCode. "; checkEmail(email, msg); // 获得验证码 String validCode = RandomTools.generateNumber(4); // 存入缓存 // 缓存时间(单位:秒) int time = smsService.getCacheTime(Constants.EmailTemplate.login); cache.set(CacheKeys.VALID_CODE_LOGIN.name(), email, validCode, new Second(time)); // 验证码:@1@,@2@分钟内有效。 smsService.send(email, Constants.EmailTemplate.login, validCode, time / 60); }
@Override public List<KeyString> listAreaText(String scene, List<String> areaCodes) throws ServiceException { if (VerifyTools.isBlank(scene)) { log.error("params is null: SceneType"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } if (VerifyTools.isBlank(areaCodes)) { return null; } Map<String, AreaBean> map; if (cacheService.exist(AREA_DIVISION_KEY, scene)) { // 从缓存中查询 map = cacheService.haget(AREA_DIVISION_KEY, scene, AreaBean.class); } else { // 从数据库查询 PageList<AreaDivisionBean> temp = listAllFromDb(scene); if (temp == null) { return null; } else { List<AreaDivisionBean> list = temp.toList(); map = new HashMap<>(); for (AreaBean bean : list) { map.put(bean.getAreaCode(), bean.to(AreaBean.class)); } // 放入缓存 cacheService.hmset(AREA_DIVISION_KEY, scene, map); } } return AreaDivisionTools.getAreaText(areaCodes, map); }
cacheService.hset(AreaDivisionQueryer.AREA_DIVISION_KEY, scene, areaCode, bean.to(AreaBean.class));
@Override public void delete(String scene, String id, IAccount me) throws ServiceException { String msg = "Failed to delete AreaDivision. "; if (VerifyTools.isBlank(me)) { log.error(msg + "params is null: me"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } if (VerifyTools.isBlank(id)) { log.error(msg + "params is null: id"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } if (VerifyTools.isBlank(scene)) { log.error(msg + "params is null: SceneType"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } // 从gn_area_division表查询原记录 AreaDivisionBean older = areaDivisionBasic.findById(id); if (older == null) { log.error(msg + "record is not found.\n\tid is " + id); throw new ServiceException(ResultCode.RECORD_NOT_EXIST); } // 删除gn_area_division表的记录 areaDivisionBasic.deleteByIds(Arrays.asList(id), false); // 清除缓存 cacheService.hdel(AreaDivisionQueryer.AREA_DIVISION_KEY, scene, older.getAreaCode()); }
public void checkRepasswordValidCode(String phone, String validCode, boolean remove) throws ServiceException { String msg = "Failed to CheckRepasswordCode. "; checkPhone(phone, msg); if (VerifyTools.isBlank(validCode)) { log.error(msg + "params is null: validCode"); throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED); } // 获得验证码 if (!cache.exist(CacheKeys.VALID_CODE_REPASSWORD.name(), phone)) { throw new ServiceException(UserErrorCode.USER_SMS_VALID_CODE_EXPIRED); } if (!validCode.equals(cache.get(CacheKeys.VALID_CODE_REPASSWORD.name(), phone, String.class))) { throw new ServiceException(UserErrorCode.USER_SMS_VALID_CODE_ERROR); } if (remove) { cache.del(CacheKeys.VALID_CODE_REPASSWORD.name(), phone); } }