RedisUtil工具类

package com.moyu.redarmy.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public final class RedisUtil {
    public static final int DEVICE_USER = 0;
    public static final int DEVICE_EXPERIENCER = 1;
    public static final int DEVICE_LEADER = 2;
    public static final int DEVICE_CONTROL = 3;
    public static final int ROOM_EXPERIENCERS = 4;
    public static final int TYPE_DEVICE = 5;

    @Autowired
    private RedisTemplate redisTemplate;
    // =============================common============================

    public String generateKey(String key, int type) {
        String _key = "";
        switch (type) {
//            case RedisUtil.DEVICE_EXPERIENCER: {
//                _key="Device:" + key + ":Experiencer";
//                break;
//            }
//            case RedisUtil.DEVICE_LEADER: {
//                _key="Device:" + key + ":Leader";
                _key="Device:" + key + ":Experiencer";
//                break;
//            }
            case RedisUtil.DEVICE_CONTROL: {
                _key = "Device:" + key + ":Controller";
                break;
            }
            case RedisUtil.ROOM_EXPERIENCERS: {
                _key = "Room:" + key + ":Experiencer";
                break;
            }
            case RedisUtil.TYPE_DEVICE: {
                _key="Type:"+key;
                break;
            }
            default: {
                _key = "Device:" + key + ":User";
                break;
            }
        }
        return _key;
    }

    /**
     * 指定缓存失效时间
     *
     * @param key  键
     * @param time 时间(秒)
     * @return
     */
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据key 获取过期时间
     *
     * @param key 键 不能为null
     * @return 时间(秒) 返回0代表为永久有效
     */
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值