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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| import redis.RedisExample import cats.effect.IO import cats.effect.kernel.Resource import cats.effect.unsafe.implicits.global import cn.dev33.satoken.dao.SaTokenDao import cn.dev33.satoken.session.SaSession import cn.dev33.satoken.util.SaFoxUtil import cn.hutool.json.JSONUtil import dev.profunktor.redis4cats.RedisCommands import dev.profunktor.redis4cats.effects.{KeyScanArgs, RedisType} import java.util import scala.concurrent.duration.* import scala.jdk.CollectionConverters.*
class RedisTokenDao extends SaTokenDao { val redis: Resource[IO, RedisCommands[IO, String, String]] = RedisExample.api
override def update(key: String, value: String): Unit = { val expire = getTimeout(key) if (expire == SaTokenDao.NOT_VALUE_EXPIRE) return set(key, value, expire) }
override def getTimeout(key: String): Long = { redis .use { r => r.ttl(key).flatMap { case Some(duration) => IO.pure(duration.toSeconds) case None => IO.pure(0L) } } .unsafeRunSync() }
override def set(key: String, value: String, timeout: Long): Unit = { if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) return if (timeout == SaTokenDao.NEVER_EXPIRE) redis.use(r => r.set(key, value)).unsafeRunSync() else redis.use(r => r.setEx(key, value, timeout.seconds)).unsafeRunSync() }
override def getObject(key: String): AnyRef = { redis .use { r => r.get(key).flatMap { case Some(value) => IO.pure(SaSessionUtil.GetSession(JSONUtil.parse(value))) case None => IO.pure(null) } } .unsafeRunSync() }
override def updateObject(key: String, `object`: Any): Unit = { val expire = getTimeout(key) if (expire == SaTokenDao.NOT_VALUE_EXPIRE) return setObject(key, `object`, expire) }
override def setObject(key: String, `object`: Any, timeout: Long): Unit = { if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) return if (timeout == SaTokenDao.NEVER_EXPIRE) redis .use(r => r.set(key, JSONUtil.toJsonPrettyStr(`object`))) .unsafeRunSync() else redis .use(r => r.setEx(key, JSONUtil.toJsonPrettyStr(`object`), timeout.seconds) ) .unsafeRunSync() }
override def deleteObject(key: String): Unit = delete(key)
override def delete(key: String): Unit = redis.use(r => r.del(key)).unsafeRunSync()
override def getObjectTimeout(key: String): Long = getTimeout(key)
override def updateObjectTimeout(key: String, timeout: Long): Unit = updateTimeout(key, timeout)
override def updateTimeout(key: String, timeout: Long): Unit = { if (timeout == SaTokenDao.NEVER_EXPIRE) { val expire = getTimeout(key) if (expire != SaTokenDao.NEVER_EXPIRE) { this.set(key, this.get(key), timeout) } return } redis.use(r => r.expire(key, timeout.seconds)).unsafeRunSync() }
override def get(key: String): String = { redis .use { r => r.get(key).flatMap { case Some(value) => IO.pure(value) case None => IO.pure(null) } } .unsafeRunSync() }
override def searchData( prefix: String, keyword: String, start: Int, size: Int, sortType: Boolean ): util.List[String] = { val list: List[String] = redis .use { cmd => val pattern = s"$prefix*$keyword*" val keyScanArgs = KeyScanArgs(RedisType.String, pattern, 30) cmd.scan(keyScanArgs).map(cursor => cursor.keys) } .unsafeRunSync() SaFoxUtil.searchList(list.asJava, start, size, sortType) }
override def setSession(session: SaSession, timeout: Long): Unit = setObject(session.getId, session, timeout) override def updateSession(session: SaSession): Unit = updateObject(session.getId, session) override def deleteSession(sessionId: String): Unit = deleteObject(sessionId) override def getSessionTimeout(sessionId: String): Long = getObjectTimeout(sessionId) override def updateSessionTimeout(sessionId: String, timeout: Long): Unit = updateObjectTimeout(sessionId, timeout) }
|