Reids
Redis手机验证码(1)
@RequestMapping("/getloginsms")
@ResponseBody
public DemoEntity getSmsCode(@RequestParam(value = "userId") String userId) {
Jedis jedis = RedisProvider.getJedis();
DemoEntity entity = new DemoEntity();
String str= null;
boolean isExist = jedis.exists(userId+ "_smslogin");
if (isExist) {
str= jedis.get(userId+ "_smslogin");
return entity;
} else {
str= TestRedis.getRandom();
jedis.setex(userId+ "_smslogin", 60, str);
}
entity.setPwd(str);
return entity;
}
public static String getRandom() {
String num = "";
for (int i = 0 ; i < 6 ; i ++) {
num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1));
}
return num;
}
Redis手机验证码(2)三次限制
public class TestValidateCode {
public static void main(String[] args) {
login("13912127777","778380");
}
public static String getCode(){
String code = "";
Random random = new Random();
for (int i = 0; i < 6; i++) {
int n = random.nextInt(10);
code+=n;
}
return code;
}
public static void setValidateCode(String phone){
Jedis jedis = new Jedis("139.224.102.249",6379);
String codeKey = "validate_"+phone+"_code";
String countKey = "validate_"+phone+"_count";
String count = jedis.get(countKey);
if(count==null){
jedis.setex(countKey,24*60*60,"1");
}else if(Integer.parseInt(count)<=2){
jedis.incr(countKey);
}else if(Integer.parseInt(count)>2){
System.out.println("今天内已经获取三次密码,请明日再来!");
jedis.close();
return;
}
jedis.set(codeKey,getCode());
jedis.expire(codeKey,120);
jedis.close();
}
public static void login(String phone,String code){
Jedis jedis = new Jedis("139.224.102.249",6379);
String redisCode = jedis.get("validate_"+phone+"_code");
if(redisCode==null){
System.out.println("验证码已失效,请重新获取");
return;
}
if(redisCode.equals(code)){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
jedis.close();
}
}
购物车(未登录和已登录)
实体层
未登录购物车实体类
@Data
public class CookieCart {
private Long productId;
private int amount;
}
登录用户购物车实体类
@Data
public class Cart {
private Long userId;
private Long productId;
private int amount;
}
业务层
@PostMapping(value = "/addCart")
public void addCart(CookieCart obj) {
String cartId=this.getCookiesCartId();
String key=COOKIE_KEY+cartId;
Boolean hasKey = redisTemplate.opsForHash().getOperations().hasKey(key);
if(hasKey){
this.redisTemplate.opsForHash().put(key, obj.getProductId().toString(),obj.getAmount());
}else{
this.redisTemplate.opsForHash().put(key, obj.getProductId().toString(), obj.getAmount());
this.redisTemplate.expire(key,90, TimeUnit.DAYS);
}
}
@PostMapping(value = "/updateCart")
public void updateCart(CookieCart obj) {
String cartId=this.getCookiesCartId();
String key=COOKIE_KEY+cartId;
this.redisTemplate.opsForHash().put(key, obj.getProductId().toString(),obj.getAmount());
}
@PostMapping(value = "/delCart")
public void delCart(Long productId) {
String cartId=this.getCookiesCartId();
String key=COOKIE_KEY+cartId;
this.redisTemplate.opsForHash().delete(key, productId.toString());
}
@PostMapping(value = "/findAll")
public CartPage findAll() {
String cartId=this.getCookiesCartId();
String key=COOKIE_KEY+cartId;
CartPage<CookieCart> cartPage=new CartPage();
long size=this.redisTemplate.opsForHash().size(key);
cartPage.setCount((int)size);
Map<String,Integer> map= this.redisTemplate.opsForHash().entries(key);
List<CookieCart> cartList=new ArrayList<>();
for (Map.Entry<String,Integer> entry:map.entrySet()){
CookieCart cart=new CookieCart();
cart.setProductId(Long.parseLong(entry.getKey()));
cart.setAmount(entry.getValue());
cartList.add(cart);
}
cartPage.setCartList(cartList);
return cartPage;
}
@PostMapping(value = "/mergeCart")
public void mergeCart(Long userId) {
String cartId=this.getCookiesCartId();
String keycookie=COOKIE_KEY+cartId;
Map<String,Integer> map= this.redisTemplate.opsForHash().entries(keycookie);
String keyuser = "cart:user:" + userId;
this.redisTemplate.opsForHash().putAll(keyuser,map);
this.redisTemplate.delete(keycookie);
Cookie cookie=new Cookie("cartId",null);
cookie.setMaxAge(0);
response.addCookie(cookie);
}
public String getCookiesCartId(){
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("cartId")){
return cookie.getValue();
}
}
}
long id=this.idGenerator.incrementId();
Cookie cookie=new Cookie("cartId",String.valueOf(id));
response.addCookie(cookie);
return id+"";
}
}
@Service
public class IdGenerator {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final String ID_KEY = "id:generator:cart";
public Long incrementId() {
long n=this.stringRedisTemplate.opsForValue().increment(ID_KEY);
return n;
}
}