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

com.yixan.base.web.utils.InSessionCache Maven / Gradle / Ivy

There is a newer version: 3.7.1
Show newest version
package com.yixan.base.web.utils;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import com.yixan.tools.common.cache.BaseCache;
import com.yixan.tools.common.cache.InMemoryCache;
import com.yixan.tools.common.model.Duration;

/**
 * Session中的缓存
 *
 * @author zhaohuihua
 * @version V1.0 2016年9月6日
 */
public class InSessionCache extends BaseCache {

    /** 静态实例 **/
    public static final InMemoryCache me = new InMemoryCache();

    public InSessionCache() {
    }

    @Override
    protected void set(String key, Object value, Duration duration) {
        set(key, value, duration == null ? 0 : duration.toMillis());
    }

    @Override
    protected void set(String key, Object value, long expire) {
        getSession().setAttribute(key, value);

        if (expire > 0) {
            expire(key, expire);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    protected  T get(String key, Class clazz) {
        return (T) getSession().getAttribute(key);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected  List list(String key, Class clazz) {
        return (List) getSession().getAttribute(key);
    }

    @Override
    protected boolean exist(String key) {
        return getSession().getAttributeKeys().contains(key);
    }

    @Override
    protected void del(String key) {
        // 删除
        getSession().removeAttribute(key);
    }

    @Override
    protected void expire(String key, Duration duration) {
        expire(key, duration == null ? 0 : duration.toMillis());
    }

    @Override
    protected void expire(String key, long expire) {
        // 不支持过期时间
    }

    private Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy