import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class CacheService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setCacheWithDefaultTime(String key,String value,long minute){
stringRedisTemplate.opsForValue().set(key,value,minute, TimeUnit.MINUTES);
}
public void setCach(String key,String value){
stringRedisTemplate.opsForValue().set(key,value);
}
public Boolean exitsKey(String key){
return stringRedisTemplate.hasKey(key);
}
public String getCache(String key){
if(exitsKey(key)){
return stringRedisTemplate.opsForValue().get(key);
}
return "";
}
}
以下用于储存时拼接到key前边在redis里形成文件夹,取的时候也不要忘记拼接上
public class KeyUtils {
public static final String CODE_PREFIX="captcha_code:";
}