package com.games.gateway.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @description: redis 工具类
 * @author: mzc
 * @date: 2022-09-21 16:23
 **/
@Component
public class RedisUtils {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * redis string设置
     *
     * @param key
     * @param val
     */
    public void stringSet(String key, String val) {
        stringRedisTemplate.opsForValue().set(key, val);
    }

    /**
     * redis string获取
     *
     * @param key
     * @return
     */
    public String stringGet(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * redis string获取
     *
     * @param key
     * @return
     */
    public Integer stringGetToInt(String key) {
        String str = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotEmpty(str)) {
            return Integer.valueOf(str);
        } else {
            return 0;
        }
    }

}
