ShiroRedisCache.kt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.gxzc.zen.umps.config
  2. import cn.gygxzc.tina.cache.redis.RedisKeyGenerator
  3. import com.gxzc.zen.umps.util.SerializeUtils
  4. import org.apache.shiro.cache.Cache
  5. import org.apache.shiro.cache.CacheException
  6. import org.apache.shiro.subject.PrincipalCollection
  7. import org.slf4j.LoggerFactory
  8. import org.springframework.data.redis.core.RedisTemplate
  9. import java.nio.charset.Charset
  10. import java.util.*
  11. import java.util.concurrent.TimeUnit
  12. /**
  13. *
  14. * @author NorthLan
  15. * @date 2018/4/23
  16. * @url https://noahlan.com
  17. */
  18. @Suppress("UNCHECKED_CAST")
  19. class ShiroRedisCache<V> : Cache<Any, V> {
  20. companion object {
  21. private val logger = LoggerFactory.getLogger(ShiroRedisCache::class.java)
  22. }
  23. private var redisTemplate: RedisTemplate<String, V>
  24. private var prefix = "zen_shiro_redis:"
  25. private var charset: Charset = Charsets.UTF_8
  26. private val properties: ShiroRedisProperties
  27. constructor(redisTemplate: RedisTemplate<String, V>, properties: ShiroRedisProperties) {
  28. this.redisTemplate = redisTemplate
  29. this.properties = properties
  30. }
  31. constructor(redisTemplate: RedisTemplate<String, V>, prefix: String, properties: ShiroRedisProperties, charset: Charset = Charsets.UTF_8) {
  32. this.redisTemplate = redisTemplate
  33. this.prefix = prefix
  34. this.properties = properties
  35. this.charset = charset
  36. }
  37. override fun values(): MutableCollection<V> {
  38. val keys = keys()
  39. val values = ArrayList<V>(keys.size)
  40. for (k in keys) {
  41. get(k)?.let {
  42. values.add(it)
  43. }
  44. }
  45. return values
  46. }
  47. override fun clear() {
  48. redisTemplate.delete(keys())
  49. }
  50. override fun put(p0: Any?, p1: V?): V? {
  51. logger.debug("Put/ Key: $p0, value: $p1")
  52. if (p0 == null || p1 == null) {
  53. return null
  54. }
  55. return try {
  56. redisTemplate.boundValueOps(getByteKey(p0)).set(p1)
  57. // vo.expire(properties.cacheTime, TimeUnit.SECONDS)
  58. p1
  59. } catch (e: Throwable) {
  60. throw CacheException(e)
  61. }
  62. }
  63. override fun remove(p0: Any?): V? {
  64. logger.debug("Remove/ Key: $p0")
  65. if (p0 == null) {
  66. return null
  67. }
  68. return try {
  69. val key = getByteKey(p0)
  70. val v = redisTemplate.boundValueOps(key).get()
  71. redisTemplate.delete(key)
  72. v
  73. } catch (e: Throwable) {
  74. throw CacheException(e)
  75. }
  76. }
  77. override fun size(): Int {
  78. return keys().size
  79. }
  80. override fun get(p0: Any?): V? {
  81. logger.debug("Get/ key: $p0")
  82. if (p0 == null) {
  83. return null
  84. }
  85. return try {
  86. val vo = redisTemplate.boundValueOps(getByteKey(p0))
  87. vo.expire(properties.cacheTime, TimeUnit.SECONDS)
  88. vo.get()
  89. } catch (e: Throwable) {
  90. throw CacheException(e)
  91. }
  92. }
  93. override fun keys(): MutableSet<String> {
  94. logger.debug("Keys/ ")
  95. return redisTemplate.keys(getByteKey("*"))
  96. }
  97. private fun getByteKey(key: Any): String? {
  98. return when (key) {
  99. is String -> "${this.prefix}${RedisKeyGenerator.SEPARATOR}$key"
  100. is PrincipalCollection -> getByteKey(key.primaryPrincipal)
  101. else -> SerializeUtils.serialize(key).toString()
  102. }
  103. }
  104. }