hutool实现本地锁,本地token功能

实现本地锁,本地token功能  。

TimedCache类中put()设置的时间比newTimedCache()设置的时间优先级高,get()会刷新时间。

package com.navigation.berth.common.util;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.thread.ThreadUtil;
import com.navigation.berth.entity.SysUser;


/**
 * @author dll
 */
public class LocalCache {
    // 超过一分钟缓存自动删除
    public static final TimedCache<String, String> LOCK = CacheUtil.newTimedCache(1000*3);

    public static final long TOKEN_ACTION_TIME = 24*60*60*1000;

    // 超过1天缓存自动删除
    public static final TimedCache<String, SysUser> TOKENS = CacheUtil.newTimedCache(TOKEN_ACTION_TIME);

    static {
        /** 每100ms检查一次过期 */
        LOCK.schedulePrune(100);
        /** 每10秒检查一次过期 */
        TOKENS.schedulePrune(10000);
    }

    /**
     * 存入键值对,提供消逝时间
     *
     * @param key
     * @param value
     * @param timeout
     */
    public static void put(TimedCache<String, String>  cache,String key, String value, Long timeout) {
        /** 设置消逝时间 */
        cache.put(key, value, timeout);
    }

    /**
     * 每次重新get一次缓存,均会重新刷新消逝时间
     *
     * @param key
     * @return
     */
    public static String get(TimedCache<String, String>  cache,String key) {
        return cache.get(key);
    }


    public static void setToken(String key, SysUser value) {
        TOKENS.put(key, value ,TOKEN_ACTION_TIME);
    }

    public static SysUser getToken(String key) {
        return TOKENS.get(key);
    }



    static Lock rtLock = new ReentrantLock();
    public static boolean lock(String key, String value, Long timeout) {
        if (rtLock.tryLock()) {
            try {
                if (LOCK.containsKey(key)) {
                    return false;
                } else {
                    LocalCache.put(LOCK, key, "1", timeout);
                    return true;
                }
            } finally {
                rtLock.unlock();
            }
        } else {
            return false;
        }
    }



    public static void unLock(String key) {
        LOCK.remove(key);
    }


    public static void main(String[] args) {
//        put(LOCK,"haha", "1", 3000L);
        LOCK.put("haha", "1",2000L);
        ThreadUtil.sleep(2000);
        if (LOCK.containsKey("haha")) {
            System.out.println("aa");
        }
        System.out.println("第1次结果:" + get(LOCK,"haha"));
        ThreadUtil.sleep(2000);
        System.out.println("第2次结果:" + get(LOCK,"haha"));
        ThreadUtil.sleep(3000);
        System.out.println("第3次结果:" + get(LOCK,"haha"));
        if (LOCK.containsKey("haha")) {
            System.out.println("aa");
        }
//        // 取消定时清理
//        LOCK.cancelPruneSchedule();

//        Thread ta = new Thread(() -> {
//            System.out.println(lock("haha", "1", 3000L));
//        });
//
//        Thread tb = new Thread(() -> {
//            System.out.println(lock("haha", "1", 3000L));
//        });
//
//        ta.start();
        ThreadUtil.sleep(10);
//        tb.start();

    }

}

login 方法

        @ApiOperation(value = "登录接口", notes = "登录接口")
        @PostMapping(value="/login")
        public Result login(@RequestBody SysUser sysUser, HttpServletRequest request){
                SysUser user = sysUserService.lambdaQuery().eq(SysUser::getUserName,sysUser.getUserName()).one();
                Assert.notNull(user,"账号不存在");
                Assert.equals(user.getUserPwd(),DigestUtil.md5Hex(sysUser.getUserPwd()),"密码错误");
//                request.getSession().setAttribute("user",user);

                Map map = new HashMap<>();
                String token = UUID.randomUUID().toString();
                LocalCache.setToken(token,user);
                map.put("token",token);
                return  Result.ok(map);
        }

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值