All Downloads are FREE. Search and download functionalities are using the official Maven repository.

work.wangjw.dao.impl.AuthRedisDao Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package work.wangjw.dao.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import work.wangjw.bean.AuthSession;
import work.wangjw.dao.AuthDao;

import java.util.Objects;
import java.util.Set;

/**
 * @author Gavin
 * @since 2021/7/21 10:10
 **/
@ConditionalOnProperty(prefix = "wjw.auth", name = "storage", havingValue = "redis")
@Slf4j
public class AuthRedisDao implements AuthDao {

    RedisTemplate objectRedisTemplate;
    RedisTemplate stringRedisTemplate;

    @Autowired
    public void init(RedisConnectionFactory connectionFactory) {
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        RedisTemplate objectRedisTemplate = new RedisTemplate<>();
        objectRedisTemplate.setConnectionFactory(connectionFactory);
        objectRedisTemplate.setKeySerializer(stringRedisSerializer);
        objectRedisTemplate.setValueSerializer(jsonRedisSerializer);
        objectRedisTemplate.setHashKeySerializer(stringRedisSerializer);
        objectRedisTemplate.setHashValueSerializer(jsonRedisSerializer);
        objectRedisTemplate.afterPropertiesSet();
        this.objectRedisTemplate = objectRedisTemplate;
        RedisTemplate stringRedisTemplate = new RedisTemplate<>();
        stringRedisTemplate.setConnectionFactory(connectionFactory);
        stringRedisTemplate.setKeySerializer(stringRedisSerializer);
        stringRedisTemplate.setValueSerializer(stringRedisSerializer);
        stringRedisTemplate.setHashKeySerializer(stringRedisSerializer);
        stringRedisTemplate.setHashValueSerializer(stringRedisSerializer);
        stringRedisTemplate.afterPropertiesSet();
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public void set(String key, AuthSession value) {
        objectRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public AuthSession get(String key) {
        return (AuthSession) (objectRedisTemplate.opsForValue().get(key));
    }

    @Override
    public void delete(String key) {
        objectRedisTemplate.delete(key);
    }

    @Override
    public void clearExpireData() {
        Set keys = objectRedisTemplate.keys("auth*");
        if (Objects.nonNull(keys)) {
            for (String key : keys) {
                AuthSession authSession = get(key);
                Long expireTime = authSession.getExpireTime();
                Long activityExpireTime = authSession.getActivityExpireTime();
                long now = System.currentTimeMillis();
                if ((expireTime != -1L && expireTime < now) || (activityExpireTime != -1L && activityExpireTime < now)) {
                    log.info("清除key=" + key);
                    delete(key);
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy