Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

spring-session-data-redis: about the SaveMode.ON_GET_ATTRIBUTE

$
0
0

I find the is just to be used in the method of org.springframework.session.data.redis.RedisIndexedSessionRepository.RedisSession#getAttribute

In my opinion, getAttribute has not function, because the cached is got form redis ather through SessionRepositoryFilter and the DispacthServlet flush session when invoke doService , and back to SessionRepositoryFilter save the RedisSession' delta to redis.

so getAttribute form cached is equal with form redis, and getAttribute will put the key-value to delta in the SaveMode.ON_GET_ATTRIBUTE and has the Attribute.So it maybe put the some key-value back to redis.I really unkown why spring write that.

public <T> T getAttribute(String attributeName) {            T attributeValue = this.cached.getAttribute(attributeName);            if (attributeValue != null && RedisIndexedSessionRepository.this.saveMode.equals(SaveMode.ON_GET_ATTRIBUTE)) {                this.delta.put(RedisIndexedSSaveMode.ON_GET_ATTRIBUTEessionRepository.getSessionAttrNameKey(attributeName), attributeValue);            }            return attributeValue;        }

I've racked my brains for a long time for what is the function of SaveMode.ON_GET_ATTRIBUTE and asked many times to chatgpt, but it always gave me error answer and repeat error many time.

I also see the offical docment.

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

and the Test

@Test    void saveWithSaveModeOnGetAttribute() {        given(this.redisOperations.<String, Object>boundHashOps(anyString())).willReturn(this.boundHashOperations);        given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);        given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);        this.redisRepository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE);        MapSession delegate = new MapSession();        delegate.setAttribute("attribute1", "value1");        delegate.setAttribute("attribute2", "value2");        delegate.setAttribute("attribute3", "value3");        RedisSession session = this.redisRepository.new RedisSession(delegate, false);        session.getAttribute("attribute2");        session.setAttribute("attribute3", "value4");        this.redisRepository.save(session);        assertThat(getDelta()).hasSize(2);    }

I think the Test is just as I thought.It repeatly add attribute2=value2 to redis.I really unkown why the Test is writend like that.

please, who can tell me waht the function of SaveMode.ON_GET_ATTRIBUTE


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>