96 lines
2.4 KiB
Java
96 lines
2.4 KiB
Java
package com.tiesheng.util.service;
|
||
|
||
import cn.hutool.core.util.StrUtil;
|
||
import cn.hutool.extra.servlet.ServletUtil;
|
||
import cn.hutool.extra.spring.SpringUtil;
|
||
import com.alibaba.fastjson.JSON;
|
||
import com.tiesheng.util.ServletKit;
|
||
import com.tiesheng.util.exception.ApiException;
|
||
import com.tiesheng.util.service.cache.TsCacheHandler;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import java.util.Set;
|
||
|
||
@Service
|
||
public class TsCacheService {
|
||
|
||
@Autowired
|
||
TsCacheHandler tsCacheHandler;
|
||
|
||
/**
|
||
* 获取一个缓存
|
||
*
|
||
* @return
|
||
*/
|
||
public static TsCacheService of() {
|
||
return SpringUtil.getBean(TsCacheService.class);
|
||
}
|
||
|
||
public void put(String key, String value) {
|
||
tsCacheHandler.put(key, value);
|
||
}
|
||
|
||
public void put(String key, String value, long seconds) {
|
||
tsCacheHandler.put(key, value, seconds);
|
||
}
|
||
|
||
public void putObj(String key, Object value, long seconds) {
|
||
tsCacheHandler.put(key, JSON.toJSONString(value), seconds);
|
||
}
|
||
|
||
public String get(String key) {
|
||
return tsCacheHandler.get(key);
|
||
}
|
||
|
||
public String get(String key, long seconds) {
|
||
return tsCacheHandler.get(key, seconds);
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取一个缓存对象
|
||
*
|
||
* @param key
|
||
* @param tClass
|
||
* @param seconds =0时获取数据后会删除缓存,<0时不改变原有缓存时间
|
||
* @param <T>
|
||
* @return
|
||
*/
|
||
public <T> T getObj(String key, Class<T> tClass, long seconds) {
|
||
return JSON.parseObject(get(key, seconds), tClass);
|
||
}
|
||
|
||
public void remove(String key) {
|
||
tsCacheHandler.remove(key);
|
||
}
|
||
|
||
/**
|
||
* 获取key
|
||
*
|
||
* @param prefix
|
||
* @return
|
||
*/
|
||
public Set<String> keys(String prefix) {
|
||
return tsCacheHandler.keys(prefix);
|
||
}
|
||
|
||
|
||
///////////////////////////////////////////////////////////////////////////
|
||
// 图形验证码
|
||
///////////////////////////////////////////////////////////////////////////
|
||
|
||
/**
|
||
* 验证 图片验证码
|
||
*/
|
||
public void verifyCaptcha(String value) {
|
||
String captchaKey = ServletKit.getHeader("captcha");
|
||
String cache = get(captchaKey);
|
||
if (StrUtil.isEmpty(cache) || !StrUtil.equals(cache, value)) {
|
||
throw new ApiException("验证码不正确");
|
||
}
|
||
remove(captchaKey);
|
||
}
|
||
|
||
}
|