123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package com.gxzc.zen.umps.config
- import cn.gygxzc.tina.cache.redis.RedisKeyGenerator
- import com.gxzc.zen.umps.util.SerializeUtils
- import org.apache.shiro.cache.Cache
- import org.apache.shiro.cache.CacheException
- import org.apache.shiro.subject.PrincipalCollection
- import org.slf4j.LoggerFactory
- import org.springframework.data.redis.core.RedisTemplate
- import java.nio.charset.Charset
- import java.util.*
- import java.util.concurrent.TimeUnit
- /**
- *
- * @author NorthLan
- * @date 2018/4/23
- * @url https://noahlan.com
- */
- @Suppress("UNCHECKED_CAST")
- class ShiroRedisCache<V> : Cache<Any, V> {
- companion object {
- private val logger = LoggerFactory.getLogger(ShiroRedisCache::class.java)
- }
- private var redisTemplate: RedisTemplate<String, V>
- private var prefix = "zen_shiro_redis:"
- private var charset: Charset = Charsets.UTF_8
- private val properties: ShiroRedisProperties
- constructor(redisTemplate: RedisTemplate<String, V>, properties: ShiroRedisProperties) {
- this.redisTemplate = redisTemplate
- this.properties = properties
- }
- constructor(redisTemplate: RedisTemplate<String, V>, prefix: String, properties: ShiroRedisProperties, charset: Charset = Charsets.UTF_8) {
- this.redisTemplate = redisTemplate
- this.prefix = prefix
- this.properties = properties
- this.charset = charset
- }
- override fun values(): MutableCollection<V> {
- val keys = keys()
- val values = ArrayList<V>(keys.size)
- for (k in keys) {
- get(k)?.let {
- values.add(it)
- }
- }
- return values
- }
- override fun clear() {
- redisTemplate.delete(keys())
- }
- override fun put(p0: Any?, p1: V?): V? {
- logger.debug("Put/ Key: $p0, value: $p1")
- if (p0 == null || p1 == null) {
- return null
- }
- return try {
- redisTemplate.boundValueOps(getByteKey(p0)).set(p1)
- // vo.expire(properties.cacheTime, TimeUnit.SECONDS)
- p1
- } catch (e: Throwable) {
- throw CacheException(e)
- }
- }
- override fun remove(p0: Any?): V? {
- logger.debug("Remove/ Key: $p0")
- if (p0 == null) {
- return null
- }
- return try {
- val key = getByteKey(p0)
- val v = redisTemplate.boundValueOps(key).get()
- redisTemplate.delete(key)
- v
- } catch (e: Throwable) {
- throw CacheException(e)
- }
- }
- override fun size(): Int {
- return keys().size
- }
- override fun get(p0: Any?): V? {
- logger.debug("Get/ key: $p0")
- if (p0 == null) {
- return null
- }
- return try {
- val vo = redisTemplate.boundValueOps(getByteKey(p0))
- vo.expire(properties.cacheTime, TimeUnit.SECONDS)
- vo.get()
- } catch (e: Throwable) {
- throw CacheException(e)
- }
- }
- override fun keys(): MutableSet<String> {
- logger.debug("Keys/ ")
- return redisTemplate.keys(getByteKey("*"))
- }
- private fun getByteKey(key: Any): String? {
- return when (key) {
- is String -> "${this.prefix}${RedisKeyGenerator.SEPARATOR}$key"
- is PrincipalCollection -> getByteKey(key.primaryPrincipal)
- else -> SerializeUtils.serialize(key).toString()
- }
- }
- }
|