NutzCN Logo
问答 caffeine缓存 后台如何获取对应的缓存
发布于 1801天前 作者 Hamming 3534 次浏览 复制 上一个帖子 下一个帖子
标签:

如下 不同手机号 取得不同的验证码 我后台 如何 通过手机号 如何取到对应的验证码 来验证 是否正确呢

package io.nutz.nutzsite.module.open.api;

import io.nutz.nutzsite.common.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nutz.boot.starter.caffeine.Cache;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;

/**
 * 登录注册
 * @Author: Haimming
 * @Date: 2019-05-15 10:28
 * @Version 1.0
 */
@Api(value = "会员登录")
@IocBean
@At("/open/api/login")
public class OpenLoginController {
    private static final Log log = Logs.get();


    @GET
    @ApiOperation(value = "获取验证码", notes = "发送手机号 返回验证码", httpMethod="GET")
    @At("/getVerificationCode/?")
    @Ok("json:full")
    @Cache("verificationCode")
    public Object getVerificationCode(String phone) {
        if (Strings.isEmpty(phone)) {
            return Result.error("手机号为空");
        }
        log.debug("REST request to send sms : {}" + phone);
        String msg;
        String code = R.captchaNumber(6);;
        msg = "验证码发送成功" + code;
        return Result.success("system.success",msg);
    }
}

8 回复

这caffeine是缓存页面的?不是通用缓存?

nutzboot caffeine缓存 我要如何 在程序内 查询到呢

不晓得,读一下源码?

大概思路
1、封装一个iocbean 维护一个caffeine对象,配置好caffeine的过期策略
2、在OpenLoginController注入iocbean
3、每发一个验证码,以手机号为key,验证码为value,写入caffeine
4、验证时直接读取手机号key对应的value进行验证即可

近期打算封装一个应用缓存的starter

看了源码 获取是这样


@Override public void filter(InterceptorChain chain) throws Throwable { Method method = chain.getCallingMethod(); if (method.getReturnType() == void.class) { log.warnf("method [%s] is void,should not use @Cache", method); chain.doChain(); return; } String name = method.getAnnotation(org.nutz.boot.starter.caffeine.Cache.class).value(); CacheStrategy strategy = cacheStrategyMap.get(name); if (strategy == null) { log.warnf("CacheStrategy[%s] on Method[%s] doesn't exist", name, method); chain.doChain(); return; } Cache<String, Object> cache = getCache(strategy); String key = getKey(method, chain.getArgs()); Object value = cache.getIfPresent(key); if (value == null) { chain.doChain(); cache.put(key, chain.getReturn()); } else { chain.setReturnValue(value); } } private String getKey(Method method, Object[] args) { String fullName = String.format("%s.%s", method.getDeclaringClass().getName(), method.getName()); if (args == null || args.length == 0) { return fullName; } return fullName + Arrays.stream(args).map(stringifier::stringify).collect(Collectors.joining("$")); }

name=verificationCode
key=io.nutz.nutzsite.module.open.api.OpenLoginController.getVerificationCode18187119326
写了个方法 获取对应的值
有没有方法可以获取到注解对应的路径

    public static Object getIfPresent(String name ,String path ,String key) {
        CacheStrategy strategy = cacheStrategyMap.get(name);
        Cache<String, Object> cache = getCache(strategy);
        Object value = cache.getIfPresent(path + key);
        return value;
    }

如何在 static 方法获取配置文件
public static boolean putValue(String name ,String key,Object data) {
CacheStrategy strategy = cacheStrategyMap.get(name);
if(Lang.isNotEmpty(strategy)){
Cache<String, Object> cache = getCache(strategy);
cache.put(name + key, data);
return true;
}
else {
// 获取name对应的配置文件的参数
}
return false;
}

public static boolean putValue(String name ,String key,Object data) {
 CacheStrategy strategy = cacheStrategyMap.get(name);
 if(Lang.isNotEmpty(strategy)){
 Cache<String, Object> cache = getCache(strategy);
 cache.put(name + key, data);
 return true;
 }
 else {
// 获取name对应的配置文件的参数
 }
 return false;
 }
添加回复
请先登陆
回到顶部