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

tech.hdis.framework.security.session.imp.RedisSessionClient Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package tech.hdis.framework.security.session.imp;

import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 缓存工具包
 *
 * @author 黄志文
 */
@Component
public class RedisSessionClient {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 设置缓存
     *
     * @param key   key
     * @param value value
     * @param time  time(秒为单位)
     */
    void set(String key, String value, long time) {
        BoundValueOperations valueOps = stringRedisTemplate.boundValueOps(key);
        valueOps.set(value);
        if (time > 0) {
            valueOps.expire(time, TimeUnit.SECONDS);
        }
    }

    /**
     * 删除缓存
     *
     * @param key key
     */
    void del(String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * 查询缓存
     *
     * @param key key
     * @return value
     */
    String get(String key) {
        BoundValueOperations valueOps = stringRedisTemplate.boundValueOps(key);
        return valueOps.get();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy