티스토리 뷰

package com.lunastratos.mudspoon.Service

import com.lunastratos.mudspoon.Entity.RefreshTokenEntity
import com.lunastratos.mudspoon.Repository.RefreshTokenRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Service
import java.time.Duration


@Service
class RedisService @Autowired constructor(
    private val refeshRepo: RefreshTokenRepository,
    private val stringRedisTemplate: StringRedisTemplate
){
    @Value("\${mudSpoon.refresh_token_time_s}")
    private lateinit var refeshTokenValidTime :String

    fun save(accessToken:String, refreshToken :String){
        val refreshTokenValue = RefreshTokenEntity(refreshToken, refreshToken)
        refeshRepo.save(refreshTokenValue)

    }


    fun getData(key: String?): String? {
        val valueOperations = stringRedisTemplate!!.opsForValue()
        return valueOperations[key!!]
    }

    fun setData(key: String, value: String) {
        val valueOperations = stringRedisTemplate!!.opsForValue()
        valueOperations[key] = value
    }

    fun setDataExpire(key: String, value: String, duration: Long) {
        val valueOperations = stringRedisTemplate!!.opsForValue()
        val expireDuration: Duration = Duration.ofSeconds(duration)
        valueOperations.set(key, value, expireDuration)
    }

    //시간 지정됨
    fun setDataExpired(key: String, value: String) {
        val valueOperations = stringRedisTemplate!!.opsForValue()
        val expireDuration: Duration = Duration.ofSeconds(refeshTokenValidTime.toLong())
        valueOperations.set(key, value, expireDuration)
    }

    fun deleteData(key: String) {
        stringRedisTemplate!!.delete(key)
    }
}

 

다음과 같은 방식으로 RedisService를 만들어서 사용중이다. 테스트삼아 

 

@Test
	fun redisConnectionTest() {
		val expireDuration: Duration = Duration.ofSeconds(10)
		redisService.setDataExpire("test1", "111", 10)
	}

로 10초동안 살리고 그 다음은 자동으로 없어지는 것을 테스트 해봤다.

 

10초후에 없어진다. 이를 통해서 토큰의 살아있는 시간을 설정 가능하다. 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함