The Java backend interface adds front-end visible attributes to the response header

scenes to be used:

When the background data update needs to notify the front end to update the cache synchronously, the front end can be notified by adding an attribute in the response header in the interface interceptor.

Processing logic:

1. Manage user cache status.

import java.util.HashMap;
import java.util.Map;

/**
 * Manage whether front-end users need to clear the cache status
 */
public class CacheUtil {
     private static Map<String,Boolean> cacheMap = new HashMap<>();
     /**
     * Add/modify user cache status
     * @param username */ public static void putStatus(String username, boolean b){ cacheMap .put(username,b);    } /**      * Get user cache status      * @param username * @return */ public static
     
    
        


    


     
     
    Boolean getStatus(String username) {
        Boolean b = cacheMap.get(username);
        if (b==null) {
            return false;
        }
        return b;
    }
    /**
     * 获取登录用户缓存状态
     * @return
     */
    public static Boolean getStatus() {
        String username = UserUtil.getLoginUser().getUsername();
        Boolean cacheStatus = cacheMap.get(username);
        boolean b = false;
        if (cacheStatus!=null) {
            b=cacheStatus;
        }
        putStatus (username, false );
        return b;
    }

    /**
     * Set the cache status of the logged-in user
     */ public static void setStatus( boolean b){ for (String username: cacheMap .keySet()){ cacheMap .put(username,b);         }     } }
    
        
            


2. Add state in the interceptor.

httpResponse.setHeader( "Access-Control-Expose-Headers" , "cache-status" );// [Set this attribute to be visible at the front end]
httpResponse.setHeader( "cache-status" , CacheUtil. getStatus ().toString()) ;// [Set attributes]

3. Modify the cache state after the data is changed.

 to sum up

When the background data changes, the front-end status will be notified through the status in the response header of the interface

Guess you like

Origin blog.csdn.net/wangpei930228/article/details/109235963