package com.javaweb.platform.utils;
|
|
|
|
import com.javaweb.common.utils.IdGenerate;
|
|
import cn.hutool.cache.CacheUtil;
|
import cn.hutool.cache.impl.TimedCache;
|
|
public class UserAuthCacheUtils {
|
|
|
private static long timeout = 1000* 60 *60 * 2;//2小时过期
|
|
//用户缓存hash表
|
public static TimedCache<String, Object> userCaches = CacheUtil.newTimedCache(timeout);
|
|
//用户双向关联缓存 ,防止用户无限次登录 userCaches 中存储多份用户数据
|
public static TimedCache<String, String> userCachesRel = CacheUtil.newTimedCache(timeout);
|
|
|
/**
|
* 取消定时清理
|
*/
|
static{
|
userCaches.cancelPruneSchedule();
|
userCachesRel.cancelPruneSchedule();
|
}
|
|
|
/**
|
* @param key token
|
* @param object
|
* @param rel 用户主键
|
*/
|
public static void setCahce(String key ,Object object,String rel){
|
if(isExist(rel)){
|
removeCahce(rel);
|
}
|
|
userCaches.put(key, object, timeout);
|
userCachesRel.put(rel, key,timeout);
|
}
|
|
/**
|
* 获取用户
|
* @param key
|
* @return
|
*/
|
public static Object getCahce(String key){
|
return userCaches.get(key,false);
|
}
|
|
/**
|
* 移除用户缓存
|
* @param key
|
* @param rel
|
*/
|
public static void removeCahce(String rel){
|
String key = userCachesRel.get(rel,false);
|
userCaches.remove(key);
|
userCachesRel.remove(rel);
|
|
}
|
/**
|
* 强制移除
|
* @param key
|
* @param rel
|
*/
|
public static void removeCahce(String key , String rel){
|
userCaches.remove(key);
|
userCachesRel.remove(rel);
|
}
|
|
/**
|
* 判断用户是否存在缓存中
|
* @param rel
|
* @return
|
*/
|
public static boolean isExist(String rel){
|
String key = userCachesRel.get(rel,false);
|
if(key!=null){
|
return true;
|
}
|
return false;
|
}
|
|
public static void clear(){
|
userCaches.clear();
|
userCachesRel.clear();
|
}
|
|
}
|