Spring Session中的SaveMode和FlushMode有什么区别?

问题描述 投票:0回答:1

Spring Session有两个枚举,一个叫 SaveMode 和一个叫 FlushMode 参考文档中没有提到SaveMode和Flush模式。javadoc的描述听起来非常相似。

  • Save Mode和Flush Mode到底控制什么?
  • 哪些用例需要显式设置SaveMode和Flush Mode?

保存模式(SaveMode)

/**
 * Supported modes of tracking and saving session changes to session store.
 *
 * @author Rob Winch
 * @author Vedran Pavic
 * @since 2.2.0
 */
public enum SaveMode {

    /**
     * Save only changes made to session, for instance using
     * {@link Session#setAttribute(String, Object)}. In highly concurrent environments,
     * this mode minimizes the risk of attributes being overwritten during processing of
     * parallel requests.
     */
    ON_SET_ATTRIBUTE,

    /**
     * Same as {@link #ON_SET_ATTRIBUTE} with addition of saving attributes that have been
     * read using {@link Session#getAttribute(String)}.
     */
    ON_GET_ATTRIBUTE,

    /**
     * Always save all session attributes, regardless of the interaction with the session.
     * In highly concurrent environments, this mode increases the risk of attributes being
     * overwritten during processing of parallel requests.
     */
    ALWAYS

}

冲洗模式

/**
 * Supported modes of writing the session to session store.
 *
 * @author Rob Winch
 * @author Vedran Pavic
 * @since 2.2.0
 */
public enum FlushMode {

    /**
     * Only writes to session store when {@link SessionRepository#save(Session)} is
     * invoked. In a web environment this is typically done as soon as the HTTP response
     * is committed.
     */
    ON_SAVE,

    /**
     * Writes to session store as soon as possible. For example
     * {@link SessionRepository#createSession()} will write the session to session store.
     * Another example is that setting an attribute using
     * {@link Session#setAttribute(String, Object)} will also write to session store
     * immediately.
     */
    IMMEDIATE

}

java spring spring-session
1个回答
1
投票

现在看看 春季会议文件:

flushMode: 允许指定何时将数据写入Redis。默认情况下,只有在调用保存时,才会将数据写入 ReactiveSessionRepository. 价值为 FlushMode.IMMEDIATE 尽快写入Redis。

翻开源码,具体在RedisSession->flushImmediateIfNecessary()这个函数调用。

至于作用是什么?SaveMode,我也在寻找答案。

© www.soinside.com 2019 - 2024. All rights reserved.