3

I have this problem.

There is a spring session stored in redis,saved this way

HMSET spring:session:sessions:33fdd1b6-b496-4b33-9f7d-df96679d32fe creationTime 1404360000000 \
    maxInactiveInterval 1800 \
    lastAccessedTime 1404360000000 \
    sessionAttr:attrName someAttrValue \
    sessionAttr2:attrName someAttrValue2

As far as I understand is a hashed set.

I can access it via redis-cli with HMGET that as far as I understand is to get values of hashed keys.

Works great.

Now on spring, I want to access it via Redis Template (org.springframework.data.redis.core).

So, I did this

@Autowired RedisOperationsSessionRepository redisOperationsSessionRepository;

@Autowired RedisTemplate redisTemplate;

and tried...

stringRedisTemplate.opsForValue().get(loggedSessionId);

redisTemplate.opsForHash().get("mykey", WHAT GOES HERE?)
redisOperationsSessionRepository.sessionRedisOperations.opsForHash().get(mykey", OR HERE?)

so far I cannot access it to the information, any idea what hash I have to define? there is no info how spring gets it ...

Thanks.

EDIT:

What I basically want to do is

COPY this

enter image description here

and save it in other redis using another template...

as you can see, it has not the value serialized in right way, could be a problem if I just copy and "paste" the objects using opsForHash or will I save corrupted data?

1 Answer 1

5
+100

Quote from https://redis.io/topics/data-types

Redis Hashes are maps between string fields and string values ...

So

redisTemplate.opsForHash().get("mykey", "myKeyHash")

Will return value of the field myKeyHash stored in key mykey.

For your examle your fileds would be creationTime, maxInactiveInterval, lastAccessedTime and attributes.

By the way there is a more convinient method:

redisTemplate.opsForHash().entries("mykey")

that returns map of all fileds contained in that hash with their values.

EDIT

Since Spring uses JdkSerializationRedisSerializer for valules and StringRedisSerializer for keys/hash keys, you must configure your RedistTemplate as follows to be able to read data correctly:

RedisTemplate<String, Object> template = new RedisTemplate<>(); 
RedisSerializer stringSerializer = new StringRedisSerializer();
template.setConnectionFactory(...); 
template.setKeySerializer(stringSerializer); 
template.setHashKeySerializer(stringSerializer);
13
  • is there any converter i must specify when creating my redis template??
    – jpganz18
    Jul 25, 2017 at 9:23
  • and... what kind of object is the value from "sessionAttr:SPRING_SECURITY_CONTEXT" ?
    – jpganz18
    Jul 25, 2017 at 9:24
  • Thanks @chimmi, do you know if there is any easier way to get the user principal currently logged with data saved in redis?
    – jpganz18
    Jul 25, 2017 at 14:13
  • @jpganz18 If you want current user, why do you Redis? Why can`t you just pull it from SecurityContextHolder or http session?
    – chimmi
    Jul 25, 2017 at 17:08
  • because I am running it by a filter before the session filter runs, when I run my filter there is no info of the user principal in the request information
    – jpganz18
    Jul 25, 2017 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.