package cn.reghao.tnb.message.app.redis.ds;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

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

/**
 * @author reghao
 * @date 2021-03-07 17:44:23
 */
@Component
public class RedisHash<T> {
    private final HashOperations<String, String, T> hashOps;

    public RedisHash(RedisTemplate<String, Object> redisTemplate) {
        this.hashOps = redisTemplate.opsForHash();
    }

    public void set(String key, String hashKey, T hashValue) {
        hashOps.put(key, hashKey, hashValue);
    }

    public T get(String key, String hashKey) {
        return hashOps.get(key, hashKey);
    }

    public List<T> multiGet(String key, Set<String> hashKeys) {
        return hashOps.multiGet(key, hashKeys);
    }

    public Map<String, T> entries(String key) {
        return hashOps.entries(key);
    }

    public void delete(String key, String hashKey) {
        hashOps.delete(key, hashKey);
    }

    public void hset(String key, String hashKey, T hashValue) {
        hashOps.put(key, hashKey, hashValue);
    }

    public void hincrby(String key, String hashKey, int value) {
        hashOps.increment(key, hashKey, value);
    }

    public Long hlen(String key) {
        return hashOps.size(key);
    }

    public void hdel(String key, String hashKey) {
        hashOps.delete(key, hashKey);
    }

    public Map<String, T> hgetall(String key) {
        return hashOps.entries(key);
    }
}
