当前位置:网站首页>Actual combat in ThreadLocal project
Actual combat in ThreadLocal project
2022-07-25 02:31:00 【WuSong1999】
ThreadLocal The function of is to share data in the same thread , Every request in the project comes in , It is the same thread from beginning to end , In the same thread, if the data put in memory by the previous thread is to be shared with the next thread , Then you can use it ThreadLocal
ThreadLocal The core principle of is , At the bottom of it is a Map<Thread,Object> threadLocal structure
/**
* Before executing the target method , First judge the user login status . And encapsulate user information to Controller
*/
public class CartInterceptor implements HandlerInterceptor {
public static ThreadLocal<UserInfoTo> threadLocal = new ThreadLocal<>();
/*
Intercept before target method
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
UserInfoTo userInfoTo = new UserInfoTo();
HttpSession session = request.getSession();
MemberRespVo member = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
if (member != null) {
// The user login
userInfoTo.setUserId(member.getId());
}
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
userInfoTo.setUserKey(cookie.getValue());
userInfoTo.setTempUser(true);
}
}
}
// Before the target method is executed
// If there are no temporary users , Assign temporary users
if (StringUtils.isEmpty(userInfoTo.getUserKey())) {
String uuid = UUID.randomUUID().toString();
userInfoTo.setUserKey(uuid);
}
threadLocal.set(userInfoTo);
return true;
}
/*
After business execution Assign temporary users
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
UserInfoTo userInfoTo = threadLocal.get();
// If there is no information , Create temporary user , preservation cookie
if (!userInfoTo.isTempUser()) {
Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTo.getUserKey());
cookie.setDomain("gulimall.com");
// Unit second
cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
response.addCookie(cookie);
}
}
}
边栏推荐
- File file name change
- Use SAP ui5 application to consume create and delete operations of OData in business application studio
- When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
- I was forced to graduate by a big factory and recited the eight part essay in a two-month window. Fortunately, I went ashore, otherwise I wouldn't be able to repay the mortgage
- "I gave up programming and wrote a 1.3 million word hard science fiction."
- Solution to the occupation of project startup port
- Consul cluster deployment
- Let's customize the loader
- "Nowadays, more than 99.9% of the code is garbage!"
- HAC cluster is modified to stand-alone
猜你喜欢

"Nowadays, more than 99.9% of the code is garbage!"

JVM Foundation

A bit of knowledge - websites about scripts

Babbitt | metauniverse daily must read: Dubai launched the national metauniverse strategy, which plans to increase the number of related companies of metauniverse by five times in the next five years

Let's customize the loader

Deep understanding of string class
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil

VRRP virtual redundancy protocol configuration

TS uses a third-party library, and there is no type declaration file error handling

Simulate the implementation of StrCmp function
随机推荐
Detailed explanation of manually writing servlet in idea
Redis unauthorized access vulnerability recurrence (www.hetianlab.com)
Codeforces:d1. choosing carrots (easy version) [max min problem + control one side to make the other side as close as possible + thinking]
Google launched another "man grabbing war" for core making, and Intel's 17 year veteran joined!
Server performance monitoring
Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022
6-10 vulnerability exploitation SMTP experimental environment construction
[C language] program compilation (preprocessing)
Remote sensing image classification tool and visualization application of WebGIS
Pycharm writes SQLite statements without code prompts
Let's customize the loader
Android memory optimized disk cache
Text reading end judgment
Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
Use SAP ui5 application to consume create and delete operations of OData in business application studio
Remove & lt; li&gt; Front blank distance
When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
Arthas case: dynamic update application logger level
[system design] distributed key value database
Four redis cluster schemes you must know and their advantages and disadvantages